encyclopegia.org

Recursive ascent parser

In computer science, recursive ascent parsing is a technique for implementing an LR parser which uses mutually-recursive functions rather than tables.[src] Thus, the parser is directly encoded in the host language similar to recursive descent.[src] Direct encoding usually yields a parser which is faster than its table-driven equivalent for the same reason that compilation is faster than interpretation.[src] It is also (nominally) possible to hand edit a recursive ascent parser, whereas a tabular implementation is nigh unreadable to the average human.[src] Recursive ascent was first described by Thomas Pennello in his article in 1986.[src] He was not intending to create a hand-editable implementation of an LR parser, but rather a maintainable and efficient parser implemented in assembly language.[src] The technique was later expounded upon by G.H.[src] Roberts in 1988 as well as in an article by Leermakers, Augusteijn, Kruseman Aretz in 1992 in the journal Theoretical Computer Science.[src] An extremely readable description of the technique was written by Morell and Middleton in 2003.[src] A good exposition can also be found in a TOPLAS article by Sperber and Thiemann.[src] Recursive ascent has also been merged with recursive descent, yielding a technique known as recursive ascent/descent.…[src]

In computer science, recursive ascent parsing is a technique for implementing an LR parser which uses mutually-recursive functions rather than tables.[src] Thus, the parser is directly encoded in the host language similar to recursive descent.[src] Direct encoding usually yields a parser which is faster than its table-driven equivalent for the same reason that compilation is faster than interpretation.[src] It is also (nominally) possible to hand edit a recursive ascent parser, whereas a tabular implementation is nigh unreadable to the average human.[src] Recursive ascent was first described by Thomas Pennello in his article in 1986.[src] He was not intending to create a hand-editable implementation of an LR parser, but rather a maintainable and efficient parser implemented in assembly language.[src] The technique was later expounded upon by G.H.[src] Roberts in 1988 as well as in an article by Leermakers, Augusteijn, Kruseman Aretz in 1992 in the journal Theoretical Computer Science.[src] An extremely readable description of the technique was written by Morell and Middleton in 2003.[src] A good exposition can also be found in a TOPLAS article by Sperber and Thiemann.[src] Recursive ascent has also been merged with recursive descent, yielding a technique known as recursive ascent/descent.[src] This implementation technique is arguably easier to hand-edit due to the reduction in states and fact that some of these states are more intuitively top-down rather than bottom up.[src] It can also yield some minimal performance improvements over conventional recursive ascent.[src] expr : expr '+' term { $$ = $1 + $3; } | expr '-' term { $$ = $1 - $3; } | term { $$ = $1; } ; term : '(' expr ')' { $$ = $2; } | num { $$ = $1; } ; num : '0' { $$ = 0; } | '1' { $$ = 1; } ; This grammar is LR(0) in that it is left-recursive (in the expr non-terminal) but does not require any lookahead.[src] Recursive ascent is also capable of handling grammars which are LALR(1) in much the same way that table-driven parsers handle such cases (by pre-computing conflict resolutions based on possible lookahead).[src] The following is a Scala implementation of a recursive ascent parser based on the above grammar: object ExprParser { private type Result = (NonTerminal, Int) private sealed trait NonTerminal { val v: Int } private case class NTexpr(v: Int, in: Stream[Char]) extends NonTerminal private case class NTterm(v: Int, in: Stream[Char]) extends NonTerminal private case class NTnum(v: Int, in: Stream[Char]) extends NonTerminal class ParseException(msg: String) extends RuntimeException(msg) { def this() = this("") def this(c: Char) = this(c.toString) } def parse(in: Stream[Char]) = state0(in)._1.v /* * 0 $accept: .[src] expr $end * * '(' shift, and go to state 1 * '0' shift, and go to state 2 * '1' shift, and go to state 3 * * expr go to state 4 * term go to state 5 * num go to state 6 */ private def state0(in: Stream[Char]) = in match { case cur #:: tail => { def loop(tuple: Result): Result = { val (res, goto) = tuple if (goto == 0) { loop(res match { case NTexpr(v, in) => state4(in, v) case NTterm(v, in) => state5(in, v) case NTnum(v, in) => state6(in, v) }) } else (res, goto - 1) } loop(cur match { case '(' => state1(tail) case '0' => state2(tail) case '1' => state3(tail) case c => throw new ParseException(c) }) } case Stream() => throw new ParseException } /* * 4 term: '(' .[src] expr ')' * * '(' shift, and go to state 1 * '0' shift, and go to state 2 * '1' shift, and go to state 3 * * expr go to state 7 * term go to state 5 * num go to state 6 */ private def state1(in: Stream[Char]): Result = in match { case cur #:: tail => { def loop(tuple: Result): Result = { val (res, goto) = tuple if (goto == 0) { loop(res match { case NTexpr(v, in) => state7(in, v) case NTterm(v, in) => state5(in, v) case NTnum(v, in) => state6(in, v) }) } else (res, goto - 1) } loop(cur match { case '(' => state1(tail) case '0' => state2(tail) case '1' => state3(tail) case c => throw new ParseException(c) }) } case Stream() => throw new ParseException } /* * 6 num: '0' .[src] * * $default reduce using rule 6 (num) */ private def state2(in: Stream[Char]) = (NTnum(0, in), 0) /* * 7 num: '1' .[src] * * $default reduce using rule 7 (num) */ private def state3(in: Stream[Char]) = (NTnum(1, in), 0) /* * 0 $accept: expr .[src] $end * 1 expr: expr .[src] '+' term * 2 | expr .[src] '-' term * * $end shift, and go to state 8 * '+' shift, and go to state 9 * '-' shift, and go to state 10 */ private def state4(in: Stream[Char], arg1: Int): Result = in match { case cur #:: tail => { decrement(cur match { case '+' => state9(tail, arg1) case '-' => state10(tail, arg1) case c => throw new ParseException(c) }) } case Stream() => state8(arg1) } /* * 3 expr: term .[src] * * $default reduce using rule 3 (expr) */ private def state5(in: Stream[Char], arg1: Int) = (NTexpr(arg1, in), 0) /* * 5 term: num .[src] * * $default reduce using rule 5 (term) */ private def state6(in: Stream[Char], arg1: Int) = (NTterm(arg1, in), 0) /* * 1 expr: expr .[src] '+' term * 2 | expr .[src] '-' term * 4 term: '(' expr .[src] ')' * * '+' shift, and go to state 9 * '-' shift, and go to state 10 * ')' shift, and go to state 11 */ private def state7(in: Stream[Char], arg1: Int): Result = in match { case cur #:: tail => { decrement(cur match { case '+' => state9(tail, arg1) case '-' => state10(tail, arg1) case ')' => state11(tail, arg1) case c => throw new ParseException(c) }) } case Stream() => throw new ParseException } /* * 0 $accept: expr $end .[src] * * $default accept */ private def state8(arg1: Int) = (NTexpr(arg1, Stream()), 1) /* * 1 expr: expr '+' .[src] term * * '(' shift, and go to state 1 * '0' shift, and go to state 2 * '1' shift, and go to state 3 * * term go to state 12 * num go to state 6 */ private def state9(in: Stream[Char], arg1: Int) = in match { case cur #:: tail => { def loop(tuple: Result): Result = { val (res, goto) = tuple if (goto == 0) { loop(res match { case NTterm(v, in) => state12(in, arg1, v) case NTnum(v, in) => state6(in, v) case _ => throw new AssertionError }) } else (res, goto - 1) } loop(cur match { case '(' => state1(tail) case '0' => state2(tail) case '1' => state3(tail) case c => throw new ParseException(c) }) } case Stream() => throw new ParseException } /* * 2 expr: expr '-' .[src] term * * '(' shift, and go to state 1 * '0' shift, and go to state 2 * '1' shift, and go to state 3 * * term go to state 13 * num go to state 6 */ private def state10(in: Stream[Char], arg1: Int) = in match { case cur #:: tail => { def loop(tuple: Result): Result = { val (res, goto) = tuple if (goto == 0) { loop(res match { case NTterm(v, in) => state13(in, arg1, v) case NTnum(v, in) => state6(in, v) case _ => throw new AssertionError }) } else (res, goto - 1) } loop(cur match { case '(' => state1(tail) case '0' => state2(tail) case '1' => state3(tail) case c => throw new ParseException(c) }) } case Stream() => throw new ParseException } /* * 4 term: '(' expr ')' .[src] * * $default reduce using rule 4 (term) */ private def state11(in: Stream[Char], arg1: Int) = (NTterm(arg1, in), 2) /* * 1 expr: expr '+' term .[src] * * $default reduce using rule 1 (expr) */ private def state12(in: Stream[Char], arg1: Int, arg2: Int) = (NTexpr(arg1 + arg2, in), 2) /* * 2 expr: expr '-' term .[src] * * $default reduce using rule 2 (expr) */ private def state13(in: Stream[Char], arg1: Int, arg2: Int) = (NTexpr(arg1 - arg2, in), 2) private def decrement(tuple: Result) = { val (res, goto) = tuple assert(goto != 0) (res, goto - 1) } } The following is a Prolog implementation of a recursive ascent parser based on the above grammar: state(S), [S] --> [S].[src] state(S0, S), [S] --> [S0].[src] /* 0.[src] S --> E$ 1.[src] E --> E + T 2.[src] E --> E - T 3.[src] E --> T 4.[src] T --> (E) 5.[src] T --> N 6.[src] N --> 0 7.[src] N --> 1 / accept --> state(s([], [e(_)])).[src] r(1) --> state(s(Ts, [t(A1), '+', e(A0)|Ss]), s(Ts, [e(A0+A1)|Ss])).[src] r(2) --> state(s(Ts, [t(A1), '-', e(A0)|Ss]), s(Ts, [e(A0-A1)|Ss])).[src] r(3) --> state(s(Ts, [t(A)|Ss]), s(Ts, [e(A)|Ss])).[src] r(4) --> state(s(Ts, [')', e(A), '('|Ss]), s(Ts, [t(A)|Ss])).[src] r(5) --> state(s(Ts, [n(A)|Ss]), s(Ts, [t(A)|Ss])).[src] r(6) --> state(s(Ts, ['0'|Ss]), s(Ts, [n(0)|Ss])).[src] r(7) --> state(s(Ts, ['1'|Ss]), s(Ts, [n(1)|Ss])).[src] t(T) --> state(s([T|Ts], Ss), s(Ts, [T|Ss])).[src] /* S --> .E$ E --> .E + T E --> .E - T E --> .T T --> .(E) T --> .N N --> .0 N --> .1 / s0 --> t('('), s3, s2, s1.[src] s0 --> t('0'), s11, s10, s2, s1.[src] s0 --> t('1'), s12, s10, s2, s1.[src] /* S --> E.$ E --> E.[src] + T E --> E.[src] - T / s1 --> accept.[src] s1 --> t('+'), s7, s1.[src] s1 --> t('-'), s8, s1.[src] /* E --> T.[src] / s2 --> r(3).[src] /* T --> (.E) E --> .E + T E --> .E - T E --> .T T --> .(E) T --> .N N --> .0 N --> .1 / s3 --> t('('), s3, s2, s4.[src] s3 --> t('0'), s11, s10, s2, s4.[src] s3 --> t('1'), s12, s10, s2, s4.[src] /* T --> (E.) E --> E .+ T E --> E .- T / s4 --> t(')'), s9.[src] s4 --> t('+'), s7, s4.[src] s4 --> t('-'), s8, s4.[src] /* E --> E + T.[src] / s5 --> r(1).[src] /* E --> E - T.[src] / s6 --> r(2).[src] /* E --> E + .T T --> .(E) T --> .N N --> .0 N --> .1 / s7 --> t('('), s3, s5.[src] s7 --> t('0'), s11, s10, s5.[src] s7 --> t('1'), s12, s10, s5.[src] /* E --> E - .T T --> .(E) T --> .N N --> .0 N --> .1 / s8 --> t('('), s3, s6.[src] s8 --> t('0'), s11, s10, s6.[src] s8 --> t('1'), s12, s10, s6.[src] /* T --> (E).[src] / s9 --> r(4).[src] /* T --> N.[src] / s10 --> r(5).[src] /* N --> '0'.[src] / s11 --> r(6).[src] /* N --> '1'.[src] / s12 --> r(7).[src] parser(Cs, T) :- length(Cs, _), phrase(s0, [s(Cs, [])], [s([], [e(T)])]).[src] % state(S0, S), [S] --> [S0, S].[src] %?- S0 = [s("(1+1)", [])|_], phrase(s0, S0, [S]), maplist(portray_clause, S0).[src] ==See also== Recursive descent parser[src]

101 sentences · 101 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Recursive ascent parser." encyclopegia.org, 2026, https://encyclopegia.org/wiki/recursive-ascent-parser. Accessed 25 November 2026.

Chicago

"Recursive ascent parser." encyclopegia.org. Accessed November 25, 2026. https://encyclopegia.org/wiki/recursive-ascent-parser.

APA

encyclopegia.org. (2026). Recursive ascent parser. Retrieved November 25, 2026, from https://encyclopegia.org/wiki/recursive-ascent-parser

Accessed November 25, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1354867431 · 0.5 · snapshot enwiki-001354867431

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.