Compiler Design Chapter 3 - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Compiler Design Chapter 3

Description:

Any reasonable programming language has a LALR(1) grammar ... Many programming languages have grammar rules such as: Will allow programs such as: ... – PowerPoint PPT presentation

Number of Views:160
Avg rating:3.0/5.0
Slides: 37
Provided by: jian9
Category:

less

Transcript and Presenter's Notes

Title: Compiler Design Chapter 3


1
Compiler Design - Chapter 3
Parsing - LR Parsing
2
LR 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.

3
LR Parsing
S?S
(Augmented with S?S)
Program to parse
4
LR 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

5
LR Parser
  • Initially
  • Stack is empty
  • Parser is at beginning of input
  • Accepting
  • Shifting EOF marker - causes parser to stop
    successfully

6
S?S
Rightmost derivation
7
LR Parsing Engine
  • Apply a DFA (to the stack) to know when to shift
    / reduce
  • Transition table

S?S
Actions
8
LR Parsing Engine
S?S
9
Parsing Algorithm
10
LR(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.

11
LR(0) Parser Generation
  • Initially
  • Stack is empty
  • Input complete S sentence followed by

Current position of parser
12
LR(0) Parser Generation
In this state, if input begins with S, it can
begin with any RHS of S-production
state
LR(0) items
13
LR(0) Shift Actions
From state 1 shift on x
From state 1 shift on (
Add L-productions
Add S-productions
14
LR(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
15
LR(0) Reduce Actions
the dot is at the end of an item RHS on top of
stack parsercan now reduce
16
LR(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
17
LR(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
18
LR(0) States
19
LR(0) Reduce Actions
set R of LR(0) reduce actions
20
LR(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
21
Another LR(0) Parsing Table
  • Conflict !
  • Not LR(0) grammar
  • Cannot be parsed by LR(0) parser
  • Need a more powerful parsing algorithm

22
SLR
  • 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
23
SLR Parsing Table
Follow(E)
Follow(T) ,
Follow(T) ,
  • No Conflicts
  • SLR grammar

24
LR(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

25
LR(1) Closure and Goto Operations
Incorporates Lookahead
26
LR(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
27
LR(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
28
LR(1) States
Start State
FIRST(E)
29
LR(1) Grammar example
LR(1) Parsing Table
30
LALR(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

31
LALR(1) Grammar example
LALR(1) Parsing Table
7
8
6
8
6
7
10
10
32
Hierarchy 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

33
LR 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
34
Shift-Reduce Conflict
Two interpretations
Reduce
Shift
35
Solution 1 Use of Auxiliary Non-Terminals
Can introduce Auxiliary non-terminals
36
Solution 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!
Write a Comment
User Comments (0)
About PowerShow.com