CC lec 15b
CC lec 15b
CONSTRUCTIO
N
Lecture 15
Part - b
LR PARSING ALGORITHM
Model:
LR(0) PARSER
Looking only at the stack.
Making shift-reduce decision without any lookahead.
Too weak but good introduction.
STRUCTURE OF LR PARSING
TABLE
The parsing table consists of two parts: a parsing-action function ACTION and
a goto function GOTO.
1. The ACTION function takes as arguments a state i a terminal a (or $, the
input end marker). The value of ACTION[i, a] can have one of four forms:
a. Shift j , where j is a state. The action taken by the parser effectively
shifts input a to the stack but uses state j to represent a .
b. Reduce A -> . The action of the parser effectively reduces on the top of
the stack to head A.
c. Accept. The parser accepts the input and finishes parsing.
d. Error. The parser discovers an error in its input and takes some
corrective action.
2. We extend the GOTO function, defined on sets of items, to states: if GOTO
[Ii , A] = Ij , then GOTO also maps a state i and a nonterminal A to state j .
NOTE
The expression grammar is not LR(0), due to shift/reduce conflicts in states 2 & 9 of the LR(0)
automaton.
LET'S TAKE ANOTHER
EXAMPLE
Given Grammar:
S → AA
A → aA | b
Add Augment Production and insert '•' symbol at the first position for every
production in G
S` → •S
S → •AA
A → •aA
A → •b
CONSTRUCTION OF
PARSING TABLE
If a state is going to some other state on a terminal it correspond to the
ACTION MOVE.
If a state going to some other state on a non-terminal it correspond to the
GOTO MOVE.
If the state contain the final item in the row than we write the reduce node
completely.
Stack Input Action
$0 aabb$ Shift 3
TRACING OF $03
$033
abb$
bb$
Shift 3
Shift 4
No Conflict
DRAWBACKS OF LR(0)
PARSING
LR(0) is the Simplest Technique in the LR family.
LR(0) Parsers are too weak to be of practical use.
LR(0) accepts only small class of LR(0) grammar because conflicts occurs.
The Fundamental Limitation of LR(0) is that no look ahead tokens are used.
LR(0) Parsing is the weakest, and it is not used much in practice because of
its limitations.
PRACTICE EXAMPLE
COMPLETE THE FOLLOWING EXAMPLE FOR LR(0):
Consider the following grammar:
Determine if the grammar contain any conflicts. If not parse for the
following input string id+id*id.
Items: E’ ->E
EXAMPLE
LR(0) Automaton