Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
61 views

Unit-3 Syntax Analysis

Uploaded by

khushiganatra4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Unit-3 Syntax Analysis

Uploaded by

khushiganatra4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 319

Compiler Design

Unit-3 Syntax Analysis


The Role of the Parser, Types of grammar, CFG, Leftmost derivation, Rightmost derivation,
Parse Tree, Restriction on CFG, Ambiguous grammar, TopDown Parsing, Issues of CFG,
Recursive Descent Parser, Construction of Predictive Parsing Table , LL (1) Grammar, String
Parsing using M-Table, Bottom-Up Parsing: Handle, Shift-reduce parser, LR parsers: LR (0), SLR
(1), LALR (1), CLR(1), String parsing procedure using LR parser, R-R and S-R Conflicts.
Role of parser

Token Parse
Source Lexical Parse tree Rest of front IR
Parser
tree end
program analyzer

Get next token

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.

Alphabets Ai Languages Li for Ai


A0={0,1} L0={0,1,100,101,...}
A1={a,b,c} L1={a,b,c, ac, abcc..}
A2={all of C tokens} L2= {all sentences of C program }
Grammar
• A finite set of rules
• that generates only and all sentences of a language.
• that assigns an appropriate structural description to each one.
Formal Grammar G
• formal grammar G as a production system consisting of the following:
• A finite alphabet Σ. The concept of an alphabet used here is a very general one.
An alphabet can, for example, consist of all Unicode characters; but it may also
consist of all keywords of a programming language, all pictographs of the
Sumerian script, the sounds of a bird song (bird songs do have a grammar!2), or
the element and attribute names defined for an XML document type.
• A finite set of non-terminal symbols N. As the name says, these symbols will not
appear in the final document instance but are used only in the production
process.
• A start symbol S taken out of the set of non-terminal symbols N.
• A finite set of generative rules R. Each rule transforms an expression of non-
terminal symbols and alphabet symbols (terminal symbols) into another
expression of non-terminal symbols and alphabet symbols.
Classification of Grammars
Type-0 Recursively Enumerable Grammar
• Type-0 grammars (unrestricted grammars) include all formal
grammars.
• They generate exactly all languages that can be recognized by a Turing
machine.
• These languages are also known as the recursively enumerable
languages.
• Note that this is different from the recursive languages which can be
decided by an always-halting Turing machine.
• Class 0 grammars are too general to describe the syntax of
programming languages ​and natural languages​​.
Type 1: Context-sensitive grammars
• Type-1 grammars generate the context-sensitive languages.
• These grammars have rules of the form α→β where α, β∈ (T∪N)* and
len(α) <= len(β) and α should contain atleast 1 non terminal.
• The languages described by these grammars are exactly all languages that
can be recognized by a linear bounded automaton.
• Example:
• AB → CDB
• AB → CdEB
• ABcd → abCDBcd
• B→b
Type 2: Context-free grammars
• Type-2 grammars generate the context-free languages.
• These grammars have rules of the form A→ρ where A ∈ N
and ρ ∈ (T∪N)*.
• The languages described by these grammars are exactly all languages
that can be recognized by a non-deterministic pushdown automaton.
• Example:
• A → aBc
Type 3: Regular grammars
• Type-3 grammars generate the regular languages.
• These grammars have rules of the form A→a or A→aB where A,B ∈ N(non terminal) and
a∈T(Terminal).
• These languages are exactly all languages that can be decided by a finite state
automaton. Additionally,this family of formal languages can be obtained by regular
expresions. Regular languages are commonly used to define search patterns and the
lexical structure of programming languages.
• Example:
• A→ε
• A→ a
• A → abc
• A→ B
• A → abcB
Chomsky Classification of Languages
Grammar Type Production Rules Language Accepted Automata

Type-3 (Regular A→a or A→aB where A,B ∈ N(non


terminal) and a∈T(Terminal)
Regular (RL) Finite Automata (FA)
Grammar)

Type-2 (Context Free A→ρ where A ∈ N Push Down Automata


and ρ ∈ (T∪N)*
Context Free (CFL)
Grammar) (PDA)

α→β where α, β∈ (T∪N)* and len(α)


Type-1 (Context Linear Bound
<= len(β) and α should contain Context Sensitive (CSL)
Sensitive Grammar) atleast 1 non terminal. Automata (LBA)

Type-0 (Recursive α→β where α, β ∈ (T∪N)* and α Recursive Enumerable


contains atleast 1 non-terminal Turing Machine (TM)
Enumerable) (RE)
Context Free Grammar
• A context free grammar (CFG) is a 4-tuple G = (V, Σ, S, P) where,
• V is finite set of non terminals,
• Σ is disjoint finite set of terminals,
• S is an element of V and it’s a start symbol,
• P is a finite set formulas of the form A→ α where A ∈ V and α∈ (V ∪ Σ)∗
Context Free Grammar
• Nonterminal symbol:
• The name of syntax category of a language, e.g., noun, verb, etc.
•  The It is written as a single capital letter, or as a name enclosed between
< … >, e.g., A or
• <Noun>
Context Free Grammar
• Terminal symbol:
•  A symbol in the alphabet.
•  It is denoted by lower case letter and punctuation marks used in
language.
Context Free Grammar
• Start symbol:
•  First nonterminal symbol of the grammar is called start symbol.
Context Free Grammar
• Production:
A production, also called a rewriting rule, is a rule of grammar. It has the form
of
A nonterminal symbol → String of terminal and nonterminal symbols
Example of Context Free Grammar
• Write terminals, non terminals, start symbol, and productions for following
grammar.
• E → E O E| (E) | -E | id
• O→ +|-|*|/ |↑
Example of Context Free Grammar
•Write terminals, non terminals, start symbol, and productions for following grammar.
• E → E O E| (E) | -E | id
• O → +|-| *|/ |↑
• Terminals:id + - * / ↑ ( )
• Non terminals: E, O
• Start symbol: E
• Productions: E → E O E| (E) | -E | id
O→+|-|*|/ |↑
Example of Grammar
• Grammar for expressions consisting of digits and plus and minus signs.
• Grammar G for a language L={9-5+2, 3-1, ...}
• G=(N,T,P,S)
• N={list,digit}
• T={0,1,2,3,4,5,6,7,8,9,-,+}
• P:
• list -> list + digit
• list -> list - digit
• list -> digit
• digit -> 0|1|2|3|4|5|6|7|8|9
• S=list
Example of Grammar
• Some definitions for a language L and its grammar G
• Derivation : A sequence of replacements S⇒α1⇒α2⇒…⇒αn is a derivation
of αn.
• Language of grammar L(G)
• L(G) is a set of sentences that can be generated from the grammar G.
• L(G)={x| S ⇒* x} where x ∈ a sequence of terminal symbols
• Example: Consider a grammar G=(N,T,P,S):
• N={S} T={a,b}
• S=S P ={S → aSb | ε }
• is aabb a sentecne of L(g)? (derivation of string aabb)
• S⇒aSb⇒aaSbb⇒aaεbb⇒aabb(or S⇒* aabb) so, aabbεL(G)
• there is no derivation for aa, so aa∉L(G)
• note L(G)={anbn| n≧0} where anbn meas n a's followed by n b's.
Example of Grammar
• Example: S->aSb|ε
Simplifying Context Free Grammars
• Some of the productions of CFGs are not useful and are redundant.
• Types of redundant productions and the procedure of removing them
are mentioned below.
• 1. Useless productions
• 2. λ productions (lambda productions or null productions)
• 3. Unit productions
1. Useless productions
• The productions that can never take part in derivation of any string , are called useless
productions. Similarly , a variable that can never take part in derivation of any string is called a
useless variable.
• Example:
• S -> abS | abA | abB
• A -> cd
• B -> aB
• C -> dc
• production ‘C -> dc’ is useless because the variable ‘C’ will never occur in derivation of any string.
• Production ‘B ->aB’ is also useless because there is no way it will ever terminate .
• To remove useless productions , So the modified grammar becomes –
• S -> abS | abA
• A -> cd
2. λ productions (lambda productions or null
productions)
• The productions of type ‘A -> λ’ are called λ productions.
• These productions can only be removed from those grammars that do
not generate λ (an empty string). It is possible for a grammar to
contain null productions and yet not produce an empty string.
• Consider the grammar –
• S -> ABCd (1)
• A -> BC (2)
• B -> bB | λ (3)
• C -> cC | λ (4)
2. λ productions (lambda productions or null
productions)
• start with the first production. Add the first production as it is. Then
we create all the possible combinations that can be formed by
replacing the nullable variables with λ.
• S -> ABCd | ABd | ACd | BCd | Ad | Bd |Cd | d
• A -> BC | B | C
• B -> bB | b
• C -> cC | c
3. Unit productions
• The productions of type ‘A -> B’ are called unit productions.
• The unit productions are the productions in which one non-terminal gives
another non-terminal.
• Example:
• S -> Aa | B
• A -> b | B
• B -> A | a
• Last Result:
• S->Aa|b|a
• A->b|a
Derivation
• Derivation is used to find whether the string belongs to a given grammar
or not.
• Types of derivations are:
1. Leftmost derivation
2. Rightmost derivation
Leftmost derivation
• A derivation of a string W in a grammar G is a left most derivation if at
every step the left most non terminal is replaced.
• Grammar: S→S+S | S-S | S*S | S/S | a Output string: a*a-a
Leftmost derivation
• A derivation of a string W in a grammar G is a left most derivation if at
every step the left most non terminal is replaced.
• Grammar: S→S+S | S-S | S*S | S/S | a Output string: a*a-a

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

A small piece of silicon


Ambiguity
• In formal language grammar, ambiguity would arise if identical string can
occur on the RHS of two or more productions.
• Grammar:
N1 N2 Replaced by
• N1 → α N 1 or N 2 ?

• N2 → α a

• α can be derived from either N1 or N2


Ambiguous grammar
• Ambiguous grammar is one that produces more than one leftmost or
more than one rightmost derivation for the same sentence.
• Grammar: S→S+S | S*S | (S) | a Output string: a+a*a
Ambiguous grammar
• Ambiguous grammar is one that produces more than one leftmost or
more then one rightmost derivation for the same sentence.
• Grammar: S→S+S | S*S | (S) | a Output string: a+a*a
S S S S
→S*S →S+S
S * S S + S
→S+S*S →a+S
→a+S*S S S →a+S*S a
+ a S * S
→a+a*S →a+a*S
→a+a*a a a →a+a*a a a
• Here, Two leftmost derivation for string a+a*a is possible hence, above grammar is ambiguous.
Check Ambiguity in following grammars:
1. S→ aS | Sa | ∈ (output string: aaaa)
2. S→ aSbS | bSaS | ∈ (output string: abab)
3. S→ SS+ | SS* | a (output string: aa+a*)
4. <exp> → <exp> + <term> | <term>
• <term> → <term> * <letter> | <letter>
• <letter> → a|b|c|…|z (output string: a+b*c)
5. Prove that the CFG with productions: S → a | Sa | bSS | SSb | SbS is
ambiguous (Hint: consider output string yourself)
Associativity of Operators
• When an operand has operators on both its sides (left and right) then we
need rules to decide with which operator we will associate this operand.
• Left Associative & Right Associative
• +:Left Associative
• -, *,/:Left Associative
• =, ↑:Right Associative
• Parse trees for left associative operators are more towards left side in
terms of length.
• Parse trees for right associative operators are more towards right side in
terms of length.
Precedence of Operators
• Whenever an operator has a higher precedence than the other
operator, it means that the first operator will get its operands before
the operator with lower precedence.
• *,/ > +,-
Converting Ambiguous grammar to
unambiguous grammar
• * / -> Left, Higher
• + - -> Left, Lower
• E->E+T|E-T|T
• T->T*F|T/F|F
• F->id
+
• Left Recursion: If 𝐴 ⇒ 𝐴𝛼
+
• Right Recursion: If 𝐴 ⇒ 𝛼𝐴
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 α.

• Direct Left Recursion


• A->Aa
• Indirect Left Recursion
• S->Aa
• A->Sb

• Why need to remove Left Recursion?


• Top Down Parsers can not handle left recursion/grammars with having left
recursion
• Left recursion elimination:
• 𝐴 → 𝐴𝛼|𝛽 ⇒
• 𝐴 → 𝛽𝐴′
• 𝐴′ → 𝛼𝐴′ | ∈
Left recursion
• Advantages:
• We are able to generate the same language even after remaining Left Recursion

• 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

Top down parsing Bottom up parsing (Shift reduce)

Operator precedence

Parsing without LR (k) parsing


Back backtracking
tracking LL – Left to Right &
Left Most Derivation

LR(0) LR (1) LR – Left to Right &


Recursive LL(1) Right Most Derivation
descent /Predictiv
e Parser

LR(0) SLR LALR CLR


Backtracking
• In backtracking, expansion of nonterminal symbol we choose one
alternative and if any mismatch occurs then we try another
alternative.
• Grammar:
• S→ cAd
• A→ ab | a

• 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→ cAd
• A→ ab | a c A d

• 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→ cAd
• A→ ab | a c A d c A d
Make prediction

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

a b Backtrack a Parsing done


• Input string: cad
Question
• E→ 5+T | 3-T
• T→ V | V*V | V+V V→ a | b
• String: 3-a+b
Left Factoring

• At times, it is not clear which out of 2 (or more) productions to use to


expand a non terminal because multiple productions begin with same
lookahead.
• A->aa Input String: ac
• A->ab
• A->ac
• A Grammar with left factoring present is a NON DETERMINISTIC Grammar.
• Top Down Parser will not work with grammar having Left Factoring.
• Removing Left Factoring:
Solution:
• 𝐴 → 𝛼𝛽1 |𝛼𝛽2 𝐴 → 𝛼𝐴′
A->aA’
𝐴′ → 𝛽1 |𝛽2
A’->a|b|c
Left Factoring
Example: Left Factoring
• stmt->if expr then stmt else stmt|if expr then stmt
Example: Left Factoring
• stmt->if expr then stmt else stmt|if expr then stmt

• 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’

• Remove Left recursion if it is there


• Then Find out FIRST & FOLLOW
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| ∈

• First (S) = {a, b, ∈}


• FOLLOW(S) = {b, a, $}
Example for LL(1) Table
• S->aSbS|bSaS| ∈

• First (S) = {a, b, ∈}


• FOLLOW(S) = {b, a, $}

1. S->aSbS
a b $
2. S->bSaS
3. S->∈ S
Example for LL(1) Table
• S->aSbS|bSaS| ∈

• First (S) = {a, b, ∈}


• FOLLOW(S) = {b, a, $}

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| ∈

• First (S) = {a, b, ∈} Given Grammar is not LL(1) Grammar


• FOLLOW(S) = {b, a, $}

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) Parse Table


LL(1) Parser
Left to Right Parser
Input Buffer Input String: abab$
a b a b $

LL(1) Parsing
Output
$ Algorithm

Parse Stack

LL(1) Parse Table


a b $ Input Grammar:
S S->AA S->AA S->AA
A A->aA A->b A->aA|b
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
$Ab bab$
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
$Ab bab$ Pop b
$A ab$
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
$Ab bab$ Pop b
$A ab$ A->aA
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
$Ab bab$ Pop b
$A ab$ A->aA
$Aa ab$
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
$Ab bab$ Pop b
$A ab$ A->aA
$Aa ab$ Pop a
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
$Ab bab$ Pop b
$A ab$ A->aA
$Aa ab$ Pop a
$A b$
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
$Ab bab$ Pop b
$A ab$ A->aA
$Aa ab$ Pop a
$A b$ A->b
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
$Ab bab$ Pop b
$A ab$ A->aA
$Aa ab$ Pop a
$A b$ A->b
$b b$
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
$Ab bab$ Pop b
$A ab$ A->aA
$Aa ab$ Pop a
$A b$ A->b
$b b$ Pop b
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
$Ab bab$ Pop b
$A ab$ A->aA
$Aa ab$ Pop a
$A b$ A->b
$b b$ Pop b
$ $
a b $
Input Grammar:

LL(1) Parser S->AA


A->aA|b
S
A
S->AA
A->aA
S->AA
A->b

Left to Right Parser Input String: abab$


Input Buffer
Stack Input Action
a b a b $
$ abab$ Push S into Stack
$S abab$ S->AA
$AA abab$ A->aA
$AAa abab$ Pop a
$AA bab$ A->b
$Ab bab$ Pop b
$A ab$ A->aA
$Aa ab$ Pop a
$A b$ A->b
$b b$ Pop b
$ $ Accept
Example for LL(1)
• S -> aABb
• A -> c | ∈
• B -> d | ∈
Example for LL(1)
• S -> aABb
• A -> c | ∈
• B -> d | ∈

• 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->∈

• S -> aABb B 5. B->∈ 4. B->d


• A -> c | ∈
Example String: acdb$
• B -> d | ∈
• FIRST(S) = {a} Stack Input Action
• FIRST(A) = {c, ∈}
$ acdb$ Push S into Stack
• FIRST(B) = {d, ∈}
• FOLLOW(S) = {$} $S acdb$ 1. S->aABb
• FOLLOW(A) = {d, b} $bBAa acdb$ Pop a
• FOLLOW(B) = {b}
$bBA cdb$ 2. A->c
1. S -> aABb
2. A -> c $bBc cdb$ Pop c
3. A -> ∈ $bB db$ B->d
4. B -> d $bd db$ Pop d
5. B -> ∈
$b b$ Pop b
$ $ Accept
Example
• E->E+T | T
• T->T*F | F
• F->(E) | id
Example
• E->E+T | T
• T->T*F | F
• F->(E) | id

• Remove Left Recursion:


• E->TE‘
• E‘->+TE‘| ∈
• T->FT’
• T’->*FT’| ∈
• F->(E)|id
Example
NT FIRST FOLLOW
• E->E+T | T E
• T->T*F | F E’
• F->(E) | id T
T’
• Remove Left Recursion: F
• E->TE‘
• E‘->+TE‘| ∈
• T->FT’
• T’->*FT’| ∈
• F->(E)|id
Example
NT FIRST FOLLOW
• E->E+T | T E {(,id}
• T->T*F | F E’ {+, ∈}
• F->(E) | id T {(,id}
T’ {*, ∈}
• Remove Left Recursion: F {(,id}
• E->TE‘
• E‘->+TE‘| ∈
• T->FT’
• T’->*FT’| ∈
• F->(E)|id
Example
NT FIRST FOLLOW
• E->E+T | T E {(,id} {$, )}
• T->T*F | F E’ {+, ∈} {$, )}
• F->(E) | id T {(,id} {+,$,)}
T’ {*, ∈} {+,$,)}
• Remove Left Recursion: F {(,id} {*, +,$,)}
• E->TE‘
• E‘->+TE‘| ∈
• T->FT’
• T’->*FT’| ∈
• F->(E)|id
NT FIRST FOLLOW
Example E {(,id} {$, )}
E’ {+, ∈} {$, )}
• E->E+T | T T {(,id} {+,$,)}
• T->T*F | F T’ {*, ∈} {+,$,)}
• F->(E) | id F {(,id} {*, +,$,)}

• Remove Left Recursion:


• E->TE‘ id + * ( ) $
• E‘->+TE‘| ∈ E E->TE’ E->TE’
• T->FT’
E’ E’->+TE’ E’->∈ E’->∈
• T’->*FT’| ∈
T T->FT’ T->FT’
• F->(E)|id
T’ T’->∈ T’->*FT’ T’->∈ T’->∈

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

• Remove Left $E id+id*id$


Recursion:
• E->TE‘
• E‘->+TE‘| ∈
• T->FT’
• T’->*FT’| ∈
• F->(E)|id
Select()
• SELECT(A → α) = FIRST(α) if α is not nullable
• SELECT(A → α) = FIRST(α) U FOLLOW(A) if α is nullable
Recursive descent parser
• A recursive descent parser is a top down parser built from a set of
mutually recursive procedures (or a non recursive equivalent) where
each such procedure implements one of the non-terminals of the
grammar. Thus the structure of the resulting program closely mirrors
that of the grammar it recognizes.
Recursive descent parser
Recursive descent parser
Recursive descent parser
Recursive descent parser
• E->iE’
• E’->+iE’| ∈
Recursive descent parser
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;
}
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(){

Example-Recursive descent parser F();


TPrime();
}
E(){ TPrime(){
T(); if(look_ahead == '*'){
• E->TE’ EPrime(); input++;
} F();
• E’->+TE’|∈ TPrime();
EPrime(){
• T->FT’ if(look_ahead == '+'){ }
look_ahead++; else
• T’->*FT’|∈ T(); return;
EPrime();
• F->(E)|id F(){
if(look_ahead == '('){
}

} 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(){

Example-Recursive descent parser F();


TPrime();
E(){ }
T(); TPrime(){
EPrime(); if(look_ahead == '*'){
• E->TE’ } Input String: id+id$ input++;
F();
• E’->+TE’|∈ EPrime(){ TPrime();

• T->FT’ if(look_ahead == '+'){


look_ahead++; }
else
• T’->*FT’|∈ T();
EPrime(); return;
F(){
• F->(E)|id if(look_ahead == '('){
}
} 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(){

Example-Recursive descent parser F();


TPrime();
}
E(){ TPrime(){
T(); if(look_ahead == '*'){
• E->TE’ EPrime(); Input String: id+id*id$ input++;
} F();
• E’->+TE’|∈ TPrime();
EPrime(){
• T->FT’ if(look_ahead == '+'){ }
look_ahead++; else
• T’->*FT’|∈ T(); return;
EPrime();
• F->(E)|id F(){
if(look_ahead == '('){
}

} 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

• Verify acceptability of below String:


• (a,(a,a))
• (a,((a,a),(a,a))
Example-Recursive descent parser
• S->(L)|a
• L->L,S|S

• Eliminate Left Recursion


Example-Recursive descent parser
• S->(L)|a
• L->L,S|S

• Eliminate Left Recursion


• S->(L)|a
• L->SL’
• L’->,SL’| ∈
Example-Recursive descent parser
• S->(L)|a
• L->SL’
• L’->,SL’| ∈

• Verify acceptability of below String:


• (a,(a,a))
• (a,((a,a),(a,a))
Example-Recursive descent parser
S(){
• S->(L)|a if(look_ahead == '('){
look_ahead++;
void main(){
S();
• L->SL’ L(); if(look_ahead == '$')
if(look_ahead == ")") String is Accepted
• L’->,SL’| ∈ look_ahead++; else
else String is not Accepted
error(); }
}
else if(look_ahead == 'a') LPrime(){
look_ahead++; if(look_ahead == ','){
else look_ahead++;
error(); S();
} LPrime();
L(){
S(); }
LPrime(); else
} return;
}
Example-Recursive descent parser
S(){
Input String: (a,(a,a))$
• S->(L)|a if(look_ahead == '('){
look_ahead++;
void main(){
S();
• L->SL’ L(); if(look_ahead == '$')
if(look_ahead == ")") String is Accepted
• L’->,SL’| ∈ look_ahead++; else
else String is not Accepted
error(); }
}
else if(look_ahead == 'a') LPrime(){
look_ahead++; if(look_ahead == ','){
else look_ahead++;
error(); S();
} LPrime();
L(){
S(); }
LPrime(); else
} return;
}
Example-Recursive descent parser
Input String: (a,((a,a),(a,a))$
S(){
• S->(L)|a if(look_ahead == '('){
look_ahead++;
void main(){
S();
• L->SL’ L(); if(look_ahead == '$')
if(look_ahead == ")") String is Accepted
• L’->,SL’| ∈ look_ahead++; else
else String is not Accepted
error(); }
}
else if(look_ahead == 'a') LPrime(){
look_ahead++; if(look_ahead == ','){
else look_ahead++;
error(); S();
} LPrime();
L(){
S(); }
LPrime(); else
} return;
}
Example
• S->aAB|bB
• A->aA|b
• B->b
Example
• S → rXd | rZd
• X → oa | ea
• Z → ai
Example
• S->Aa
• A->BD
• B->b|∈
• D->d|∈
Example
• S-> A
• A -> BC | x
• B -> t | ε
• C -> v | ε
Example
• S-> A
• A -> BC | x
• B -> t
• C -> v
•E→TX
•X→+E|ε
• T → ( E ) | int Y
•Y→*T|ε
Example

Input String: 4 + 29 DIV 3


Bottom up Parser
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Operator precedence

Parsing without LR (k) parsing


Back backtracking
tracking LL – Left to Right &
Left Most Derivation

LR(0) LR (1) LR – Left to Right &


Recursive LL(1) Right Most Derivation
descent /Predictiv
e Parser

LR(0) SLR LALR CLR


Bottom up Parser (Shift Reduce Parser)
• It is the process of reducing the input string to start symbol i.e. the
parse tree is constructed in from leaves to the root (bottom to top)
• It is also known as shift reduce Parsing.
• Shift means push into stack
• Reduce means pop from stack
• Also called as LR Parser
Shift Reduce Parser
• Left to Right Scanning
• Right Most derivation
• E->E+E|E*E|id
• Input String: id*id+id
• At each reduction step, a particular substring matching the right side
of a production is replaced by the symbol on the left of that
production and if the substring is chosen correctly at each step a right
most derivation is traced in reverse.
Handle
• S->aABe
• A->Abc|b
• B->d
• Handles: A handle of a string is a substring that matches the right side of a
production and whose reduction to the non terminal on the left side of the
production represents one step along the reverse of a rightmost derivation.
• Is left most substring always handle? No, choosing the left most substring
as the handle always, may not give correct SR parsing.
• A handle of a right sentential form Y is a production A->B and a position of
Y where the string B may be found and replaced by A to produce the
previous right sentential form in a rightmost derivation of Y.
• Example String: abbcde
• S->aABe
• A->Abc|b
• B->d
• Example String: abbcde
• abbcde: Y=abbcde, A->b, Handle=b
• aAbcde: Y=aAbcde, A->Abc, Handle=Abc
• aAde: Y=aAde, B->d, Handle=d
• aABe: Y=aABe, S->aABe, Handle = aABe
•S
Handle the Pruning
• Removing the children of Left Hand side non terminal from the parse
tree is called as Handle Pruning.
• A rightmost derivation in reverse can be obtained by Handle Pruning.
• Steps to follow:
• Start with a string of terminals ‘w’ that is to be parsed.
• Let w = Yn, where Yn is the nth right sentential form of an unknown
RMD.
• To reconstruct the RMD in reverse, locate handle Bn in Yn; Replace Bn
by LHS by some An->Bn to get (n-1) th RSF Yn-1, Repeat.
• S=>Y0=>Y1=>…=>Yn-1=>Yn
Example of Handle Pruning
• E->E+E|E*E|id
Right Sentential Form(RSF) Handle Reducing Production
Id1+id2*id3 Id1 E->id
E+id2*id3 Id2 E->id
E+E*id3 Id3 E->id
E+E*E E+E E->E+E
E*E E*E E->E*E
E
• S->aA
• A->bc
• Input: abc$
SR Parser
Input Buffer
a b a b $

SR Parsing
Output
$ Algorithm

Parse Stack

Terminals Non Terminals

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)

Left to Right No. of LA symbols


Reverse of RMD
Classification of LR Parser
• LR(0)
• SLR(1): Simple LR
• CLR(1): Canonical LR
• LALR(1): Look Ahead LR
Components of LR Parser
Input Buffer
a b a b $

LR Parsing
Output
$ Algorithm

Parse Stack

Action Goto Parsing Table


Behavior of LR Parser
• Parsing algorithm reads the next unread input character from the
Input Buffer
• Parsing algorithm also reads the character on the top of the stack.
• A stack can have grammar symbol (Xi) or state symbol (Si)
• Combination of input character and top of stack char is used to index
parsing table
• Parsing action can be: (1) Shift (2) Reduce (3) Error (4) Accept
• Goto function takes a state and a grammar symbol and produces a
state.
General Procedure to construct LR Parse
Table
1. Construct the augmented grammar
2. Create canonical Collection of LR item or items of compiler
3. Draw the DFA using sets of LR items
4. Prepare the LR parse table based on LR items
• Note:
• Any grammar for which we construct the LR(k) parser is called LR(k) grammar
• LR(k) grammar is accepted by DPDA
• The language generated by LR(k) grammar is DCFL
Augmented Grammar
• If G is a grammar with start symbol S, then G’ (Augmented Grammar) contains the production
from G along with a new production 𝑆’ → 𝑆 where S’ is new start symbol of G’.
• The grammar which is obtained by adding one more production is called as augmented grammar.
• Example:
• S->AB
• A->a
• B->b Why Required?
• Augmented Grammar:
• S’->S It indicates that parser should stop
• S->AB parsing and announce acceptable when
it is about to reduce 𝑆’ → 𝑆
• A->a
• B->b
LR(0) items
• The Production which has a dot (.) any where on R.H.S. is
called as LR(0) items
• Ex. A->abc
• Item indicates how much part of a production we have
seen at a given point in parsing process.
• LR(0)items: A -> .abc
𝐴→𝜖 A -> a.bc Non Final (Shift)
A -> ab.c
A -> abc. Final (Reduce)
𝐴 →∙
Handle
Canonical Collection
• If 𝐼0 , 𝐼1 , 𝐼2 , … , 𝐼𝑘 be the set containing LR(0) items so then the set 𝐼 =
{𝐼0 , 𝐼1 , … , 𝐼𝑘 } this called canonical collection.
• The Function is used to generate LR(0) items:
• Closure(𝐼) where, 𝐼 = 𝐼𝑡𝑒𝑚
• Goto(𝐼, 𝑥) where, x is Grammar Symbol
Closure(𝐼)
Set of Items (𝐼𝑖 ) Set of Items (𝐼𝑗 ) 𝐼
Closure 𝐴 → 𝛼 ∙ 𝐵𝛽
1. Add everything from input to output B →∙ 𝛾
2. If 𝐴 → 𝛼 ∙ 𝐵𝛽 is in 𝐼 and B → 𝛾 is in the grammar G then add
B →∙ 𝛾 to the Closure (𝐼). Where B is non terminal.
3. Repeat the step(2) for every newly added item.
Given Grammar Augmented Grammar 𝐼0 : 𝐶𝑙𝑜𝑠𝑢𝑟𝑒(𝐴′ → 𝐴) 𝐼0
𝐴 → 𝑎𝐴 𝐴′→𝐴 𝐴′ →∙ 𝐴
𝐴→𝑏 𝐴 → 𝑎𝐴 𝐴 →∙ 𝑎𝐴
𝐴→𝑏 A →∙ 𝑏
Closure(𝐼)
• Compute CLOSURE whenever there is a dot to the immediate left of a Non-
Terminal(NT) and the NT has not yet been expanded.
• Expansion of such NT into items with dot at extreme left is called CLOSURE.
• STEPS:
• Construct the Augmented Grammar
• Construct set 𝐼 of LR(0) items of augmented Grammar.
• For each item that has dot to the immediate left of a non terminal expand
the Set 𝐼 by including items formed from this NT; including only those items
with dot at extreme left.
• Repeat until new items are added.
𝐺𝑜𝑡𝑜(𝐼, 𝑥)
• 𝐺𝑜𝑡𝑜(𝐼, 𝑥) is the closure of 𝐴 → 𝛼𝑥 ∙ 𝛽 such that 𝐴 → 𝛼 ∙ 𝑥𝛽
is in 𝐼.
1 𝐴 →∙ 𝑋𝑌
• Example,
𝑋
𝐼0 𝐼1 2 𝐴→𝑋∙𝑌
𝑥
𝐴 → 𝛼 ∙ 𝑥𝛽 𝐴 → 𝛼𝑥 ∙ 𝛽
𝑋
3 𝐴 → 𝑋𝑌 ∙
Goto Function Example
Example Grammar:
𝐴 → 𝑎𝐴
𝐴→𝑏
Goto Function Example
Example Grammar: Augmented Grammar:
𝐴 → 𝑎𝐴 𝐴′ → 𝐴
𝐴→𝑏 𝐴 → 𝑎𝐴
𝐴→𝑏
Goto Function Example
Example Grammar: Augmented Grammar: 𝑰𝟎 : Closure(𝑨′ → 𝑨)
𝐴 → 𝑎𝐴 𝐴′ → 𝐴
𝐴→𝑏 𝐴 → 𝑎𝐴 𝐴′ →∙ 𝐴
𝐴 →∙ 𝑎𝐴
𝐴→𝑏
𝐴 →∙ 𝑏
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(𝑰𝟎 ,𝑨)
𝐴 → 𝑎𝐴 𝐴′ → 𝐴
𝐴→𝑏 𝐴 → 𝑎𝐴 𝐴′ →∙ 𝐴
𝐴→𝑏
𝐴 →∙ 𝑎𝐴 𝐴′ → 𝐴 ∙
𝐴 →∙ 𝑏

𝑰𝟐 : Goto(𝑰𝟎 ,𝒂) 𝑰𝟑 : Goto(𝑰𝟎 ,𝒃)

𝐴 →𝑎∙𝐴
𝐴 →∙ 𝑎𝐴 𝐴→𝑏∙
𝐴 →∙ 𝑏
Goto Function Example
Example Grammar: Augmented Grammar: 𝑰𝟎 : Closure(𝑨′ → 𝑨) 𝑰𝟏 : Goto(𝑰𝟎 ,𝑨)
𝐴 → 𝑎𝐴 𝐴′ → 𝐴
𝐴→𝑏 𝐴 → 𝑎𝐴 𝐴′ →∙ 𝐴
𝐴→𝑏
𝐴 →∙ 𝑎𝐴 𝐴′ → 𝐴 ∙
𝐴 →∙ 𝑏

𝑰𝟐 : Goto(𝑰𝟎 ,𝒂) 𝑰𝟑 : Goto(𝑰𝟎 ,𝒃) 𝑰𝟒 : Goto(𝑰𝟐 ,𝑨) 𝑰? : Goto(𝑰𝟐 ,𝒂)

𝐴 →𝑎∙𝐴 ?
𝐴 →∙ 𝑎𝐴 𝐴→𝑏∙ 𝐴 → 𝑎𝐴 ∙
𝐴 →∙ 𝑏
𝑰? : Goto(𝑰𝟐 ,𝒃)

?
Goto Function Example
Example Grammar: Augmented Grammar: 𝑰𝟎 : Closure(𝑨′ → 𝑨) 𝑰𝟏 : Goto(𝑰𝟎 ,𝑨)
𝐴 → 𝑎𝐴 𝐴′ → 𝐴
𝐴→𝑏 𝐴 → 𝑎𝐴 𝐴′ →∙ 𝐴
𝐴→𝑏
𝐴 →∙ 𝑎𝐴 𝐴′ → 𝐴 ∙
𝐴 →∙ 𝑏

𝑰𝟐 : Goto(𝑰𝟎 ,𝒂) 𝑰𝟑 : Goto(𝑰𝟎 ,𝒃) 𝑰𝟒 : Goto(𝑰𝟐 ,𝑨) Goto(𝑰𝟐 ,𝒂) = 𝑰𝟐

𝐴 →𝑎∙𝐴 Goto(𝑰𝟐 ,𝒃)= 𝑰𝟑


𝐴 →∙ 𝑎𝐴 𝐴→𝑏∙ 𝐴 → 𝑎𝐴 ∙
𝐴 →∙ 𝑏
Canonical Collection
Example Grammar: Augmented Grammar: 𝑰𝟎 : Closure(𝑨′ → 𝑨) 𝑰𝟏 : Goto(𝑰𝟎 ,𝑨)
𝐴 → 𝑎𝐴 𝐴′ → 𝐴
𝐴→𝑏 𝐴 → 𝑎𝐴 𝐴′ →∙ 𝐴
𝐴→𝑏
𝐴 →∙ 𝑎𝐴 𝐴′ → 𝐴 ∙
𝐴 →∙ 𝑏

𝑰𝟐 : Goto(𝑰𝟎 ,𝒂) 𝑰𝟑 : Goto(𝑰𝟎 ,𝒃) 𝑰𝟒 : Goto(𝑰𝟐 ,𝑨) Goto(𝑰𝟐 ,𝒂) = 𝑰𝟐

𝐴 →𝑎∙𝐴 Goto(𝑰𝟐 ,𝒃)= 𝑰𝟑


𝐴 →∙ 𝑎𝐴 𝐴→𝑏∙ 𝐴 → 𝑎𝐴 ∙
𝐴 →∙ 𝑏

Canonical Collection 𝑪 = {𝑰𝟎 , 𝑰𝟏 , 𝑰𝟐 , 𝑰𝟑 , 𝑰𝟒 }


Construction of DFA 𝐼4
𝐼1
𝐴 → 𝑎𝐴 ∙
𝐴′ → 𝐴 ∙

𝐴
𝐼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
• 𝐸 → 𝑇 + 𝐸|𝑇
• 𝑇 → 𝑇 ∗ 𝐹|𝐹
• 𝐹 → 𝑖𝑑

You might also like