Discussion Session 2¶
Grammar¶
- Nonterminal
- Terminal
- Production Rule (Left Hand Side, Right Hand Side)
Parsing¶
Parsing is the procedure of transforming a list of tokens into a tree with tokens as leaves.
Note
This process is also called derivation.
Token stream t1, t2, t3, t4, t5, t6, ...... and Parsing Tree (Abstract Syntax)
| Color | Meaning |
|---|---|
| Green | Terminal |
| Blue | Nonterminal |
Example
The abstract syntax for 1 - 2 - 3 - 4 goes as follows:
A good grammar has no ambiguity. Only one tree can be constructed from the token stream.
A good grammar should match human expectation.
Anatomy of LL(k)¶
L: Left-to-right
Examine the input (token stream) left-to-right in one parse.
L: Leftmost-derivation
Create / expand the left most sub-tree first.
Note
R: Rightmost-derivation
demo
Try parsing “1 + 2 - 3 * 4 + 5” with the following grammar.
start ::=exp// Start exp ::=exp+term// Add exp ::=exp-term// Minus exp ::=term// Term term ::=term* NUM // Mul term ::=term/ NUM // Div term ::= NUM // Num term ::= (exp) // Group
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
k: Lookahead
Inspect the first k tokens before making decision.
Eliminating Left Recursion¶
For productions like this:
It will be turned into
And you can verify that the resulting language is the same.
Warning
This is actually eliminating direct left recursions, and turning them into right recursions. There are methods to eliminate all recursions, direct or indirect, but it is more complicated, and needs some restrictions on the input grammar.
Coding Demo¶
Left-factoring¶
For productions like this:
We turn them into
Example
start ::=exp// Start exp ::=exp+term// Add exp ::=exp-term// Minus exp ::=term// Term term ::= ID // Id term ::= NUM // Num term ::= (exp) // Group
is turned into
start ::=expexp ::=expterm1term1 ::= +termterm1 ::= -termexp ::=term
Do left recursion elimination.
start ::=expexp ::=termexp1exp1 ::=term1exp1exp1 ::= epsilon term1 ::= +termterm1 ::= -term