COMS W4115
Programming Languages and Translators
Lecture 9: Top-Down Parsing
October 7, 2009
Lecture Outline
- Examples of context-free grammars
- Top-down parsing
- Transformations on grammars
- Reading and exercises
1. Review
- Role of the parser
- Context-free grammars
- Derivations and parse trees
- Ambiguity
- Yacc: a language for specifying translators
2. Examples of Context-Free Grammars
3. Top-down Parsing
- Top-down parsing consists of constructing a parse tree
for an input string starting from the root and creating
the nodes of the parse tree in preorder.
- Equivalently, top-down parsing consists of finding a
leftmost derivation for the input string.
- Consider grammar G:
S → + S S | * S S | a
Leftmost derivation for + a * a a:
S ⇒ + S S
⇒ + a S
⇒ + a * S S
⇒ + a * a S
⇒ + a * a a
Recursive-descent parsing
- Recursive-descent parsing is a top-down method of syntax
analysis in which a set of recursive procedures is used
to process the input string.
- One procedure is associated with each nonterminal of
the grammar. See Fig. 4.13, p. 219.
- The sequence of successful procedure calls defines the parse tree.
Nonrecursive predictive parsing
- A nonrecursive predictive parser uses an explicit stack.
- See Fig. 4.19, p. 227, for a model of table-driven predictive
parser.
- Parsing table for G:
Input Symbol
Nonterminal a + * $
S S → a S → +SS S → *SS
Moves made by predictive parser on input (()()).
Stack Input Output
S$ +a*aa$
+SS$ +a*aa$ S → +SS
SS$ a*aa$
aS$ a*aa$ S → a
S$ *aa$
*SS$ *aa$ S → *SS
SS$ aa$
aS$ aa$ S → a
S$ a$
a$ a$ S → a
$ $
4. Transformations on Grammars
- Two common language-preserving transformations are often applied to
grammars to try to make them parsable by top-down methods.
These are elimination of left recursion and left factoring.
- Eliminating left recursion:
expr → expr + term
| term
by
expr → term expr'
expr' → + term expr'
| ε
Left factoring:
stmt → if ( expr ) stmt else stmt
| if (expr) stmt
| other
by
stmt → if ( expr ) stmt stmt'
| other
stmt' → else stmt
| ε
5. Reading and Exercises
- Try doing exercises 4.2.1, 4.2.2, 4.2.3, 4.3.1.
- Read Sections 4.3, 4.4.
aho@cs.columbia.edu