Download presentation
Presentation is loading. Please wait.
1
Compiler Construction
Chapter 5 Compiler Construction Dr K. V. N. Sunitha
2
Task: detect the handle and reduce the handle.
Bottom Up Parser/SR Parser Task: detect the handle and reduce the handle. Compiler Construction Dr K. V. N. Sunitha
3
Compiler Construction
LR Parsing Program Let ‘X’ be the state on top of the stack and ‘a’ be the symbol pointed by ‘ip’. Repeat for ever if action[X, a]= Si, then (Shift action) Push ‘a’ then state I onto stack. Advance the input pointer to the next symbol. Compiler Construction Dr K. V. N. Sunitha
4
Compiler Construction
if action[X,a]= ri, then (Reduce action) { Take ri i.e ith production from the grammar. Let ri is A → β . Pop 2 * |β| symbols from stack and replace them by nonterminal A. If Xi is the state below the nonterminal A, then push goto[Xi,A] onto the stack. Output the production A → β .} Now continue parsing. if action[X,a]= accept, then (Successful completion action) Compiler Construction Dr K. V. N. Sunitha
5
Compiler Construction
Example w = aabb (1) S → AA (2) A → aA (3) A → b State Action Goto a b $ S A S3 S4 1 2 acc 5 3 r 4 6 4 r 3 8 r 1 r 2 Compiler Construction Dr K. V. N. Sunitha
6
Compiler Construction
LR Grammars LR grammar A grammar is said to be an LR grammar if we can construct a parsing table for it. LR(k) grammar lookahead of up to k input symbols LR(0), SLR(1), LR(1), and LALR(1) grammars Compiler Construction Dr K. V. N. Sunitha
7
Types of Bottom Up Parsers
Compiler Construction Dr K. V. N. Sunitha
8
Construction of LR Parsing Table
Given grammar, take augmented grammar. Create canonical collection of LR items. Draw DFA and prepare table. LR(0) SLR(1) LALR(1) CLR(1) LR(0) ITEMS LR(1) ITEMS Compiler Construction Dr K. V. N. Sunitha
9
Compiler Construction
Augment the grammar with production: Add S’ S to G where S is start symbol and S’ not in V. This rule helps the parser to understand when to stop parsing and announce the successful completion of the process. Compiler Construction Dr K. V. N. Sunitha
10
Compiler Construction
LR(0) Item/Item A production with a dot at some position on the RHS is known as Item A → XYZ produces 4 items. A →.XYZ A → X . YZ A → XY . Z A → XY Z. . Indicates how far right hand side of production is seen by parser. Compiler Construction Dr K. V. N. Sunitha
11
Compiler Construction
Closure (I) Initially add every item from input to output. If A → α•Bβ is in I where B is nonterminal & B → ૪ is the rule for B then add B → •૪ to Closure (I). Repeat this for every newly added item. Compiler Construction Dr K. V. N. Sunitha
12
Closure of a Set of Items I Example
E' →E E → E + T | T T → T * F | F F → id |(E) closure({E' → .E}) = ? Compiler Construction Dr K. V. N. Sunitha
13
Compiler Construction
The Goto Operation Goto function defines the transition in the set of items I on seeing the grammar symbol X. goto(I,X) = closure({A → aX.b |A → a.Xb is in I}) The goto Operation Example I = {E' → .E , E → E . + T} goto(I,+) = ? Compiler Construction Dr K. V. N. Sunitha
14
Canonical LR(0) Collection of Set of Items C={I0, I1, I2…}
The initial item in C is I0 = closure (augmented production with dot at the beginning) for Example in the above grammar, I0= closure(E' → •E ) For each Ii in C and each grammar symbol X in G Repeat While goto (I,X) is not empty and not in C Add goto (I,X) to C Until no more set of items can be added to C. Compiler Construction Dr K. V. N. Sunitha
15
Compiler Construction
LR(0) Parsers No lookahead How to produce table? Consider grammar: S’ S S (L) S x L S L L , S Compiler Construction Dr K. V. N. Sunitha
16
Compiler Construction
LR(0) Parsers Initial State (S1) S’ . S S . x S . (L) If we shift x , we get state (S2) S x. From S1, if we shift (, we get S3 S ( . L) L . L,S L . S Compiler Construction Dr K. V. N. Sunitha
17
Compiler Construction
S’ . S $ S . x S . (L) x S2: S x . S3: S ( . L) L . L,S L . S S1: S’ S . ( S5: L S . S S4: S (L . ) L L . , S L ) S6: S (L) . , S7: L L , . S S. (L ) S .x S8: L L , S . Compiler Construction Dr K. V. N. Sunitha
18
LR(0) Parsing – Table Construction
X For each edge I J if X is a terminal, put shift entry as SJ at action[I,X] if X is a non-terminal, put goto [I,X] as J For each state I containing S’ S. put accept at state I. For each state containing A γ. Put reduce A γ at (I, Z) for every token Z. Compiler Construction Dr K. V. N. Sunitha
19
LR(0) Parsing Table - Example
, $ S L s3 s2 1 Accepting state 2 r2 3 5 4 s6 s7 r3 6 r1 7 8 r4 S (L) S x L S L L , S Compiler Construction Dr K. V. N. Sunitha
20
Compiler Construction
Parsing Conflicts A .a r2:S c. a I1 A x. S c. I0 I0 SR conflict RR conflict LR(0) a b $ S1/r2 r2 LR(0) a b $ r1/r2 A conflict produces multiple entries in table. Compiler Construction Dr K. V. N. Sunitha
21
LR(0) Parsing - Limitation
Consider the grammar: S E E T + E E T T x Compiler Construction Dr K. V. N. Sunitha
22
Compiler Construction
S E . S0: S . E E . T + E E . T T .x T S4: E T + . E E . T + E E . T T .x S2: E T . + E E T . + S3: T x. x T E S5: E T + E . In state S3, should we shift or do we reduce? We need to look ahead (or do we?) Compiler Construction Dr K. V. N. Sunitha
23
Shift-Reduce Conflict
x + $ E T s5 1 2 acc r2 S4,r2 Mutliple entries denote Conflicting actions: Shift vs. Reduce (Other possibilities: Reduce vs. Reduce.) Compiler Construction Dr K. V. N. Sunitha
24
Compiler Construction
SLR Parser Generation Simple LR – slightly better than LR(0) Change the rule adding reductions to the table. For each state containing A γ. Put reduce A γ at (I, Z) for each token Z in FOLLOW(A) Reconsider the previous example: S E$ E T + E E T T x Compiler Construction Dr K. V. N. Sunitha
25
Compiler Construction
S . E E . T + E E . T T .x E S1: S E . S2: E T . + E E T . T S5: E T+ E. S3: T x. x S4: E T + . E + S3 Compiler Construction Dr K. V. N. Sunitha
26
Compiler Construction
SLR Parsing Table x + $ E T s3 1 2 acc s4 r2 3 r3 4 5 r1 S3: E T . + E E T . Conflict resolved in favor of shift Compiler Construction Dr K. V. N. Sunitha
27
Compiler Construction
LR(1) Items Algorithm for constructing parsing table similar to that for LR(0). But LR(1) items include a look-ahead symbol as well: Compiler Construction Dr K. V. N. Sunitha
28
Compiler Construction
LR(1) Parsing Tables LR(1) item – LR(0) item + look ahead terminal [A→a. ,a] – reduce a to A only if the next symbol is a Compiler Construction Dr K. V. N. Sunitha
29
LR(1) Parsing Table Construction
Closure(I) : j=I; repeat for any item (A α . X β, z) in I for any production X γ add { (X . γ, w) } to J until no more items can be added to J. for any w in FIRST(βz) Goto(I, X) = J := {} for any item (A α . X β , z) in I add (A α X . β, z) to J return Closure(J) Compiler Construction Dr K. V. N. Sunitha
30
Compiler Construction
Example - S → AA A → aA A → b Construct LR(1) and LALR(1) parsers Compiler Construction Dr K. V. N. Sunitha
31
Compiler Construction
Dr K. V. N. Sunitha
32
LR(1) Parsing – Table Construction
For each edge I J if X is a terminal, put shift entry as SJ at action[I,X] if X is a non-terminal, put goto [I,X] as J For each state I containing S’ S., $ put accept at state I on $. For each state containing A γ. ,Z Put reduce A γ at (I, Z) for token Z. Compiler Construction Dr K. V. N. Sunitha
33
Compiler Construction
LR(1) Table state a b $ S A s3 s4 1 2 acc s6 s7 5 3 8 4 r3 r1 6 9 7 r2 Compiler Construction Dr K. V. N. Sunitha
34
Compiler Construction
LALR(1) Tables If two (or more) states in an LR(1) PDA are identical except for look ahead, then the states can be merged This leads to a compressed table Efficient look ahead may become important for large grammars. But this may introduce conflicts not present in the LR(1) tables Compiler Construction Dr K. V. N. Sunitha
35
Compiler Construction
LALR(1) Table state a b $ S A s36 s47 1 2 acc 5 36 89 47 r3 r1 r2 For a given grammar, no.of states of SLR(1) and LALR(1) is always equal. Compiler Construction Dr K. V. N. Sunitha
36
Compiler Construction
Parsing Strengths LL(0) < LL(1) < LL(2) < … LR(0) < LR(1) < LR(2) <… LR(0) < SLR < LALR(1) < LR(1) LL(k) < LR(k) for any k LL(1) ≠ LR(0) An ambiguous grammar is not LL(k) nor LR(k) for any k. Compiler Construction Dr K. V. N. Sunitha
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.