Predictive Recursive Descent
Rewrite a grammar with EBNF so one token of lookahead picks each rule, then run the generated recursive-descent parser.
- Lecture reference: Top-Down Parsing
- Lecture reference: Top-Down Parsing
Grammar as written, in BNF
- Left recursion E →+ E + T
- E
- E + T
By the production
E → E + T. - Left recursion T →+ T * F
- T
- T * F
By the production
T → T * F.
Open in Recursive Descent with Backtracking, Context-Free Grammars or LL(1) Parsing Table
RD w/prediction: eliminate left-recursion using EBNF, left factor using EBNF, and write one function per (rewritten) grammar rule.
Rewrite left recursion eliminated, common prefixes factored
- Left recursion removed Lecture reference: Top-Down Parsing
E → E + T | TE → T { + T }E → E + T | T has the form S → S α | β with α = + T and β = T. E generates all strings of the form β { α }.
Left recursion implies left associativity. With EBNF it becomes { }: a loop in the parser.
- Left recursion removed Lecture reference: Top-Down Parsing
T → T * F | FT → F { * F }T → T * F | F has the form S → S α | β with α = * F and β = F. T generates all strings of the form β { α }.
Left recursion implies left associativity. With EBNF it becomes { }: a loop in the parser.
Same strings Both grammars generate the same strings of up to 7 tokens.
The parser below is generated from the grammar under “After”.
Open the result in BNF in LL(1) Parsing TableParser one function per rule
The rewritten grammar. It follows the Rewrite panel until it is edited here.
Prediction Suitable for prediction
One token of lookahead picks every rule: no token selects two ways at any choice.
| Rule | Choice | Lookahead tokens |
|---|---|---|
| E → T { + T } | ||
| E → T { + T } | { + T } repeated | { + } left on { ), $ } |
| T → F { * F } | ||
| T → F { * F } | { * F } repeated | { * } left on { +, ), $ } |
| F → ( E ) | int | ||
| F → ( E ) | int | ( E ) alternative | { ( } |
| int alternative | { int } | |
Code the line being executed is marked
main () {// E $E ();if (token == '$')match ('$');elseerror ("main");}// E -> T { + T }E () {T ();while (token == '+') {match ('+');T ();}}// T -> F { * F }T () {F ();while (token == '*') {match ('*');F ();}}// F -> ( E ) | intF () {if (token == '(') {match ('(');E ();match (')');}else if (isdigit (token)) {match (token);}elseerror ("F");}
Run the parser on a token string
} main () returns It was the first call, so the run is over.Accepted: every token matched, including $
Call stack innermost call first
- main () called first, on int
AST built while parsing
A loop makes the tree so far the left child of each new node.
Node*E () {Node* tree = T ();while (token == '+') {match ('+');Node* plus = makeNode ('+');plus->left = tree;plus->right = T ();tree = plus;}return tree;}Node*T () {Node* tree = F ();while (token == '*') {match ('*');Node* times = makeNode ('*');times->left = tree;times->right = F ();tree = times;}return tree;}Node*F () {Node* tree;if (token == '(') {match ('(');tree = E ();match (')');}else if (isdigit (token)) {tree = makeNode (token);match (token);}elseerror ("F");return tree;}
No node is made for ( and ): the tokens are matched, and the tree of
what they enclose is returned.
return tree; E () returns tree The AST is +( int, *( int, int ) ).Trees every node that is no node’s child, with its tree
tree
Variables of every call, innermost first
- E () called first, on int
- tree
- +( int, *( int, int ) )