Professional Documents
Culture Documents
1 Basic Concepts
1.1 Derivations
A derivation is a sequence of mappings between the START symbol and the input
tokens. At each step, one nonterminal is replaced by the RHS of a production rule which
has the nonterminal on it LHS.
The Leftmost Derivation is obtained by starting with the START symbol, and always
expanding the leftmost nonterminal first. E.g., (the nonterminal in red is the one about to
be expanded):
E
E*E
(E)*E
(E+E)*E
( id + E ) * E
( id + id ) * E
( id + id ) * id
The RightMost Derivation is obtained by starting with the START symbol, and always
expanding the rightmost nonterminal first:
E
E*E
E * id
( E ) * id
( E + E ) * id
( E + id ) * id
( id + id ) * id
LR parsing produces a rightmost derivation, but since it works from the bottom up, it
reduces sequence of tokens on the left side first. But when read from the top, the
derivation sequence appears to expand rightmost nonterminals first.
2 LR(0) Parsing
For this discussion, assume the following grammar:
1. E :- T
2. E :- E + T
3. T :- id
4. T :- ( E )
S0
E’:- . E $
E :- . T
E :- . E + T
T :- . id
T :- . ( E )
E S2
S0
E’:- . E $
T
E :- . T S3
E :- . E + T id
T :- . id
( S4
T :- . ( E )
e. Where the closure includes an item with dot at end, place a double circle around
the state (some actions from this state cause a REDUCE)
S1
E’:- E . $
E :- E . + T
E S2
S0
E’:- . E $ E :- T .
T S3
E :- . T
E :- . E + T id T :- id .
T :- . id
( S4
T :- . ( E )
T :- ( . E )
E :- . T
E :- . E + T
T :- . id
T :- . ( E )