A .lr1 file has two components:
- a context-free grammar
- the states, transitions, and reduce actions of the LR(1) machine
Context-free Grammar
Same as the CFG File Format. The
grammar must be augmented; that is, the start symbol must occur as the
LHS of exactly one rule that begins and ends with a terminal.
LR(1) Machine
The parser representation has three components:
- An integer s -- the number of states in the LR(1) parser. States are numbered
0 to s-1. State 0 is the start state.
- An integer t -- the number of transitions or reduce actions in the LR(1)
parser.
- t lines, each containing one of:
- state terminal reduce rule (Rules (productions) are numbered from
0 in the order they appear in the CFG.)
-
state1 symbol shift state2 (where symbol is either a terminal or a non-terminal)
Example .lr1 File
sample.lr1 (see bubble diagram)
6
BOF
EOF
id
-
(
)
3
S
expr
term
S
5
S BOF expr EOF
expr term
expr expr - term
term id
term ( expr )
11
28
8 EOF reduce 2
9 - reduce 4
7 - shift 1
1 id shift 2
6 ( shift 3
6 term shift 4
10 EOF shift 5
2 - reduce 3
4 ) reduce 1
3 id shift 2
4 EOF reduce 1
2 ) reduce 3
0 BOF shift 6
8 - reduce 2
2 EOF reduce 3
3 expr shift 7
9 ) reduce 4
9 EOF reduce 4
4 - reduce 1
1 term shift 8
3 term shift 4
3 ( shift 3
10 - shift 1
6 id shift 2
8 ) reduce 2
1 ( shift 3
7 ) shift 9
6 expr shift 10