Syntax analysis

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 · slides 23–27
  • Lecture reference: Top-Down Parsing · slides 36–41

Grammar as written, in BNF

  • Left recursion E →+ E + T
    1. E
    2. E + T

    By the production E → E + T.

  • Left recursion T →+ T * F
    1. T
    2. T * F

    By the production T → T * F.

Open in Recursive Descent with Backtracking, Context-Free Grammars or LL(1) Parsing Table

Lecture reference: Top-Down Parsing · slide 41

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

Before
E → E + T | T
T → T * F | F
F → ( E ) | int
After
E → T { + T }
T → F { * F }
F → ( E ) | int
Result as
  1. Left recursion removed Lecture reference: Top-Down Parsing · slides 24–26
    E → E + T | T
    E → 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.

  2. Left recursion removed Lecture reference: Top-Down Parsing · slides 24–26
    T → T * F | F
    T → 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 Table

Parser 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.

RuleChoiceLookahead 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

  1. main () {
  2. // E $
  3. E ();
  4. if (token == '$')
  5. match ('$');
  6. else
  7. error ("main");
  8. }
  9. // E -> T { + T }
  10. E () {
  11. T ();
  12. while (token == '+') {
  13. match ('+');
  14. T ();
  15. }
  16. }
  17. // T -> F { * F }
  18. T () {
  19. F ();
  20. while (token == '*') {
  21. match ('*');
  22. F ();
  23. }
  24. }
  25. // F -> ( E ) | int
  26. F () {
  27. if (token == '(') {
  28. match ('(');
  29. E ();
  30. match (')');
  31. }
  32. else if (isdigit (token)) {
  33. match (token);
  34. }
  35. else
  36. error ("F");
  37. }

Run the parser on a token string

Terminals of the grammar, separated by spaces. $ is added at the end.
end of input
Step 32 of 32
Line 8 } main () returns It was the first call, so the run is over.

Accepted: every token matched, including $

Call stack innermost call first

  1. main () called first, on int

AST built while parsing

Operator rules with

A loop makes the tree so far the left child of each new node.

Grammar the functions are for
E → T { + T }
T → F { * F }
F → ( E ) | int
  1. Node*
  2. E () {
  3. Node* tree = T ();
  4. while (token == '+') {
  5. match ('+');
  6. Node* plus = makeNode ('+');
  7. plus->left = tree;
  8. plus->right = T ();
  9. tree = plus;
  10. }
  11. return tree;
  12. }
  13. Node*
  14. T () {
  15. Node* tree = F ();
  16. while (token == '*') {
  17. match ('*');
  18. Node* times = makeNode ('*');
  19. times->left = tree;
  20. times->right = F ();
  21. tree = times;
  22. }
  23. return tree;
  24. }
  25. Node*
  26. F () {
  27. Node* tree;
  28. if (token == '(') {
  29. match ('(');
  30. tree = E ();
  31. match (')');
  32. }
  33. else if (isdigit (token)) {
  34. tree = makeNode (token);
  35. match (token);
  36. }
  37. else
  38. error ("F");
  39. return tree;
  40. }

No node is made for ( and ): the tokens are matched, and the tree of what they enclose is returned.

next token: $ (6 of 6)
Step 42 of 42
Line 11 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

  1. E () called first, on int
    tree
    +( int, *( int, int ) )