Title: Compiler Design Chapter 3
1Compiler Design - Chapter 3
Parsing - LR Parsing
2LR Parsing
- Weakness of LL(k) parsing techniques
- must predict which production to use
- having seen only the first k tokens on RHS
- LR(k) parsing more powerful
- postpone decision until it has seen input
tokens for entire RHS of production - LR(k) left to right parse, rightmost-derivation,
k-token lookahead.
3LR Parsing
S?S
(Augmented with S?S)
Program to parse
4LR Parser
- LR Parser
- Stack
- Input
- First k tokens lookahead
- Based on contents of stack and lookahead, parser
actions - Shift Move first input token to top of stack
- Reduce
- Choose grammar rule X? A B C
- Pop C, B, A from the top of the stack
- Push X onto the stack
5LR Parser
- Initially
- Stack is empty
- Parser is at beginning of input
- Accepting
- Shifting EOF marker - causes parser to stop
successfully
6S?S
Rightmost derivation
7LR Parsing Engine
- Apply a DFA (to the stack) to know when to shift
/ reduce - Transition table
S?S
Actions
8LR Parsing Engine
S?S
9Parsing Algorithm
10LR(0) Parser Generation
- LR(k) parser uses contents of the stack and the
next k tokens to decide which action to take - In practice k gt 1 not used
- huge tables
- most reasonable programming languages can be
described by LR(1) grammars
- LR(0) grammars
- use only stack
- shift/reduce without lookahead
- These grammars are too weak to be useful good
introduction to LR(1) parser construction.
11LR(0) Parser Generation
- Initially
- Stack is empty
- Input complete S sentence followed by
Current position of parser
12LR(0) Parser Generation
In this state, if input begins with S, it can
begin with any RHS of S-production
state
LR(0) items
13LR(0) Shift Actions
From state 1 shift on x
From state 1 shift on (
Add L-productions
Add S-productions
14LR(0) Goto Actions
- From state 1 parsing past a string of tokens
derived from S non-terminal - After x or ( is shifted followed (eventually) by
a reduction of S-production - All RHS symbols of production will be popped
- Parser will execute goto action for S in state 1
Dot moved past S
15LR(0) Reduce Actions
the dot is at the end of an item RHS on top of
stack parsercan now reduce
16LR(0) Basic Operations
I set of items, X grammar symbol
Closure adds more items to a set of items when
there is a dot to the left of a nonterminal
Goto moves the dot past the symbol X in all items
17LR(0) Parser Construction Algorithm
T set of states seen so far E set of
(shift/goto) edges found so far
For - do not compute Goto(I, ) make accept
action
18LR(0) States
19LR(0) Reduce Actions
set R of LR(0) reduce actions
20LR(0) Parsing Table
- shift J at position (I,X) For each edge
I?J, X terminal - goto J at position (I,X) For each edge I?J,
X non terminal - accept at (I,) for each state I containing
S?S. - reduce n at (I,Y) for every token Y, for I
containing A ? ?. (production n with . at end)
X
X
21Another LR(0) Parsing Table
- Conflict !
- Not LR(0) grammar
- Cannot be parsed by LR(0) parser
- Need a more powerful parsing algorithm
22SLR
- SLR Simple LR
- Construct better-than-LR(0) parser
- SLR Parser Construction almost identical to
LR(0) except FOLLOW set determines where to
put reduce actions - Algorithm to put reduce actions in SLR table
Reduction Rule
State
Look-ahead symbol
23SLR Parsing Table
Follow(E)
Follow(T) ,
Follow(T) ,
24LR(1) Parsing
- LR(1) parsing even more powerful than SLR
parsing - Works similar to LR(0) parsing, but an item is
more sophisticated - LR(1) item consists of
- a grammar production
- a RHS position (represented by the dot)
- look ahead symbol
- Item (A?a.ß, x) indicates that the sequence a is
on top of the stack and at the head of the
input is a string derivable from ßx -
- LR(1) state set of LR(1) items
25LR(1) Closure and Goto Operations
Incorporates Lookahead
26LR(1) Start State and Reduce Actions
Start State Closure of item
EOF Marker will never be shifted
In state I on lookahead z, the parser will reduce
by rule A?a
27LR(1) Grammar example
Exercise 3.9 Diagram the LR(0) states for Grammar
3.26, build the SLR parsing table and identify
the conflicts
This grammar is not a SLR grammar, but a LR(1)
grammar
28LR(1) States
Start State
FIRST(E)
29LR(1) Grammar example
LR(1) Parsing Table
30LALR(1) Grammars
- LR(1) parsing tables can be large many states
- Smaller parsing tables by merging states with
identical items but different lookaheads - LALR(1) parser lookahead LR(1)
- LALR(1) parsing tables can have reduce-reduce
conflicts, where the LR(1) table has none - In practice difference matters little
- LALR(1) tables requires less memory fewer
states
31LALR(1) Grammar example
LALR(1) Parsing Table
7
8
6
8
6
7
10
10
32Hierarchy of Grammar Classes
- A grammar is LALR(1) if LALR(1) table contains
- no (reduce-reduce) conflicts
-
- All SLR grammars are LALR(1)
- Any reasonable programming language has a
LALR(1) grammar - LALR(1) standard for programming languages and
automatic parser generators
33LR Parsing of Ambiguous Grammars
Many programming languages have grammar rules
such as
DANGLING ELSE
Will allow programs such as
Can be understood as
Most languages - else must match the most recent
then ? interpretation (1) is correct
34Shift-Reduce Conflict
Two interpretations
Reduce
Shift
35Solution 1 Use of Auxiliary Non-Terminals
Can introduce Auxiliary non-terminals
36Solution 2 Leaving the grammar unchanged
Tolerate shift-reduce conflict choose shift
rather reduce (prefer interpretation 1)
- Shift-Reduce or Reduce-Reduce conflicts are
usually the symptoms of an ill-specified grammar - Do not fiddle with parsing table !
- Eliminate ambiguities!