Unit-3 Syntax Analysis
Unit-3 Syntax Analysis
Token Parse
Source Lexical Parse tree Rest of front IR
Parser
tree end
program analyzer
Symbol table
Syntax Analysis
• Syntax of a language refers to the structure of valid programs/
statements of that language.
• Specified using certain rules (known as production rules)
• Collections of such production rules is known as grammar
• Parsing or syntax analysis is a process of determining if a string of
tokens can be generated by the grammar
• Parser/syntax analyzer gets string of tokens from lexical analyzer and
verifies if that string of tokens is a valid sequence i.e. whether its
structures is syntactically correct.
Syntax Analysis
• Other Tasks of parser:
• Report syntactic errors.
• Recovery from such errors so as to continue the execution process
• Output of Parser:
• A representation of parse tree generated by using the stream of tokens
provided by the Lexical Analyzer.
Language
• An alphabet of a language is a set of symbols.
• Examples : {0,1} for a binary number system(language) = {0,1,100,101,...}
• {a,b,c} for language={a,b,c, ac,abcc..}
• {if,(,),else ...} for a if statements={if(a==1)goto10, if--}
• A string over an alphabet:
• is a sequence of zero or more symbols from the alphabet.
• Examples : 0,1,10,00,11,111,0101 ... strings for a alphabet {0,1}
• Null string is a string which does not have any symbol of alphabet.
Language
• Language: Is a subset of all the strings over a given alphabet.
S S
→S-S Parse tree represents the
structure of derivation S - S
→S*S-S
→a*S-S S * S a
→a*a-S
→a*a-a a a
Leftmost Derivation Parse tree
Rightmost derivation
• A derivation of a string W in a grammar G is a right most derivation if at
every step the right most non terminal is replaced.
• It is all called canonical derivation.
• Grammar: S→S+S | S-S | S*S | S/S | a Output string: a*a-a
Rightmost derivation
• A derivation of a string W in a grammar G is a right most derivation if at
every step the right most non terminal is replaced.
• It is all called canonical derivation.
• Grammar: S→S+S | S-S | S*S | S/S | a Output string: a*a-a
S
S
→S*S
S * S
→S*S-S
→S*S-a a S S
-
→S*a-a
→a*a-a a a
Rightmost Derivation Parse Tree
Question-1: Derivation
1. Perform leftmost derivation and draw parse tree.
• S→A1B
• A→0A | 𝜖
• B→0B | 1B | 𝜖
• Output string: 1001
Question-2: Derivation
• Perform leftmost derivation and draw parse tree. S→0S1 | 01 Output
string: 000111
Question-3: Derivation
• Perform rightmost derivation and draw parse tree.
• E→E+E | E*E | id | (E) | -E Output string: id + id * id
Parse Tree
• A Parse tree is pictorial depiction of how a start symbol of a grammar
derives a string in the language.
• Example:
• A->PQR
• P->a
• Q->b
• R->c|d
• Root is always labelled with the start symbol.
• Each leaf is labelled with a terminal (tokens)
• Each interior node is labelled by a non terminal.
Parse tree
• Yield of Parse tree: The Leaves of a parse tree when read from left to right
form the yield.
• Language defined by a grammar is set of all strings that are generated by
some parse tree formed by that grammar (starting symbol of grammar).
• General Types of Parser:
• Universal Parser:
• It can parse any kind of grammar
• Not very Efficient
• CYK Algorithm, Earley’s Algorithm
• Top down Parser:
• Builds the parse tree from root (top) to leaves (bottom)
• Bottom up Parser:
• Builds the parse tree from leaves (bottom) to root(top)
Ambiguous Grammar
• Ambiguity
• Ambiguity, is a word, phrase, or statement which contains more than one
meaning.
Chip
Ambiguous Grammar
• Ambiguity
• Ambiguity, is a word, phrase, or statement which contains more than one
meaning.
A long thin piece of potato
Chip
• N2 → α a
• Disadvantages:
• The procedure of Left Recursion only eliminates direct Left Recursion but not
indirect Left Recursion.
Left recursion
A grammar is said to be left recursive if it has a non terminal A such that there is a derivation
A→ Aα for some string α.
Example of Left Recursion
• 𝐸 → 𝐸 + 𝑇|𝑇 • 𝐴 → 𝐴𝛼|𝛽 ⇒
• 𝐴 → 𝛽𝐴′
• T→ 𝑇 ∗ 𝐹|𝐹 • 𝐴′ → 𝛼𝐴′ | ∈
• 𝐹 → (𝐸)|𝑖𝑑
• Final Result:
• 𝐸 → 𝑇𝐸′
• 𝐸 → +𝑇𝐸 ′ |𝜖
• T→ 𝐹𝑇 ′
• T→∗ 𝐹𝑇 ′ | ∈
• 𝐹 → (𝐸)|𝑖𝑑
Example of Left Recursion
• 𝐴 → 𝐴𝛼|𝛽 ⇒
• S→ 𝑆0𝑆1𝑆|01 • 𝐴 → 𝛽𝐴′
• 𝐴′ → 𝛼𝐴′ | ∈
• Final Result:
• S→ 01𝑆 ′
• S → 0𝑆1𝑆𝑆 ′ | ∈
Example of Left Recursion
• 𝐴 → 𝐴𝛼|𝛽 ⇒
• L→ 𝐿, 𝑆|𝑆 • 𝐴 → 𝛽𝐴′
• 𝐴′ → 𝛼𝐴′ | ∈
• Final Result:
• 𝐿 → 𝑆𝐿′
• L′ →, 𝑆𝐿′| ∈
Example of Left Recursion
• 𝐴 → 𝐴𝛼|𝛽 ⇒
• 𝑆 → 𝑆𝑋 𝑆𝑆𝑏 𝑋𝑆|𝑎 • 𝐴 → 𝛽𝐴′
• 𝐴′ → 𝛼𝐴′ | ∈
• Final Result:
• 𝑆 → 𝑋𝑆𝑆 ′ |𝑎𝑆′
• S ′ → 𝑋𝑆 ′ |𝑆𝑏𝑆 ′ | ∈
Example of Left Recursion
• 𝐴 → 𝐴𝛼|𝛽 ⇒
• 𝐴 → 𝐴𝐴|𝐴𝑏 • 𝐴 → 𝛽𝐴′
• Final Result: • 𝐴′ → 𝛼𝐴′ | ∈
• 𝐴′ → 𝐴𝐴′ |𝑏𝐴′ | ∈
Example: Left Recursion Elimination
Example: Left Recursion Elimination
Example: Left Recursion Elimination
Example: Left Recursion Elimination
Example: Left Recursion Elimination
Example: Left Recursion Elimination
Questions:
Parsing
• Parsing is a technique that takes input string and produces output
either a parse tree if string is valid sentence of grammar, or an error
message indicating that string is not a valid.
Classification of parsing methods
Parsing
Operator precedence
a b Backtrack
• Input string: cad
Backtracking
• In backtracking, expansion of nonterminal symbol we choose one
alternative and if any mismatch occurs then we try another
alternative.
• Grammar:
S S S
• S→ cAd
• A→ ab | a c A d c A d c A d
Make prediction Make prediction
• Elimination:
• Stmt->if expr then stmt A
• A->else stmt|∈
Questions: Left Factoring
• S->iEtS|iEtSeS|a
• E->b
Questions: Left Factoring
• S->iEtS|iEtSeS|a
• E->b
• Elimination:
• S->iEtSS’|a
• S’->eS| ∈
• E->b
Questions: Left Factoring
• X->X+X|X*X|D
• D->1|2|3
Questions: Left Factoring
• X->X+X|X*X|D
• D->1|2|3
• Elimination:
• X->XY|D
• Y->+X|*X
• D->1|2|3
Questions: Left Factoring
• E->T+E|T
• T->int|int*T|(E)
Questions: Left Factoring
• E->T+E|T
• T->int|int*T|(E)
• Elimination:
• E->TE’
• E’->+E| ∈
• T->intT’|(E)
• T’->*T| ∈
Questions: Left Factoring
• S->aSSbS|aSaSb|abb|b
Questions: Left Factoring
• S->aSSbS|aSaSb|abb|b
• Elimination-1:
• S->aS’|b
• S’->SSbS|SaSb|bb
Questions: Left Factoring
• S->aSSbS|aSaSb|abb|b
• Elimination-1:
• S->aS’|b
• S’->SSbS|SaSb|bb
• S’ Elimination:
• S’->SS’’|bb
• S’’->SbS|aSb
Questions: Left Factoring
• S->aSSbS|aSaSb|abb|b
• Elimination-2:
• S->aSS’|abb|b
• S’->SbS|aSb
Questions: Left Factoring
• S->aSSbS|aSaSb|abb|b
• Elimination-2:
• S->aSS’|abb|b
• S’->SbS|aSb
• S Elimination:
• S->aS’’|b
• S’’->SS’|bb
Questions: Left Factoring
• A->aA
• B->aB
Questions: Left Factoring
• A->aA
• B->aB
• Elimination
• No Common Non Terminal on LHS then only Left Factoring
elimination can perform.
Questions: Left Factoring
• S->aAB|aCD
Questions: Left Factoring
• S->aAB|aCD
• Elimination:
• S->aS’
• S’->AB|CD
Questions: Left Factoring
• A->xByA| xByAzA|a
Questions: Left Factoring
• A->xByA| xByAzA|a
• Elimination:
• A-> xByAA’|a
• A’->zA|∈
Questions: Left Factoring
• A->aAB | aA |a
Questions: Left Factoring
• A->aAB | aA |a
• Elimination:
• A-> aAA’|a
• A’->B|∈
Questions: Left Factoring
• A->aAB | aA |a
• Elimination:
• A-> aAA’|a
• A’->B|∈
• A Elimination:
• A->aA’’
• A’’->AA’|∈
Questions: Left Factoring
• A->ad | a | ab | abc | x
FIRST()
• If 𝛼 is any string of grammar symbols then FIRST(𝛼) is the set of
terminals that begin the string derived from 𝛼.
∗
• If 𝛼 ⇒∈ then ∈ is also in FIRST(𝛼)
Rules to compute first() of non terminal
Rules to compute first() of non terminal
Example
• S->ABC|ghi|jkl
• A->a|b|c
• B->b
• D->d
Example
• S->ABC|ghi|jkl
• A->a|b|c
• B->b
• D->d
• Solution:
• FIRST(S) = {a,b,c,g,j}
• FIRST(A) = {a,b,c}
• FIRST(B) = {b}
• FIRST(D) = {d}
Example
• S->ABC
• A->a|b|∈
• B->c|d|∈
• C->e|f|∈
• Solution:
• FIRST(S) = {a,b,c,d,e,f, ∈}
• FIRST(A) = {a,b, ∈}
• FIRST(B) = {c,d, ∈}
• FIRST(C) = {e,f, ∈}
Example
• X->AB
• A->a|∈
• B->b
Example
• X->AB
• A->a|∈
• B->b
• Answer:
• First(X) = {a, b}
• First(A) = The first set of A is {a, ∈}
• First(B) = The first set of B is {b}
Example
• X->AB
• A->a|∈
• B->b|∈
Example
• X->AB
• A->a|∈
• B->b|∈
• Answer:
• First(X) = {a, b, ∈}
• First(A) = The first set of A is {a, ∈}
• First(B) = The first set of B is {b, ∈}
Example
• E->TE’
• E’->+TE’| ∈
• T->FT’
• T’->*FT’| ∈
• F->id|(E)
• FIRST(F)=?
• FIRST(T’)=?
• FIRST(T)=?
• FIRST(E’)=?
• FIRST(E)=?
Example
• E->TE’
• E’->+TE’| ∈
• T->FT’
• T’->*FT’| ∈
• F->id|(E)
• FIRST(F)={id,(}
• FIRST(T’)={*, ∈}
• FIRST(T)={id,(}
• FIRST(E’)={+, ∈}
• FIRST(E)={id,(}
Example
• S->aABb
• A→c|∈
• B->d|∈
Example
• S->aABb
• A→c|∈
• B->d|∈
• Solution:
• FIRST(S) = {a}
• FIRST(A) = {c, ∈}
• FIRST(B) = {d, ∈}
Example
• S->aBDh
• B->cC
• C->bC|∈
• D->EF
• E->g|∈
• F->f|∈
Example
• S->aBDh
• B->cC
• C->bC|∈
• D->EF
• E->g|∈
• F->f|∈
• Solution:
• FIRST(S)={a}
• FIRST(B) = {c}
• FIRST(C)={b, ∈}
• FIRST(D) = {g, f, ∈}
• FIRST(E) = {g, ∈}
• FIRST(F) = {f, ∈}
Example
• S->Bb|Cd
• B->aB|∈
• C->cC|∈
Example
• S->Bb|Cd
• B->aB|∈
• C->cC|∈
• Solution:
• FIRST(S) = {a, b, c, d}
• FIRST(B) = {a, ∈}
• FIRST(C) = {c, ∈}
Example
• A->da|BC
• S->ACB|CbB|Ba
• B->g|∈
• C->h|∈
Example
• A->da|BC
• S->ACB|CbB|Ba
• B->g|∈
• C->h|∈
• Solution:
• FIRST(A) = {d, g, h, ∈}
• FIRST(S) = {d, g, h,b,a, ∈}
• FIRST(B) = {g, ∈}
• FIRST(C) = {h, ∈}
Example
• S->AB
• A->Ca|∈
• B->BaAC|c
• C->b|∈
Example
• S->AB
• A->Ca|∈
• B->BaAC|c
• C->b|∈
• Solution:
• FIRST(S) = {b,a,c}
• FIRST(A) = {b,a, ∈}
• FIRST(B) = {c}
• FIRST(C) = {b, ∈}
Example
• S->ABCDE
• A->a|∈
• B->b|∈
• C->c
• D->d|∈
• E->e|∈
Example
• S->ABCDE
• A->a|∈
• B->b|∈
• C->c
• D->d|∈
• E->e|∈
• Solution:
• FIRST(S) = {a,b,c}
• FIRST(A) = {a, ∈}
• FIRST(B) = {b, ∈}
• FIRST(C) = {c}
• FIRST(D) = {d, ∈}
• FIRST(E) = {e, ∈}
Rules to compute FOLLOW of non terminal
How to apply rules to find FOLLOW of non
terminal?
Example
• S->AaAb|BbBa
• A->∈
• B->∈
Example
• S->AaAb|BbBa
• A->∈
• B->∈
• Solution:
• Follow(S) = {$}
• Follow(A)={a,b}
• Follow(B) = {b,a}
Example
• S->ABC
• A->DEF
• B->∈
• C->∈
• D->∈
• E->∈
• F->∈
Example
• S->aABb
• A->c|∈
• B->d|∈
Example
• S->aABb
• A->c|∈
• B->d|∈
• Solution:
• Follow(S)={$}
• Follow(A) = {d,b}
• Follow(B) = {b}
Example
• S->aBDh
• B->cC
• C->bc|∈
• D->EF
• E->g|∈
• F->f|∈
Example
• S->aBDh
• B->cC
• C->bC|∈
• D->EF
• E->g|∈
• F->f|∈
• Solution:
• Follow(S) = {$}
• Follow(B) = {g,f,h}
• Follow(C) = {g,f,h}
• Follow(D) = {h}
• Follow(E) = {f,h}
• Follow(F) = {h}
Example
• S->Bb|Cd
• B->aB|∈
• C->cC|∈
Example
• S->Bb|Cd
• B->aB|∈
• C->cC|∈
• Solution:
• Follow(S) = {$}
• Follow(B) = {b}
• Follow(C) = {d}
Example
• S->ACB|CbB|Ba
• A->da|BC
• B->g|∈
• C->h|∈
Example
• S->ACB|CbB|Ba
• A->da|BC
• B->g|∈
• C->h|∈
• Solution:
• Follow(S) = {$}
• Follow(A) = {$, h,g}
• Follow(B) = {a,h,g,$}
• Follow(C) = {b,g,$,h}
Example
• S->xyz|aBC
• B->c|cd
• C->eg|df
Example
• S->xyz|aBC
• B->c|cd
• C->eg|df
• Solution:
• Follow(S) = {$}
• Follow(B) = {e,d}
• Follow(C) = {$}
Example
• S->ABCDE
• A->a|∈
• B->b|∈
• C->c
• D->d|∈
• E->e|∈
• Solution:
Example
• S->ABCDE
• A->a|∈
• B->b|∈
• C->c
• D->d|∈
• E->e|∈
• Solution:
• Follow(S) = {$}
• Follow(A) = {b,c}
• Follow(B) = {c}
• Follow(C) = {d,e,$}
• Follow(D) = {e,$}
• Follow(E)= {$}
Example
• Calculate the first and follow functions for the given grammar-
• E → E + T |T
• T → T * F |F
• F → (E) |id
Example
• Eliminate Left Recursion:
• E->TE’
• E’->+TE’| ∈
• T->FT’
• T’->*FT’| ∈
• F->id|(E)
Example
• E->TE’
• E’->+TE’| ∈
• T->FT’
• T’->*FT’| ∈
• F->id|(E)
• Follow(E) = {$,)}
• Follow(E’) = Follow(E) = {$,)}
• Follow(T) = {+,$,)}
• Follow(T’) = {+,$,)}
• Follow(F) = {*,+,$,)}
Example
• Consider the following Grammar:
• S->tABCD
• A->qt|t
• B->r|∈
• C->q|∈
• D->p
• What is the Follow(A)?
• A. {r,q,p,t}
• B. {r,q,p}
• C. {r,q,p, ∈}
• D. {r,q,p,$}
Example
• Which of the following is present in FIRST(X) ∩ FIRST(B) of the below given?
• X→A
• A → Bb | Cd
• B → aB | Cd | ∈
• C → Cc | ∈
• A. {a, c, d, ϵ}
• B. {a, c, d, $}
• C. {a, c, d}
• D. {a, c, ϵ}
Example
• Which of the following is present in FIRST(X) ∩ FIRST(B) of the below given?
• X→A
• A → Bb | Cd
• B → aB | Cd | ∈
• C → Cc | ∈
• A. {a, c, d, ϵ}
• B. {a, c, d, $}
• C. {a, c, d}
• D. {a, c, ϵ}
• Answer: C
Rules to construct LL(1) or predictive parsing
table
Grammar for LL(1) Table
• S->(L)|a
• L->SL’
• L’->∈|,L’
• FIRST (S) = {( a}
• FIRST(L) = {( a}
• FIRST (L’) = {∈ ,}
Grammar for LL(1) Table
• S->(L)|a
• L->SL’
• L’->∈|,L’
• FIRST (S) = {( a}
• FIRST(L) = {( a}
• FIRST (L’) = {∈ ,}
• FOLLOW (S) = {$ , )}
• FOLLOW (L) = { ) }
• FOLLOW (L’) = { ) }
Grammar for LL(1) Table
• S->(L)|a
• L->SL’
• L’->∈|,L’
• FIRST (S) = {( a}
• FIRST(L) = {( a} ( ) a , $
• FIRST (L’) = {∈ ,} S
• FOLLOW (S) = {$ , )}
• FOLLOW (L) = { ) }
L
• FOLLOW (L’) = { ) } L’
Grammar for LL(1) Table
1. S->(L)
• S->(L)|a 2. S->a
• L->SL’ 3. L->SL’
• L’->∈|,L’ 4. L’-> ∈
5. L’->,L’
• FIRST (S) = {( a}
• FIRST(L) = {( a} ( ) a , $
• FIRST (L’) = {∈ ,} S
• FOLLOW (S) = {$ , )}
• FOLLOW (L) = { ) }
L
• FOLLOW (L’) = { ) } L’
Grammar for LL(1) Table
1. S->(L)
• S->(L)|a 2. S->a
• L->SL’ 3. L->SL’
• L’->∈|,L’ 4. L’-> ∈
5. L’->,L’ For First Production rule
• FIRST (S) = {( a}
• FIRST(L) = {( a} ( ) a , $
• FIRST (L’) = {∈ ,}
• FOLLOW (S) = {$ , )} S 1. S->(L)
• FOLLOW (L) = { ) }
L
• FOLLOW (L’) = { ) }
L’
Grammar for LL(1) Table
1. S->(L)
• S->(L)|a 2. S->a
• L->SL’ 3. L->SL’
• L’->∈|,L’ 4. L’-> ∈
5. L’->,L’ For Second Production rule
• FIRST (S) = {( a}
• FIRST(L) = {( a} ( ) a , $
• FIRST (L’) = {∈ ,}
• FOLLOW (S) = {$ , )} S 1. S->(L) 2. S->a
• FOLLOW (L) = { ) }
L
• FOLLOW (L’) = { ) }
L’
Grammar for LL(1) Table
1. S->(L)
• S->(L)|a 2. S->a
• L->SL’ 3. L->SL’
• L’->∈|,SL’ 4. L’-> ∈
5. L’->,SL’ For Third Production rule
• FIRST (S) = {( a}
• FIRST(L) = {( a} ( ) a , $
• FIRST (L’) = {∈ ,}
• FOLLOW (S) = {$ , )} S 1. S->(L) 2. S->a
• FOLLOW (L) = { ) }
L 3. L->SL’ 3. L->SL’
• FOLLOW (L’) = { ) }
L’
Grammar for LL(1) Table
1. S->(L)
• S->(L)|a 2. S->a
• L->SL’ 3. L->SL’
• L’->∈|,SL’ 4. L’-> ∈
5. L’->,SL’ For Fourth Production rule
• FIRST (S) = {( a}
• FIRST(L) = {( a} ( ) a , $
• FIRST (L’) = {∈ ,}
• FOLLOW (S) = {$ , )} S 1. S->(L) 2. S->a
• FOLLOW (L) = { ) }
L 3. L->SL’ 3. L->SL’
• FOLLOW (L’) = { ) }
L’ 4. L’->∈
Grammar for LL(1) Table
1. S->(L)
• S->(L)|a 2. S->a
• L->SL’ 3. L->SL’
• L’->∈|,SL’ 4. L’-> ∈
5. L’->,SL’ For Fifth Production rule
• FIRST (S) = {( a}
• FIRST(L) = {( a} ( ) a , $
• FIRST (L’) = {∈ ,}
• FOLLOW (S) = {$ , )} S 1. S->(L) 2. S->a
• FOLLOW (L) = { ) }
L 3. L->SL’ 3. L->SL’
• FOLLOW (L’) = { ) }
L’ 4. L’->∈ 5. L’->,SL’
Grammar for LL(1) Table
1. S->(L)
• S->(L)|a 2. S->a
• L->SL’ 3. L->SL’
• L’->∈|,SL’ 4. L’-> ∈
5. L’->,SL’ Given Grammar is LL(1) Grammar
• FIRST (S) = {( a}
• FIRST(L) = {( a} ( ) a , $
• FIRST (L’) = {∈ ,}
• FOLLOW (S) = {$ , )} S 1. S->(L) 2. S->a
• FOLLOW (L) = { ) }
L 3. L->SL’ 3. L->SL’
• FOLLOW (L’) = { ) }
L’ 4. L’->∈ 5. L’->,SL’
Example
• S -> AaAb | BbBa
• A -> ∈
• B -> ∈
a b $
S
A
B
Example
• S -> AaAb | BbBa
• A -> ∈
• B -> ∈
a b $
S S->AaAb A->BbBa
A A->∈ A->∈
B B->∈ B->∈
Example
• S->aBa
• B->bB | ∈
a b $
S
B
Example
• S->aBa
• B->bB | ∈
a b $
S S->aBa
B B->∈ B->bB
Example
• S->aB | ∈
• B->bC | ∈
• C->cS | ∈
Example
• S->aB | ∈
• B->bC | ∈
• C->cS | ∈
a b c $
S S->aB S->∈
B B->bC B->∈
C C->cS C->∈
Example for LL(1) Table
• S->aSbS|bSaS| ∈
Example for LL(1) Table
• S->aSbS|bSaS| ∈
1. S->aSbS
a b $
2. S->bSaS
3. S->∈ S
Example for LL(1) Table
• S->aSbS|bSaS| ∈
1. S->aSbS
a b $
2. S->bSaS
1. S->aSbS 2. S->bSaS
3. S->∈ S 3. S->∈ 3. S->∈
3. S->∈
Example for LL(1) Table
• S->aSbS|bSaS| ∈
1. S->aSbS
a b $
2. S->bSaS
1. S->aSbS 2. S->bSaS
3. S->∈ S 3. S->∈ 3. S->∈
3. S->∈
Short Trick for LL(1) Grammar or not (Only for
GATE Exam)
• S->𝛼1 |𝛼2 |𝛼3
• FIRST(𝛼1 ) ∩ FIRST(𝛼2 ) ∩ FIRST(𝛼3 ) = ∅ then LL(1) otherwise not LL(1)
• S->𝛼1 |𝛼2 |∈
• FIRST(𝛼1 ) ∩ FIRST(𝛼2 ) ∩ FOLLOW(S) = ∅ then LL(1) otherwise not LL(1)
• Example-1: • Example-2:
• S->aSa|bS|c • S -> iCtSS’ |a
• S’ -> eS | ∈
• C -> b
Example
• S -> AB |eDa
• A -> ab |c
• B -> dC
• C -> eC | ∈
• D -> fD | ∈
Example
• S -> AB |eDa
• A -> ab |c
• B -> dC
• C -> eC | ∈
• D -> fD | ∈
• First(S) = {a,c,e}
• First(A) = {a,c}
• First(B) = {d}
• First(C)={e, ∈}
• First(D) = {d, ∈}
Example
• S -> AB |eDa • Follow(S) = {$}
• A -> ab |c
• Follow(A) = {d}
• B -> dC
• C -> eC | ∈ • Follow(B) = {$}
• D -> fD | ∈ • Follow(C)={$}
• First(S) = {a,c,e} • Follow(D) = {a}
• First(A) = {a,c}
• First(B) = {d}
• First(C)={e, ∈}
• First(D) = {d, ∈}
Example
• S -> AB |eDa • Follow(S) = {$}
• A -> ab |c
• Follow(A) = {d}
• B -> dC
• C -> eC | ∈ • Follow(B) = {$}
• D -> fD | ∈ • Follow(C)={$}
• First(S) = {a,c,e} • Follow(D) = {a}
• First(A) = {a,c} a b c d e f $
• First(B) = {d} S
• First(C)={e, ∈} A
• First(D) = {d, ∈} B
C
D
Example
• S -> AB |eDa • Follow(S) = {$}
• A -> ab |c
• Follow(A) = {d}
• B -> dC
• C -> eC | ∈ • Follow(B) = {$}
• D -> fD | ∈ • Follow(C)={$}
• First(S) = {a,c,e} • Follow(D) = {a}
• First(A) = {a,c} a b c d e f $
• First(B) = {d} S S->AB S->AB S->eDa
• First(C)={e, ∈} A A->ab A->c
• First(D) = {d, ∈} B B->dC
C C->eC C->∈
D D->∈ D->fD
LL(1) Parser
Left to Right Parser
Look Ahead Input Buffer
a b a b $
LL(1) Parsing
Output
$ Algorithm
Parse Stack
LL(1) Parsing
Output
$ Algorithm
Parse Stack
• FIRST(S) = {a}
• FIRST(A) = {c, ∈}
• FIRST(B) = {d, ∈}
Example for LL(1)
• S -> aABb
• A -> c | ∈
• B -> d | ∈
• FIRST(S) = {a}
• FIRST(A) = {c, ∈}
• FIRST(B) = {d, ∈}
• FOLLOW(S) = {$}
• FOLLOW(A) = {d, b}
• FOLLOW(B) = {b}
Example for LL(1) Parser Table
• S -> aABb
• A -> c | ∈
• B -> d | ∈
• FIRST(S) = {a}
• FIRST(A) = {c, ∈}
• FIRST(B) = {d, ∈}
• FOLLOW(S) = {$}
• FOLLOW(A) = {d, b}
• FOLLOW(B) = {b}
1. S -> aABb a b c d $
2. A -> c
3. A -> ∈ S
4. B -> d
5. B -> ∈ A
B
Example for LL(1) Parser Table
• S -> aABb
• A -> c | ∈
• B -> d | ∈
• FIRST(S) = {a}
• FIRST(A) = {c, ∈}
• FIRST(B) = {d, ∈}
• FOLLOW(S) = {$}
• FOLLOW(A) = {d, b}
• FOLLOW(B) = {b}
1. S -> aABb a b c d $
2. A -> c
3. A -> ∈ S 1. S -> aABb
4. B -> d
5. B -> ∈ A 3. A->∈ 2. A->c 3. A->∈
B 5. B->∈ 4. B->d
a b c d $
S 1. S -> aABb
Example for LL(1) Parser A 3. A->∈ 2. A->c 3. A->∈
F F->id F->(E)
id + * ( ) $
E E->TE’ Error Error E->TE’ Error Error
Example E’
T
Error
T->FT’
E’->+TE’
Error
Error
Error
Error
T->FT’
E’->∈
Error
E’->∈
Error
T’ Error T’->∈ T’->*FT’ Error T’->∈ T’->∈
• E->E+T | T F F->id Error Error F->(E) Error Error
• T->T*F | F Example String: id+id*id$
• F->(E) | id Stack Input Action
$ id+id*id$ Push E into Stack
EPrime(){
if(look_ahead == '+'){
if(look_ahead == 'i'){
look_ahead++;
EPrime();
}
}
else
return;
}
Recursive descent parser Input String: i+i$
E(){
if(look_ahead=='i’){
• E->iE’ look_ahead++;
EPrime();
• E’->+iE’| ∈ }
else
“Error”
EPrime(){
if(look_ahead == '+'){
if(look_ahead == 'i'){
look_ahead++;
EPrime();
}
}
else
return;
}
Input String: i+i$
Recursive descent parser
E(){
if(look_ahead=='i’){
• E->iE’ look_ahead++;
EPrime(); void main(){
• E’->+iE’| ∈ } E();
else if(look_ahead == '$')
“Error” String is Accepted
} else
String is not Accepted
EPrime(){ }
if(look_ahead == '+’){
look_ahead++;
if(look_ahead == 'i'){
look_ahead++;
EPrime();
}
}
else
return;
}
Example-Recursive descent parser
• E->TE’
• E’->+TE’|∈
• T->FT’
• T’->*FT’|∈
• F->(E)|id
T(){
} look_ahead++;
else E();
return; if(look_ahead == ')')
void main(){ } look_ahead++;
E(); }
if(input == '$') else if(look_ahead == 'id')
String is Accepted look_ahead++;
else else
String is not Accepted “error”
} }
T(){
} look_ahead++;
else E();
return; if(look_ahead == ')')
void main(){ } look_ahead++;
E(); }
if(input == '$') else if(look_ahead == 'id')
String is Accepted look_ahead++;
else else
String is not Accepted “error”
} }
Example-Recursive descent parser
• S->(L)|a
• L->L,S|S
Operator precedence
SR Parsing
Output
$ Algorithm
Parse Stack
S/R Shift
Operations Operations
Performing SR Parsing using a Stack
• Major data structure used by SR Parsing are:
• Stack: It is used to hold grammar symbols.
• Input Buffer: Holds the input string that needs to be parsed.
• Major actions performed are:
• SHIFT: Pushing the next input symbol on the top of the stack
• REDUCE: Popping the handle whose right end is at Top Of the Stack and replacing it
with left side non terminal.
• ACCEPT
• ERROR
• Stack Implementation of SR Parser:
• Shift input symbols onto the stack until a handle B is on top of stack.
• Reduce B to left side Non terminal appropriate production
• Repeat until error or stack has the start symbol left and input is empty.
Example of SR Parsing using a stack
• E->E+E|E*E|id
Stack Content Input Action
$ Id1+id2*id3$ shift
$id1 +id2*id3 Reduce by E->id
$E +id2*id3 shift
$E+ Id2*id3$ shift
$E+id2 *id3$ Reduce by E->id
$E+E *id3$ shift
Example of SR Parsing using a stack
• E->E+E|E*E|id
Stack Content Input Action
$ Id1+id2*id3$ shift
$id1 +id2*id3 Reduce by E->id
$E +id2*id3 shift
$E+ Id2*id3$ shift
$E+id2 *id3$ Reduce by E->id
$E+E *id3$ Reduced by E->E+E
$E *id3$ Shift
$E* Id3$ Shift
$E*id3 $ Reduce by E->id
$E*E $ Reduce by E->E*E
$E $ Accept
Conflicts of SR parser
• Why use stack for SR Parsing
• Any Handle will always appear on the top of the stack and the parser need
not search within the stack at any times.
• Conflict In SR Parsing
• 2 decisions decide a successful SR parsing
• Locate the substring to reduce
• Which production to choose when multiple productions with the selected substring
on RHS exist.
• SR parser may reach a configuration in which knowing the contents of stack
and input buffer, still the parser can not decide.
• Whether to perform a shift or a reduce operations (Shift-Reduce Conflicts)
• Which out of the several reductions to make (Reduce – Reduce Conflicts)
EXAMPLE Conflicts: Scenario-1
• E->E+T|T Stack Content Input Action
$ x*x$ shift
• T->T*F|F $x *x$ Reduce F->x
• F->(E)|x $F *x$ Reduce T->F
$T *x$ Shift
$T* x$ Shift
$T*x $ Reduce F->x
$T*F $ Reduce T->T*F
$T $ Reduce E->T
$E $ Accept
EXAMPLE Conflicts: Scenario-2
• E->E+T|T Stack Content Input Action
$ x*x$ shift
• T->T*F|F $x *x$ Reduce F->x
• F->(E)|x $F *x$ Reduce T->F
$T *x$ Reduce E->T
$E *x$ Shift
$E* x$ Shift
$E*x $ Reduce F->x
$E*F $ Reduce T->F
$E*T $ Reduce E->T
$E*E $ Error
• S->aABe
• A->Abc|b
• B->d
• Example String: abbcde
• S->(L)|w
• L->L,S|S
• Input String: (w,(w,w))
LR(k) parser
• Constructed for unambiguous grammar
• May or may not depend on LA symbol
LR(k)
LR Parsing
Output
$ Algorithm
Parse Stack
𝑰𝟐 : Goto(𝑰𝟎 ,𝒂)
𝐴 →𝑎∙𝐴
𝐴 →∙ 𝑎𝐴
𝐴 →∙ 𝑏
Goto Function Example
Example Grammar: Augmented Grammar: 𝑰𝟎 : Closure(𝑨′ → 𝑨) 𝑰𝟏 : Goto(𝑰𝟎 ,𝑨)
𝐴 → 𝑎𝐴 𝐴′ → 𝐴
𝐴→𝑏 𝐴 → 𝑎𝐴 𝐴′ →∙ 𝐴
𝐴→𝑏
𝐴 →∙ 𝑎𝐴 𝐴′ → 𝐴 ∙
𝐴 →∙ 𝑏
𝐴 →𝑎∙𝐴
𝐴 →∙ 𝑎𝐴 𝐴→𝑏∙
𝐴 →∙ 𝑏
Goto Function Example
Example Grammar: Augmented Grammar: 𝑰𝟎 : Closure(𝑨′ → 𝑨) 𝑰𝟏 : Goto(𝑰𝟎 ,𝑨)
𝐴 → 𝑎𝐴 𝐴′ → 𝐴
𝐴→𝑏 𝐴 → 𝑎𝐴 𝐴′ →∙ 𝐴
𝐴→𝑏
𝐴 →∙ 𝑎𝐴 𝐴′ → 𝐴 ∙
𝐴 →∙ 𝑏
𝐴 →𝑎∙𝐴 ?
𝐴 →∙ 𝑎𝐴 𝐴→𝑏∙ 𝐴 → 𝑎𝐴 ∙
𝐴 →∙ 𝑏
𝑰? : Goto(𝑰𝟐 ,𝒃)
?
Goto Function Example
Example Grammar: Augmented Grammar: 𝑰𝟎 : Closure(𝑨′ → 𝑨) 𝑰𝟏 : Goto(𝑰𝟎 ,𝑨)
𝐴 → 𝑎𝐴 𝐴′ → 𝐴
𝐴→𝑏 𝐴 → 𝑎𝐴 𝐴′ →∙ 𝐴
𝐴→𝑏
𝐴 →∙ 𝑎𝐴 𝐴′ → 𝐴 ∙
𝐴 →∙ 𝑏
𝐴
𝐼0 𝐼2 𝐴
𝐴′ →∙ 𝐴
𝐴 →∙ 𝑎𝐴 𝑎
𝐴 →∙ 𝑏 𝐴 →𝑎∙𝐴
𝐴 →∙ 𝑎𝐴
𝑏 𝐴 →∙ 𝑏
𝑎
𝑏
𝐼3 𝐴→𝑏∙
Construction of LR(0) Parse Table
Action GoTo
States Terminals Non Terminals
Terminal-1 Terminal-2 Terminal-3 Non Terminal-1 Non Terminal-2
𝐼0
𝐼1
𝐼2
GoTo
Action
𝑋 Non Terminals
𝑥 Terminals
𝐼𝑖 𝑗
𝐼𝑖 𝑆𝑗
𝐼𝑗 𝐼𝑖 𝑋 𝐼𝑗
𝐼𝑖 𝑥
Construction of LR(0) Parse Table
Action GoTo
States
a b $ A
𝑰𝟎 𝒔𝟐 𝒔𝟑 1 𝐴 → 𝑎𝐴 (𝒓𝟏 )
𝑰𝟏 Acc 𝐴 → 𝑏 (𝒓𝟐 )
𝑰𝟐 𝒔𝟐 𝒔𝟑 4
𝑰𝟑 𝒓𝟐 𝒓𝟐 𝒓𝟐
𝑰𝟒 𝒓𝟏 𝒓𝟏 𝒓𝟏
Example
• S->AA
• A->aA|b
DFA of Example 𝐼5
𝐼2 𝐴 𝑆 → 𝐴𝐴 ∙
𝐼1
𝑆′ → 𝑆 ∙ 𝑆 →𝐴∙𝐴
𝐴 →∙ 𝑎𝐴
𝐴 →∙ 𝑏 𝑏
𝑆 𝐴
𝑎
𝐼0 𝐼3
𝑆 ′ →∙S 𝐼6
𝑆 →∙ 𝐴𝐴 𝑎 𝐴 → 𝑎𝐴 ∙
𝐴 →∙ 𝑎𝐴 𝐴 →𝑎∙𝐴 𝐴
𝐴 →∙ 𝑏 𝐴 →∙ 𝑎𝐴
𝐴 →∙ 𝑏
𝑎
𝑏
𝑏
𝐼3 𝐴→𝑏∙
Parse Table of Example
Action GoTo 𝑆 → 𝐴𝐴
States 𝐴 → 𝑎𝐴|𝑏
a b $ S A
𝑰𝟎 𝒔𝟑 𝒔𝟒 1 2
𝑰𝟏 Accept
𝑰𝟐 𝒔𝟑 𝒔𝟒 5 𝑆 → 𝐴𝐴 (𝒓𝟏 )
𝑰𝟑 𝒔𝟑 𝒔𝟒 6 𝐴 → 𝑎𝐴 (𝒓𝟐 )
𝑰𝟒 𝒓𝟑 𝒓𝟑 𝒓𝟑
𝐴 → 𝑏 (𝒓𝟑 )
𝑰𝟓 𝒓𝟏 𝒓𝟏 𝒓𝟏
𝑰𝟔 𝒓𝟐 𝒓𝟐 𝒓𝟐
𝑆 → 𝐴𝐴 (𝒓𝟏 )
Parsing a string by LR(0) Parser 𝐴 → 𝑎𝐴 (𝒓𝟐 )
𝐴 → 𝑏 (𝒓𝟑 )
Stack Content Input Action Action GoTo
$𝑰𝟎 bb$ States
a b $ S A
𝑰𝟎 𝒔𝟑 𝒔𝟒 1 2
𝑰𝟏 Acc
𝑰𝟐 𝒔𝟑 𝒔𝟒 5
𝑰𝟑 𝒔𝟑 𝒔𝟒 6
𝑰𝟒 𝒓𝟑 𝒓𝟑 𝒓𝟑
𝑰𝟓 𝒓𝟏 𝒓𝟏 𝒓𝟏
𝑰𝟔 𝒓𝟐 𝒓𝟐 𝒓𝟐
𝑆 → 𝐴𝐴 (𝒓𝟏 )
Parsing a string by LR(0) Parser 𝐴 → 𝑎𝐴 (𝒓𝟐 )
𝐴 → 𝑏 (𝒓𝟑 )
Stack Content Input Action Action GoTo
$𝑰𝟎 bb$ 𝒔𝟒 States
a b $ S A
$𝑰𝟎 b𝑰𝟒 b$
𝑰𝟎 𝒔𝟑 𝒔𝟒 1 2
𝑰𝟏 Acc
𝑰𝟐 𝒔𝟑 𝒔𝟒 5
𝑰𝟑 𝒔𝟑 𝒔𝟒 6
𝑰𝟒 𝒓𝟑 𝒓𝟑 𝒓𝟑
𝑰𝟓 𝒓𝟏 𝒓𝟏 𝒓𝟏
𝑰𝟔 𝒓𝟐 𝒓𝟐 𝒓𝟐
𝑆 → 𝐴𝐴 (𝒓𝟏 )
Parsing a string by LR(0) Parser 𝐴 → 𝑎𝐴 (𝒓𝟐 )
𝐴 → 𝑏 (𝒓𝟑 )
Stack Content Input Action Action GoTo
$𝑰𝟎 bb$ 𝒔𝟒 States
a b $ S A
$𝑰𝟎 b𝑰𝟒 b$ 𝒓𝟑
$𝑰𝟎 A𝑰𝟐 b$
𝑰𝟎 𝒔𝟑 𝒔𝟒 1 2
𝑰𝟏 Acc
𝑰𝟐 𝒔𝟑 𝒔𝟒 5
𝑰𝟑 𝒔𝟑 𝒔𝟒 6
𝑰𝟒 𝒓𝟑 𝒓𝟑 𝒓𝟑
𝑰𝟓 𝒓𝟏 𝒓𝟏 𝒓𝟏
𝑰𝟔 𝒓𝟐 𝒓𝟐 𝒓𝟐
𝑆 → 𝐴𝐴 (𝒓𝟏 )
Parsing a string by LR(0) Parser 𝐴 → 𝑎𝐴 (𝒓𝟐 )
𝐴 → 𝑏 (𝒓𝟑 )
Stack Content Input Action Action GoTo
$𝑰𝟎 bb$ 𝒔𝟒 States
a b $ S A
$𝑰𝟎 b𝑰𝟒 b$ 𝒓𝟑
$𝑰𝟎 A𝑰𝟐 b$ 𝒔𝟒
𝑰𝟎 𝒔𝟑 𝒔𝟒 1 2
$𝑰𝟎 A𝑰𝟐 b𝑰𝟒 $ 𝑰𝟏 Acc
𝑰𝟐 𝒔𝟑 𝒔𝟒 5
𝑰𝟑 𝒔𝟑 𝒔𝟒 6
𝑰𝟒 𝒓𝟑 𝒓𝟑 𝒓𝟑
𝑰𝟓 𝒓𝟏 𝒓𝟏 𝒓𝟏
𝑰𝟔 𝒓𝟐 𝒓𝟐 𝒓𝟐
𝑆 → 𝐴𝐴 (𝒓𝟏 )
Parsing a string by LR(0) Parser 𝐴 → 𝑎𝐴 (𝒓𝟐 )
𝐴 → 𝑏 (𝒓𝟑 )
Stack Content Input Action Action GoTo
$𝑰𝟎 bb$ 𝒔𝟒 States
a b $ S A
$𝑰𝟎 b𝑰𝟒 b$ 𝒓𝟑
$𝑰𝟎 A𝑰𝟐 b$ 𝒔𝟒
𝑰𝟎 𝒔𝟑 𝒔𝟒 1 2
$𝑰𝟎 A𝑰𝟐 b𝑰𝟒 $ 𝒓𝟑 𝑰𝟏 Acc
$𝑰𝟎 A𝑰𝟐 𝑨𝑰𝟓 $ 𝑰𝟐 𝒔𝟑 𝒔𝟒 5
𝑰𝟑 𝒔𝟑 𝒔𝟒 6
𝑰𝟒 𝒓𝟑 𝒓𝟑 𝒓𝟑
𝑰𝟓 𝒓𝟏 𝒓𝟏 𝒓𝟏
𝑰𝟔 𝒓𝟐 𝒓𝟐 𝒓𝟐
𝑆 → 𝐴𝐴 (𝒓𝟏 )
Parsing a string by LR(0) Parser 𝐴 → 𝑎𝐴 (𝒓𝟐 )
𝐴 → 𝑏 (𝒓𝟑 )
Stack Content Input Action Action GoTo
$𝑰𝟎 bb$ 𝒔𝟒 States
a b $ S A
$𝑰𝟎 b𝑰𝟒 b$ 𝒓𝟑
$𝑰𝟎 A𝑰𝟐 b$ 𝒔𝟒
𝑰𝟎 𝒔𝟑 𝒔𝟒 1 2
$𝑰𝟎 A𝑰𝟐 b𝑰𝟒 $ 𝒓𝟑 𝑰𝟏 Acc
$𝑰𝟎 A𝑰𝟐 𝑨𝑰𝟓 $ 𝒓𝟏 𝑰𝟐 𝒔𝟑 𝒔𝟒 5
$𝑰𝟎 S𝑰𝟏 $
𝑰𝟑 𝒔𝟑 𝒔𝟒 6
𝑰𝟒 𝒓𝟑 𝒓𝟑 𝒓𝟑
𝑰𝟓 𝒓𝟏 𝒓𝟏 𝒓𝟏
𝑰𝟔 𝒓𝟐 𝒓𝟐 𝒓𝟐
𝑆 → 𝐴𝐴 (𝒓𝟏 )
Parsing a string by LR(0) Parser 𝐴 → 𝑎𝐴 (𝒓𝟐 )
𝐴 → 𝑏 (𝒓𝟑 )
Stack Content Input Action Action GoTo
$𝑰𝟎 bb$ 𝒔𝟒 States
a b $ S A
$𝑰𝟎 b𝑰𝟒 b$ 𝒓𝟑
$𝑰𝟎 A𝑰𝟐 b$ 𝒔𝟒
𝑰𝟎 𝒔𝟑 𝒔𝟒 1 2
$𝑰𝟎 A𝑰𝟐 b𝑰𝟒 $ 𝒓𝟑 𝑰𝟏 Acc
$𝑰𝟎 A𝑰𝟐 𝑨𝑰𝟓 $ 𝒓𝟏 𝑰𝟐 𝒔𝟑 𝒔𝟒 5
$𝑰𝟎 S𝑰𝟏 $ Accept
𝑰𝟑 𝒔𝟑 𝒔𝟒 6
𝑰𝟒 𝒓𝟑 𝒓𝟑 𝒓𝟑
𝑰𝟓 𝒓𝟏 𝒓𝟏 𝒓𝟏
𝑰𝟔 𝒓𝟐 𝒓𝟐 𝒓𝟐
Example
• 𝑆 → 𝐴𝑎|𝐵𝑏
•𝐴→𝑑
•𝐵→𝑑
LR(0) Grammar
• The Grammar for which LR(0) Parser can be constructed is called as
LR(0) Grammar
• The grammar whose LR(0) parse table is free from multiple entries is
called as LR(0) grammar.
SLR(1)
• In SLR(1) Parsing Table, reduce moves are not written in the complete
row. Rather, reduce moves only appear in those columns that have
terminals which appear in the follow of the left side Non-Terminal of
the final item for which reduce move is being written.
• SLR(1) has a single lookahead symbol, unlike LR(0) parser which has
NO lookahead symbol.
• Due to less no. of reduce moves, there are more empty cells. Hence,
HIGHER ERROR DETECTION POWER.
SLR(1)
• The Procedure for constructing the parse table for SLR(1) is similar to
LR(0) but there is restrictions on reducing the entry.
• Wherever there is a final item then place the reduce entries under
the follow symbol of LHS Non Terminal
• If SLR(1) parse table is free from multiple entries then the grammar is
SLR(1) grammar
Relations between LR(0), SLR(1), LL(1)
• Every LR(0) grammar is SLR(1) but every SLR(1) grammar need not be LR(0).
• Number of entries in SLR(1) parse table ≤ Number of entries in LR(0) parse table
SLR(1)
LR(0)
LL(1)
SR Conflict
• In a parsing table, if a cell has both shift move as well as reduce move,
then shift-reduce conflict arises.
• SR Conflict is caused when grammar allows a production rule to be
reduced in a state and in the same state another production rule is
shifted for same token.
RR Conflict
• In a parsing table, if a cell has 2 different reduce moves then reduce-
reduce conflict occurs.
SLR(1) Example
• 𝐸 → 𝑇 + 𝐸|𝑇
• 𝑇 → 𝑇 ∗ 𝐹|𝐹
• 𝐹 → 𝑖𝑑|(𝐸)
𝑆 → 𝐴𝑎𝐵 𝑆 → 𝐴𝑎 𝑏𝐴𝑐 𝑑𝑐|𝑏𝑑𝑎
Examples 𝐴 → 𝑎𝑏|𝑎 𝐴→𝑑
𝐵→𝑏
• 𝐸 → 𝑇 + 𝐸|𝑇 𝑆 → 𝐴𝑎|𝐵𝑏 𝑆 → 𝐴𝑎|𝐵𝑎 𝑆 → 𝐴𝑎𝐴𝑏|𝐵𝑎𝐵𝑎
• 𝑇 → 𝑖𝑑 𝐴→𝑑 𝐴→𝑑 𝐴→𝜖
𝐵→𝑑 𝐵→𝑑 𝐵→𝜖
𝐴 → (𝐴)|𝑏𝐴|𝑎
Example
• 𝐸 → 𝑇 + 𝐸|𝑇
• 𝑇 → 𝑖𝑑
CLR(1)
• S->CC 𝑆 → 𝑎𝐴𝑑 𝑏𝐵𝑑 𝑎𝐵𝑒|𝑏𝐴𝑒
• C->cC|d 𝐴→𝑐
𝐵→𝑐
CLR(1)
𝑆 → 𝐴𝐴
𝐴 → 𝑎𝐴|𝑏
𝐼9
CLR(1) 𝐴 → 𝑎𝐴 ∙, $
𝑆′ → 𝑆 𝐼1 𝐼5
(1) 𝑆 → 𝐴𝐴 𝑆 ′ → 𝑆 ∙, $ 𝑆 → 𝐴𝐴 ∙, $ 𝐴
(2)𝐴 → 𝑎𝐴
(3)𝐴 → 𝑏 𝐴 𝐼6 𝑎
𝑆 𝐼2 𝐴 → 𝑎 ∙ 𝐴, $
𝑎
𝐴 →∙ 𝑎𝐴, $
𝑆 → 𝐴 ∙ 𝐴, $
𝐴 →∙ 𝑏, $ 𝑏
𝐴 →∙ 𝑎𝐴, $
𝐼0
𝑆 ′ →∙ 𝑆, $ 𝐴 𝐴 →∙ 𝑏, $ 𝑏 𝐼7
𝑆 →∙ 𝐴𝐴, $ 𝐴 → 𝑏 ∙, $
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝑎 𝐼3
𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏
𝐴 →∙ 𝑏, 𝑎|𝑏 𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 𝐴 → 𝑎𝐴 ∙, 𝑎|𝑏
𝑏 𝐴 →∙ 𝑏, 𝑎|𝑏 𝑎 𝐼8
𝐼4
𝐴 → 𝑏 ∙, 𝑎|𝑏 𝑏
CLR(1) States
Action GoTo
𝑆′ → 𝑆 a b $ S A
(1) 𝑆 → 𝐴𝐴 𝑰𝟎 𝑠3 𝑠4 1 2
(2)𝐴 → 𝑎𝐴 𝐼9 𝑰𝟏
(3)𝐴 → 𝑏 𝐴 → 𝑎𝐴 ∙, $
𝑰𝟐 𝑠6 𝑠7 5
𝐼5 𝐴 𝑰𝟑 𝑠3 𝑠4 8
𝑆 → 𝐴𝐴 ∙, $ 𝑎
𝐼1 𝑰𝟒
𝑆 ′ → 𝑆 ∙, $ 𝐴 𝐼6 𝐴 → 𝑎 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, $ 𝑰𝟓
𝑆 𝐼2 𝑆 → 𝐴 ∙ 𝐴, $ 𝑎
𝐴 →∙ 𝑏, $
𝐴 →∙ 𝑎𝐴, $ 𝑏 𝑰𝟔 𝑠6 𝑠7 9
𝐼0 𝑏
𝐴 →∙ 𝑏, $ 𝐼7 𝐴 → 𝑏 ∙, $
𝑆 ′ →∙ 𝑆, $ 𝐴 𝑰𝟕
𝑆 →∙ 𝐴𝐴, $ 𝑎 𝐼3
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏 𝐴 𝑰𝟖
𝐴 →∙ 𝑏, 𝑎|𝑏 𝑏 𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎𝐴 ∙, 𝑎|𝑏 𝑰𝟗
𝐴 →∙ 𝑏, 𝑎|𝑏 𝐼8
𝐼4 𝐴 → 𝑏 ∙, 𝑎|𝑏
𝑏 𝑎
CLR(1) States
Action GoTo
𝑆′ → 𝑆 a b $ S A
(1) 𝑆 → 𝐴𝐴 𝑰𝟎 𝑠3 𝑠4 1 2
(2)𝐴 → 𝑎𝐴 𝐼9 𝑰𝟏 Accept
(3)𝐴 → 𝑏 𝐴 → 𝑎𝐴 ∙, $
𝑰𝟐 𝑠6 𝑠7 5
𝐼5 𝐴 𝑰𝟑 𝑠3 𝑠4 8
𝑆 → 𝐴𝐴 ∙, $ 𝑎
𝐼1 𝑰𝟒 𝑟3 𝑟3
𝑆 ′ → 𝑆 ∙, $ 𝐴 𝐼6 𝐴 → 𝑎 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, $ 𝑰𝟓 𝑟1
𝑆 𝐼2 𝑆 → 𝐴 ∙ 𝐴, $ 𝑎
𝐴 →∙ 𝑏, $
𝐴 →∙ 𝑎𝐴, $ 𝑏 𝑰𝟔 𝑠6 𝑠7 9
𝐼0 𝑏
𝐴 →∙ 𝑏, $ 𝐼7 𝐴 → 𝑏 ∙, $
𝑆 ′ →∙ 𝑆, $ 𝐴 𝑰𝟕 𝑟3
𝑆 →∙ 𝐴𝐴, $ 𝑎 𝐼3
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏 𝐴 𝑰𝟖 𝑟2 𝑟2
𝐴 →∙ 𝑏, 𝑎|𝑏 𝑏 𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎𝐴 ∙, 𝑎|𝑏 𝑰𝟗 𝑟2
𝐴 →∙ 𝑏, 𝑎|𝑏 𝐼8
𝐼4 𝐴 → 𝑏 ∙, 𝑎|𝑏
𝑏 𝑎
LALR(1)
Action GoTo
𝑆′ → 𝑆 States
a b $ S A
(1) 𝑆 → 𝐴𝐴 𝑰𝟎 𝑠3 𝑠4 1 2
(2)𝐴 → 𝑎𝐴 𝐼9 𝑰𝟏 Accept
(3)𝐴 → 𝑏 𝐴 → 𝑎𝐴 ∙, $
𝑰𝟐 𝑠6 𝑠7 5
𝐼5 𝐴 𝑰𝟑 𝑠3 𝑠4 8
𝑆 → 𝐴𝐴 ∙, $ 𝑎
𝐼1 𝑰𝟒 𝑟3 𝑟3
𝑆 ′ → 𝑆 ∙, $ 𝐴 𝐼6 𝐴 → 𝑎 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, $ 𝑰𝟓 𝑟1
𝑆 𝐼2 𝑆 → 𝐴 ∙ 𝐴, $ 𝑎
𝐴 →∙ 𝑏, $
𝐴 →∙ 𝑎𝐴, $ 𝑏 𝑰𝟔 𝑠6 𝑠7 9
𝐼0 𝑏
𝐴 →∙ 𝑏, $ 𝐼7 𝐴 → 𝑏 ∙, $
𝑆 ′ →∙ 𝑆, $ 𝐴 𝑰𝟕 𝑟3
𝑆 →∙ 𝐴𝐴, $ 𝑎 𝐼3
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏 𝐴 𝑰𝟖 𝑟2 𝑟2
𝐴 →∙ 𝑏, 𝑎|𝑏 𝑏 𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎𝐴 ∙, 𝑎|𝑏 𝑰𝟗 𝑟2
𝐴 →∙ 𝑏, 𝑎|𝑏 𝐼8
𝐼4 𝐴 → 𝑏 ∙, 𝑎|𝑏
𝑏 𝑎
LALR(1)
Action GoTo
𝑆′ → 𝑆 States
a b $ S A
(1) 𝑆 → 𝐴𝐴 𝑰𝟎 𝑠3 𝑠4 1 2
(2)𝐴 → 𝑎𝐴 𝐼9 𝑰𝟏 Accept
(3)𝐴 → 𝑏 𝐴 → 𝑎𝐴 ∙, $
𝑰𝟐 𝑠6 𝑠7 5
𝐼5 𝐴 𝑰𝟑 𝑠3 𝑠4 8
𝑆 → 𝐴𝐴 ∙, $ 𝑎
𝐼1 𝑰𝟒 𝑟3 𝑟3
𝑆 ′ → 𝑆 ∙, $ 𝐴 𝐼6 𝐴 → 𝑎 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, $ 𝑰𝟓 𝑟1
𝑆 𝐼2 𝑆 → 𝐴 ∙ 𝐴, $ 𝑎
𝐴 →∙ 𝑏, $
𝐴 →∙ 𝑎𝐴, $ 𝑏 𝑰𝟔 𝑠6 𝑠7 9
𝐼0 𝑏
𝐴 →∙ 𝑏, $ 𝐼7 𝐴 → 𝑏 ∙, $
𝑆 ′ →∙ 𝑆, $ 𝐴 𝑰𝟕 𝑟3
𝑆 →∙ 𝐴𝐴, $ 𝑎 𝐼3
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏 𝐴 𝑰𝟖 𝑟2 𝑟2
𝐴 →∙ 𝑏, 𝑎|𝑏 𝑏 𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎𝐴 ∙, 𝑎|𝑏 𝑰𝟗 𝑟2
𝐴 →∙ 𝑏, 𝑎|𝑏 𝐼8
𝐼4 𝐴 → 𝑏 ∙, 𝑎|𝑏
𝑏 𝑎
LALR(1)
Action GoTo
𝑆′ → 𝑆 States
a b $ S A
(1) 𝑆 → 𝐴𝐴 𝑰𝟎 𝑠3 𝑠4 1 2
(2)𝐴 → 𝑎𝐴 𝐼9 𝑰𝟏 Accept
(3)𝐴 → 𝑏 𝐴 → 𝑎𝐴 ∙, $
𝑰𝟐 𝑠6 𝑠7 5
𝐼5 𝐴 𝑰𝟑 𝑠3 𝑠4 8
𝑆 → 𝐴𝐴 ∙, $ 𝑎
𝐼1 𝑰𝟒 𝑟3 𝑟3
𝑆 ′ → 𝑆 ∙, $ 𝐴 𝐼6 𝐴 → 𝑎 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, $ 𝑰𝟓 𝑟1
𝑆 𝐼2 𝑆 → 𝐴 ∙ 𝐴, $ 𝑎
𝐴 →∙ 𝑏, $
𝐴 →∙ 𝑎𝐴, $ 𝑏 𝑰𝟔 𝑠6 𝑠7 9
𝐼0 𝑏
𝐴 →∙ 𝑏, $ 𝐼7 𝐴 → 𝑏 ∙, $
𝑆 ′ →∙ 𝑆, $ 𝐴 𝑰𝟕 𝑟3
𝑆 →∙ 𝐴𝐴, $ 𝑎 𝐼3
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏 𝐴 𝑰𝟖 𝑟2 𝑟2
𝐴 →∙ 𝑏, 𝑎|𝑏 𝑏 𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎𝐴 ∙, 𝑎|𝑏 𝑰𝟗 𝑟2
𝐴 →∙ 𝑏, 𝑎|𝑏 𝐼8
𝐼4 𝐴 → 𝑏 ∙, 𝑎|𝑏
𝑏 𝑎
LALR(1)
Action GoTo
𝑆′ → 𝑆 States
a b $ S A
(1) 𝑆 → 𝐴𝐴 𝑰𝟎 𝑠3 𝑠4 1 2
(2)𝐴 → 𝑎𝐴 𝐼9 𝑰𝟏 Accept
(3)𝐴 → 𝑏 𝐴 → 𝑎𝐴 ∙, $
𝑰𝟐 𝑠6 𝑠7 5
𝐼5 𝐴 𝑰𝟑 𝑠3 𝑠4 8
𝑆 → 𝐴𝐴 ∙, $ 𝑎
𝐼1 𝑰𝟒 𝑟3 𝑟3
𝑆 ′ → 𝑆 ∙, $ 𝐴 𝐼6 𝐴 → 𝑎 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, $ 𝑰𝟓 𝑟1
𝑆 𝐼2 𝑆 → 𝐴 ∙ 𝐴, $ 𝑎
𝐴 →∙ 𝑏, $
𝐴 →∙ 𝑎𝐴, $ 𝑏 𝑰𝟔 𝑠6 𝑠7 9
𝐼0 𝑏
𝐴 →∙ 𝑏, $ 𝐼7 𝐴 → 𝑏 ∙, $
𝑆 ′ →∙ 𝑆, $ 𝐴 𝑰𝟕 𝑟3
𝑆 →∙ 𝐴𝐴, $ 𝑎 𝐼3
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏 𝐴 𝑰𝟖 𝑟2 𝑟2
𝐴 →∙ 𝑏, 𝑎|𝑏 𝑏 𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎𝐴 ∙, 𝑎|𝑏 𝑰𝟗 𝑟2
𝐴 →∙ 𝑏, 𝑎|𝑏 𝐼8
𝐼4 𝐴 → 𝑏 ∙, 𝑎|𝑏
𝑏 𝑎
LALR(1)
Action GoTo
𝑆′ → 𝑆 States
a b $ S A
(1) 𝑆 → 𝐴𝐴 𝑰𝟎 𝑠36 𝑠47 1 2
(2)𝐴 → 𝑎𝐴 𝐼9 𝑰𝟏 Accept
(3)𝐴 → 𝑏 𝐴 → 𝑎𝐴 ∙, $
𝑰𝟐 𝑠36 𝑠47 5
𝐼5 𝐴 𝑰𝟑𝟔 𝑠36 𝑠47 89
𝑆 → 𝐴𝐴 ∙, $ 𝑎
𝐼1 𝑰𝟒𝟕 𝑟3 𝑟3 𝑟3
𝑆′ → 𝑆 ∙, $ 𝐴 𝐼6 𝐴 → 𝑎 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, $ 𝑰𝟓 𝑟1
𝑆 𝐼2 𝑆 → 𝐴 ∙ 𝐴, $ 𝑎
𝐴 →∙ 𝑏, $
𝐼0 𝐴 →∙ 𝑎𝐴, $ 𝑏
𝑏
𝐴 →∙ 𝑏, $ 𝐼7 𝐴 → 𝑏 ∙, $
𝑆 ′ →∙ 𝑆, $ 𝐴
𝑆 →∙ 𝐴𝐴, $ 𝑎 𝐼3
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏 𝐴 𝑰𝟖𝟗 𝑟2 𝑟2 𝑟2
𝐴 →∙ 𝑏, 𝑎|𝑏 𝑏 𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎𝐴 ∙, 𝑎|𝑏
𝐴 →∙ 𝑏, 𝑎|𝑏 𝐼8
𝐼4 𝐴 → 𝑏 ∙, 𝑎|𝑏
𝑏 𝑎
LALR(1)
Action GoTo
𝑆′ → 𝑆 States
a b $ S A
(1) 𝑆 → 𝐴𝐴 𝑰𝟎 𝑠36 𝑠47 1 2
(2)𝐴 → 𝑎𝐴 𝑰𝟏 Accept
(3)𝐴 → 𝑏
𝑰𝟐 𝑠36 𝑠47 5
𝐼5 𝑰𝟑𝟔 𝑠36 𝑠47 89
𝑆 → 𝐴𝐴 ∙, $
𝐼1 𝑰𝟒𝟕 𝑟3 𝑟3 𝑟3
𝑆′ → 𝑆 ∙, $ 𝐴
𝑏
𝐼47 𝑰𝟓 𝑟1
𝑆 𝐼2 𝑆 → 𝐴 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, $ 𝑰𝟖𝟗 𝑟2 𝑟2 𝑟2
𝐼0
𝐴 →∙ 𝑏, $ 𝑎
𝑆 ′ →∙ 𝑆, $ 𝐴
𝑆 →∙ 𝐴𝐴, $ 𝑎 𝐼36
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 → 𝑎 ∙ 𝐴, 𝑎 𝑏 $
𝑏 𝐴
𝐴 →∙ 𝑏, 𝑎|𝑏 𝐴 →∙ 𝑎𝐴, 𝑎 𝑏 $ 𝐴 → 𝑎𝐴 ∙, 𝑎 𝑏 $
𝐴 →∙ 𝑏, 𝑎 𝑏 $ 𝐼89
𝐼47 𝐴 → 𝑏 ∙, 𝑎 𝑏 $ 𝑎
𝑏
CLR(1) Example
• S → Xx | yXz | Yz | yYx
•X→v
•Y→v
S → Xx
S → yXz
S → Yz
CLR(1) 𝐼6
𝐼11
S → yYx 𝐼1 𝑆 → 𝑦𝑋𝑧 ∙, $
X→v 𝑆 → 𝑋𝑥 ∙, $
Y→v 𝑆 ′ → 𝑆 ∙, $ 𝑥 𝑧
𝐼12
𝐼7
𝐼2 𝑆 → 𝑋 ∙ 𝑥, $ 𝑆 → 𝑦𝑋 ∙ 𝑧, $ 𝑆 → 𝑦𝑌𝑥 ∙, $
𝑆 𝑋 𝑥
𝐼8
𝑋 𝐼3 𝑆 → 𝑦 ∙ 𝑋𝑧, $ 𝑌 𝑆 → 𝑦𝑌 ∙ 𝑥, $
𝐼0 𝑆 → 𝑦 ∙ 𝑌𝑥, $
𝑆 ′ →∙ 𝑆, $
𝑋 →∙ 𝑣, 𝑧 𝐼9
𝑆 →∙ 𝑋𝑥, $ 𝑦 𝑣
𝑆 →∙ 𝑦𝑋𝑧, $ 𝑌 →∙ 𝑣, 𝑥
𝑆 →∙ 𝑌𝑧, $ 𝑋 → 𝑣 ∙, 𝑧
𝐼4 𝑌 → 𝑣 ∙, 𝑥
𝑆 →∙ 𝑦𝑌𝑥, $
𝑋 →∙ 𝑣, 𝑥 𝑌 𝑆 → 𝑌 ∙ 𝑧, $ 𝐼10
𝑌 →∙ 𝑣, 𝑧 𝑧
𝐼5 𝑆 → 𝑌𝑧 ∙, $
𝑣
𝑋 → 𝑣 ∙, 𝑥
𝑌 → 𝑣 ∙, 𝑧
Stat Action GoTo
CLR(1) es v x y z $ S X Y
1. S → Xx 𝑰𝟎 𝑠5 𝑠3 1 2 4
2. S → yXz 𝑰𝟏 Accept
3. S → Yz
𝑰𝟐 𝑠6
4. S → yYx
𝑰𝟑 𝑠9 7 8
5. X→v
𝑰𝟒 𝑠10
6. Y→v
𝑰𝟓 𝑟5 𝑟6
𝑰𝟔 𝑟1
𝑰𝟕 𝑠11
𝑰𝟖 𝑠12
𝑰𝟗 𝑟6 𝑟5
𝑰𝟏𝟎 𝑟3
𝑰𝟏𝟏 𝑟2
𝑰𝟏𝟐 𝑟4
CLR(1)
Stack Symbols Input Action
$𝑰𝟎 yvz$ Shift
$𝑰𝟎 𝑰𝟑 y vz$ Shift
$𝑰𝟎 𝑰𝟑 𝑰𝟗 yv z$ Reduce by X → v
$𝑰𝟎 𝑰𝟑 𝑰𝟕 yX z$ Shift
$𝑰𝟎 𝑰𝟑 𝑰𝟕 𝑰𝟏𝟏 yXz $ Reduce by S → yXz
$𝑰𝟎 𝑰𝟏 S $ Accept
CLR(1)
𝑆 → 𝐴𝑎 𝑏𝐴𝑐 𝐵𝑐|𝑏𝐵𝑎
𝐴→𝑑
𝐵→𝑑
𝑆 → 𝐴𝑎 𝑏𝐴𝑐 𝐵𝑐|𝑏𝐵𝑎 𝐼11
𝐴→𝑑
CLR(1) 𝐵→𝑑 𝐼6 𝑆 ′ → 𝑏𝐴𝑐 ∙, $
𝑆 ′ → 𝐴𝑎 ∙, $ 𝐼12
𝑎
𝐼1 ′ 𝑐 𝑆 ′ → 𝑏𝐵𝑎 ∙, $
𝑆 → 𝑆 ∙, $ 𝐼2 𝑆 ′ → 𝐴 ∙ 𝑎, $ 𝐼7 𝐼8
𝐼3 𝑐 𝑆 → 𝐵𝑐 ∙, $ 𝑆 ′ → 𝑏𝐴 ∙ 𝑐, $
𝑎
𝑆 𝐴 𝑆 → 𝐵 ∙ 𝑐, $ 𝐼4 𝐴 𝐼9
𝑆 ′ → 𝑏𝐵 ∙ 𝑎, $
𝐼0 𝑆 ′ → 𝑏 ∙ 𝐴𝑐, $
𝐵
𝑆 ′ →∙ 𝑆, $ 𝐵 𝐼10
𝐴 →∙ 𝑑, 𝑐 𝑑 𝐴 → 𝑑 ∙, 𝑐
𝑆 →∙ 𝐴𝑎, $ 𝑏 𝑆 → 𝑏 ∙ 𝐵𝑎, $ 𝐵 → 𝑑 ∙, 𝑎
𝑆 →∙ 𝑏𝐴𝑐, $
𝑆 →∙ 𝐵𝑐, $ 𝐵 →∙ 𝑑, 𝑎
𝑆 →∙ 𝑏𝐵𝑎, $
𝑑 𝐴 → 𝑑 ∙, 𝑎
𝐴 →∙ 𝑑, 𝑎
𝐵 →∙ 𝑑, 𝑐 𝐵 → 𝑑 ∙, 𝑐
𝐼5
Action GoTo
States
a b c d $ S A B
CLR(1) 𝑰𝟎 𝑠4 𝑠5 1 2 3
𝑰𝟏 Accept
(1)𝑆 → 𝐴𝑎 𝑰𝟐 𝑠6
(2)𝑆 → 𝑏𝐴𝑐 𝑰𝟑 𝑠7
(3)𝑆 → 𝐵𝑐 𝑰𝟒 𝑠10 8 9
(4)𝑆 → 𝑏𝐵𝑎 𝑰𝟓 𝑟5 𝑟6
(5)𝐴 → 𝑑
𝑰𝟔 𝑟1
(6)𝐵 → 𝑑
𝑰𝟕 𝑟3
𝑰𝟖 𝑠11
𝑰𝟗 𝑠12
𝑰𝟏𝟎 𝑟6 𝑟5
𝑰𝟏𝟏 𝑟2
𝑰𝟏𝟐 𝑟4
𝑆 → 𝐴𝑎 𝑏𝐴𝑐 𝐵𝑐|𝑏𝐵𝑎 𝐼11
𝐴→𝑑
LALR(1) 𝐵→𝑑 𝐼6 𝑆 ′ → 𝑏𝐴𝑐 ∙, $
𝑆 ′ → 𝐴𝑎 ∙, $ 𝐼12
𝑎
𝐼1 ′ 𝑐 𝑆 ′ → 𝑏𝐵𝑎 ∙, $
𝑆 → 𝑆 ∙, $ 𝐼2 𝑆 ′ → 𝐴 ∙ 𝑎, $ 𝐼7 𝐼8
𝐼3 𝑐 𝑆 → 𝐵𝑐 ∙, $ 𝑆 ′ → 𝑏𝐴 ∙ 𝑐, $
𝑆 𝐴 𝑆 → 𝐵 ∙ 𝑐, $ 𝐼4 𝐴 𝐼9
𝑆 ′ → 𝑏𝐵 ∙ 𝑎, $
𝐼0 𝐵 𝑆 ′ → 𝑏 ∙ 𝐴𝑐, $ 𝐵
𝑆′ →∙ 𝑆, $ 𝐼10
𝐴 →∙ 𝑑, 𝑐 𝑑 𝐴 → 𝑑 ∙, 𝑐
𝑆 →∙ 𝐴𝑎, $ 𝑏 𝑆 → 𝑏 ∙ 𝐵𝑎, $ 𝐵 → 𝑑 ∙, 𝑎
𝑆 →∙ 𝑏𝐴𝑐, $
𝑆 →∙ 𝐵𝑐, $ 𝐵 →∙ 𝑑, 𝑎
𝑆 →∙ 𝑏𝐵𝑎, $
𝑑 𝐴 → 𝑑 ∙, 𝑎
𝐴 →∙ 𝑑, 𝑎
𝐵 →∙ 𝑑, 𝑐 𝐵 → 𝑑 ∙, 𝑐
𝐼5
Action GoTo
States
a b c d $ S A B
LALR(1) 𝑰𝟎 𝑠4 𝑠5 1 2 3
𝑰𝟏 Accept
(1)𝑆 → 𝐴𝑎 𝑰𝟐 𝑠6
(2)𝑆 → 𝑏𝐴𝑐 𝑰𝟑 𝑠7
(3)𝑆 → 𝐵𝑐 𝑰𝟒 𝑠10 8 9
(4)𝑆 → 𝑏𝐵𝑎 𝑰𝟓 𝑟5 𝑟6
(5)𝐴 → 𝑑
𝑰𝟔 𝑟1
(6)𝐵 → 𝑑
𝑰𝟕 𝑟3
𝑰𝟖 𝑠11
𝑰𝟗 𝑠12
𝑰𝟏𝟎 𝑟6 𝑟5
𝑰𝟏𝟏 𝑟2
𝑰𝟏𝟐 𝑟4
Example
• 𝑆 → 𝐴𝑎𝐴𝑏|𝐵𝑏𝐵𝑎
•𝐴→𝜖
•𝐵→𝜖
Example
• 𝑆 → 𝑆 |𝑎
LR
• All grammars used in the construction of LR-parsing tables must be
un-ambiguous.
• Can we create LR-parsing tables for ambiguous grammars?
– Yes, but they will have conflicts.
– We can resolve these conflicts in favor of one of them to
disambiguate the grammar
LALR Exercise
•𝑆→𝐿=𝑅
•𝑆→𝑅
• 𝐿 →∗ 𝑅
• 𝐿 → 𝑖𝑑
•𝑅→𝐿
LALR Exercise
• 𝐸 → 𝑇 + 𝐸|𝑇
• 𝑇 → 𝑇 ∗ 𝐹|𝐹
• 𝐹 → 𝑖𝑑