0% found this document useful (0 votes)
17 views15 pages

CC lec 15b

The document discusses the LR parsing algorithm, specifically the LR(0) parser, which makes shift-reduce decisions without lookahead and has limitations due to conflicts in certain grammars. It explains the structure of the parsing table, including the ACTION and GOTO functions, and provides examples of constructing parsing tables and tracing input strings. The document concludes by highlighting the drawbacks of LR(0) parsing, emphasizing its practical limitations and lack of use in complex scenarios.

Uploaded by

waseem khosa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views15 pages

CC lec 15b

The document discusses the LR parsing algorithm, specifically the LR(0) parser, which makes shift-reduce decisions without lookahead and has limitations due to conflicts in certain grammars. It explains the structure of the parsing table, including the ACTION and GOTO functions, and provides examples of constructing parsing tables and tracing input strings. The document concludes by highlighting the drawbacks of LR(0) parsing, emphasizing its practical limitations and lack of use in complex scenarios.

Uploaded by

waseem khosa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

COMPILER

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

I/P STRING $0334 b$ Reduce A ->b,


Pop 4 GOTO
[3,A]=6
 Input String: aabb
$0336 b$ Reduce A -
>Aa, Pop 6,3
GOTO [3,A]=6
$036 b$ Reduce A -
>Aa, Pop 6,3
GOTO [0,A]=2
$02 b$ Shift 4
$024 $ Reduce A ->b,
Pop 4 GOTO
[2,A]=5
$025 $ Reduce S -
>AA, Pop 2,5
GOTO [0,S]=1
$01 $ ACCEPT
EXAMPLE 2 $
S’ -> .S ACCEPT!
 Given Grammar: S
S ->
1. S -> aSb S’ -> .S S S ->
a a.Sb
S- a.Sb
2. S -> b S-
> .aSb
> .aSb
 Augmented Grammar: G’ S -> .b b
S -> .b a
 S’ -> .S b b
S ->
 S -> .aSb S’ -> .b
aSb.
 S -> .b
EXAMPLE: PARSING TABLE
States Action Goto
a b $ S
0 S2 S3 1
1 ACCEPT
2 S2 S3 4
3 R2 R2 R2
4 S5
5 R1 R1 R1
TRACING OF I/P STRING
Stack Input Action
 Input String: aabbb
$0 aabbb$ Shift 2
$02 abbb$ Shift 2
$022 bbb$ Shift 3
$0223 bb$ Reduce S ->b, Pop
3 GOTO [2,S]=4
$0224 bb$ Shift 5
$02245 b$ Reduce S ->aSb,
Pop 5,4,2
GOTO[2,S]=4
$024 b$ Shift 5
$0245 $ Reduce S ->aSb,
Pop 5,4,2
GOTO[0,S]=1
$01 $ ACCEPT
EXAMPLE 3 R/R
SET OF ITEMS WITH “EPSILON RULES”:
Conflict

 Consider the grammar: No Conflict

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

You might also like