Principles of top-down parsing


8th lecture, May 27, 2016

Language Theory and Compilers

Martin J. Dürst


© 2005-16 Martin J. Dürst 青山学院大学

Today's Schedule


Remainders from Last Lecture


Summary of Last Lecture


General Top-Down Parsing


Main Points of Backtracking

Backtracking may be very slow, but this can be improved:


Recursive Descent Parsing


Recursive Descent Parsing: Simple Hand-Written Parser

Program files: scanner.h, scanner.c, parser1.c

How to complie: gcc scanner.c parser.c && ./a


Details of Recursive Descent Parsing: Lexical Analysis

(see scanner.c)


Details of Recursive Descent Parsing: Parsing

(see parser1.c)


Details of Recursive Descent Parsing: Non-Terminal Symbols


How to Deal with Left Recursion

Example of left recursion:

E → E '-' integer | integer

Wrong solution (change of associativity):

E → integer '-' E | integer

Correct solution:

E → integer EE

EE → '-' integer EE | ε

In (E)BNF:

E → integer {'-' integer}


Differences between Grammars and Regular Expressions


Regular Expression:

A simple regular expression corresponds to a single rewriting rule in an (BNF,...) grammar




Deadline: June 2, 2016 (Thursday), 19:00

Where to submit: Box in front of room O-529 (building O, 5th floor)

Format: A4 single page (using both sides is okay; NO cover page, staple in top left corner if more than one page is necessary), printout (no wrapping lines), name (kanji and kana) and student number in comment at the top right

  1. Expand the top-down parser of parser1.c to correctly deal with the four basic arithmetic operations.
    (scanner.h/c do not change, so no need to submit them)
  2. (bonus problem) Add more operations to the top-down parser.
    (If you solve this problem, also submit the scanner.h/c files, but only one parser.c file for both problems.)
  3. Bring your notebook computer to the next lecture. Check again that flex, bison, make, and gcc are installed.



recursive descent parsing
ambiguous grammar
right associative
left recursion