Professional Documents
Culture Documents
Semester 2022-2023-II
𝑎 𝐴 𝐵 𝑒 𝑎 𝐴 𝐵 𝑒 𝑎 𝐴 𝐵 𝑒 𝑎 𝐴 𝐵 𝑒
CSE, IIT Kanpur
𝑑 𝐴 𝑏 𝑐 𝑑 𝐴 𝑏 𝑐 𝑑
Content influenced by many excellent references, see References slide for acknowledgements.
𝑏
CS 335 Swarnendu Biswas
Bottom-up Parsing
Bottom-up Parsing 𝑆 → 𝑎𝐴𝐵𝑒 Input string: 𝑎𝑏𝑏𝑐𝑑𝑒
𝐴 → 𝐴𝑏𝑐 | 𝑏 𝑎𝑏𝑏𝑐𝑑𝑒
Constructs the parse tree starting from the leaves and working up 𝐵→𝑑 → 𝑎𝐴𝑏𝑐𝑑𝑒
toward the root → 𝑎𝐴𝑑𝑒
→ 𝑎𝐴𝐵𝑒
𝑆 → 𝑎𝐴𝐵𝑒 Input string: 𝑎𝑏𝑏𝑐𝑑𝑒 →𝑆
𝐴 → 𝐴𝑏𝑐 | 𝑏 𝑆 → 𝑎𝐴𝐵𝑒 𝑎𝑏𝑏𝑐𝑑𝑒 𝑎𝑏𝑏𝑐𝑑𝑒 ⇒ 𝑎 𝐴 𝑏 𝑐 𝑑 𝑒 ⇒ 𝑎 𝐴 𝑑 𝑒 ⇒ 𝑎 𝐴 𝐵 𝑒 ⇒ 𝑆
𝐵→𝑑
→ 𝑎𝐴𝑑𝑒 → 𝑎𝐴𝑏𝑐𝑑𝑒
𝑏 𝐴 𝑏 𝑐 𝐴 𝑏 𝑐 𝑑 𝑎 𝐴 𝐵 𝑒
→ 𝑎𝐴𝑏𝑐𝑑𝑒 → 𝑎𝐴𝑑𝑒
→ 𝑎𝑏𝑏𝑐𝑑𝑒 → 𝑎𝐴𝐵𝑒 𝑏 𝑏 𝐴 𝑏 𝑐 𝑑
→𝑆 reverse of
rightmost 𝑏
CS 335 Swarnendu Biswas
derivation CS 335 Swarnendu Biswas
Reduction Handle
• Handle is a substring that matches the body of a production
• Bottom-up parsing reduces a string 𝑤 to the start symbol 𝑆 • Reducing the handle is one step in the reverse of the rightmost derivation
• At each reduction step, a chosen substring that is the RHS (or body) of a
production is replaced by the LHS (or head) nonterminal Right Sentential Form Handle Reducing Production
𝐸 →𝐸+𝑇|𝑇
id1 ∗ id2 id1 𝐹 → id
Rightmost derivation
𝑇 →𝑇∗𝐹|𝐹
𝐹 ∗ id2 𝐹 𝑇→𝐹
𝐹 → 𝐸 | id
𝑇 ∗ id2 id2 𝐹 → id
𝑆 𝛾0 𝛾1 𝛾2 … 𝛾𝑛−1 𝛾𝑛 = 𝑤 𝑇∗𝐹 𝑇∗𝐹 𝑇 →𝑇∗𝐹
𝑟𝑚 𝑟𝑚 𝑟𝑚 𝑟𝑚 𝑟𝑚 𝑟𝑚
𝑇 𝑇 𝐸→𝑇
Bottom-up Parser
Although 𝑇 is the body of the production 𝐸 → 𝑇, 𝑇 is not a handle in the sentential form 𝑇 ∗ id2 .
The leftmost substring that matches the body of some production need not be a handle.
Handle Handle
• If 𝑆 ∗ 𝛼𝐴𝑤 𝛼𝛽𝑤, then 𝐴 →
𝑟𝑚 𝑟𝑚 𝑆
𝛽 is a handle of 𝛼𝛽𝑤 If grammar 𝐺 is unambiguous, then every right sentential form has
only one handle
• String 𝑤 right of a handle must 𝐴
contain only terminals 𝛼 𝛽 𝑤
If 𝐺 is ambiguous, then there can be more than one rightmost
A handle 𝐴 → 𝛽 in the parse tree for derivation of 𝛼𝛽𝑤
𝛼𝛽𝑤
Shift Reduce
∗
$𝐹 ∗ 𝐢𝐝2 $ Reduce by 𝑇 → 𝐹
$𝑇 ∗ 𝐢𝐝2 $ Shift
$𝑇 ∗ 𝐢𝐝2 $ Shift
• Final goal Stack Input
$𝑇 ∗ 𝐢𝐝2 $ Reduce by 𝐹 → id
$𝑆 $
$𝑇 ∗ 𝐹 $ Reduce by 𝑇 → 𝑇 ∗ 𝐹
$𝑇 $ Reduce by 𝐸 → 𝑇
$𝐸 $ Accept Or report an error in
case of a syntax error
CS 335 Swarnendu Biswas CS 335 Swarnendu Biswas
Handle on Top of the Stack Possible Choices in Rightmost Derivation
• Is the following scenario possible? 1. 𝑆
𝑟𝑚
𝛼𝐴𝑧
𝑟𝑚
𝛼𝛽𝐵𝑦𝑧
𝑟𝑚
𝛼𝛽𝛾𝑦𝑧 2. 𝑆
𝑟𝑚
𝛼𝐵𝑥𝐴𝑧
𝑟𝑚
𝛼𝐵𝑥𝑦𝑧
𝑟𝑚
𝛼𝛾𝑥𝑦𝑧
$𝛼𝐵𝐴 𝑤$
… 𝐵 𝐴
𝐵
𝛼 𝛾 𝑥 𝑦 𝑧
𝛼 𝛽 𝛾 𝑦 𝑧
𝐸 → 𝐸 + 𝐸 𝐸 ∗ 𝐸 id
Shift-Reduce Conflict Shift-Reduce Conflict
id + id ∗ id id + id ∗ id S𝑡𝑚𝑡 → if 𝐸𝑥𝑝𝑟 then 𝑆𝑡𝑚𝑡
| if 𝐸𝑥𝑝𝑟 then 𝑆𝑡𝑚𝑡 else 𝑆𝑡𝑚𝑡
Stack Input Action Stack Input Action
$ id + id ∗ id$ Shift $ id + id ∗ id$ Shift | 𝑜𝑡ℎ𝑒𝑟
… …
$𝐸 + 𝐸 ∗ id$ Reduce by 𝐸 → 𝐸 + 𝐸 $𝐸 + 𝐸 ∗ id$ Shift Stack Input Action
$𝐸 ∗ id$ Shift $𝐸 + 𝐸 ∗ id$ Shift
$𝐸 ∗ id$ Shift $𝐸 + 𝐸 ∗ id $ Reduce by 𝐸 → id … if 𝐸𝑥𝑝𝑟 then 𝑆𝑡𝑚𝑡 else … $
$𝐸 ∗ id $ Reduce by 𝐸 → id $𝐸 + 𝐸 ∗ 𝐸 $ Reduce by 𝐸 → 𝐸 ∗ 𝐸
$𝐸 ∗ 𝐸 $ Reduce by 𝐸 → 𝐸 ∗ 𝐸 $𝐸 + 𝐸 $ Reduce by 𝐸 → 𝐸 + 𝐸
What is a correct thing to do for
$𝐸 $ $𝐸 $
this grammar – shift or reduce?
E.g., we can prioritize shifts.
CS 335 Swarnendu Biswas CS 335 Swarnendu Biswas
𝑀 →𝑅+𝑅 𝑅+𝑐 𝑅
Reduce-Reduce Conflict 𝑅→𝑐
𝑐+𝑐 𝑐+𝑐
The LR parsing driver is the same for all LR parsers, only the parsing
table (including ACTION and GOTO) changes across parser types
𝐸′ → 𝐸
LR(0) Item Production Items Closure Operation 𝐸 →𝐸+𝑇|𝑇
𝐴 → •𝑋𝑌𝑍 𝑇 →𝑇∗𝐹|𝐹
• An LR(0) item (also called 𝐴 → 𝑋•𝑌𝑍 • Let 𝐼 be a set of items for a 𝐹 → 𝐸 | id
item) of a grammar 𝐺 is a 𝐴 → 𝑋𝑌𝑍 grammar 𝐺
𝐴 → 𝑋𝑌•𝑍 Suppose 𝐼 = {𝐸 ′ → •𝐸 }
production of 𝐺 with a dot at • Closure(𝐼) is constructed as
𝐴 → 𝑋𝑌𝑍• Closure(𝐼) = {
some position in the body
follows: 𝐸 ′ → •𝐸,
• 𝐴 → •𝑋𝑌𝑍 indicates that we expect a 𝐸 → •𝐸 + 𝑇,
string derivable from 𝑋𝑌𝑍 next in the 1. Add every item in 𝐼 to Closure(𝐼)
• An item indicates how much input 2. If 𝐴 → 𝛼•𝐵𝛽 is in Closure(𝐼) and 𝐸 → •𝑇,
of a production we have seen • 𝐴 → 𝑋•𝑌𝑍 indicates that we saw a 𝐵 → 𝛾 is a rule, then add 𝐵 → •𝛾 𝑇 → •𝑇 ∗ 𝐹,
• Symbols on the left of “•” are string derivable from 𝑋 in the input, to Closure(𝐼) if not already 𝑇 → •𝐹,
already on the stack and we expect a string derivable from added 𝐹→• 𝐸 ,
• Symbols on the right of “•” are 𝑌𝑍 next in the input 3. Repeat until no more new items 𝐹 → •id
expected in the input • 𝐴 → ϵ generates only one item 𝐴 → • can be added to Closure(𝐼) }
𝐸′ → 𝐸
Goto(𝐼, +) = { 𝐶 = Closure({𝑆 ′ → •𝑆})
𝐸 →𝐸+𝑇|𝑇 𝐸 → 𝐸 + •𝑇, repeat
𝑇 →𝑇∗𝐹|𝐹 𝑇 → •𝑇 ∗ 𝐹, for each set of items 𝐼 in 𝐶
𝐹 → 𝐸 | id 𝑇 → •𝐹, for each grammar symbol 𝑋
𝐹→• 𝐸 ,
if Goto(𝐼, 𝑋) is not empty and not in 𝐶
Suppose 𝐼 = { 𝐹 → •id
add Goto(𝐼, 𝑋) to 𝐶
𝐸 ′ → 𝐸•, }
𝐸 → 𝐸• + 𝑇 until no new sets of items are added to 𝐶
}
𝐸′ → 𝐸
• Compute the canonical 𝐼0 = Closure({𝐸 ′ → •𝐸})= { 𝐼2 = Goto(𝐼0 , 𝑇)= { 𝐼4 = Goto(𝐼0 , "(")= {
𝐸 ′ → •𝐸, 𝐸 → 𝑇•, 𝐹 → (•𝐸),
𝐸 →𝐸+𝑇|𝑇
collection for the expression 𝐸 → •𝐸 + 𝑇, 𝑇 → 𝑇• ∗ 𝐹 𝐸 → •𝐸 + 𝑇,
𝑇 →𝑇∗𝐹|𝐹 grammar 𝐸 → •𝑇, } 𝐸 → •𝑇,
𝑇 → •𝑇 ∗ 𝐹, 𝑇 → •𝑇 ∗ 𝐹,
𝐹 → 𝐸 | id 𝑇 → •𝐹, 𝐼3 = Goto(𝐼0 , 𝐹)= { 𝑇 → •𝐹,
𝐹 → •(𝐸), 𝑇 → 𝐹• 𝐹 → •(𝐸),
𝐹 → •id, } 𝐹 → •id,
} }
𝐼5 = Goto(𝐼0 , id)= {
𝐼1 = Goto(𝐼0 , 𝐸) = { 𝐹 → id• 𝐼7 = Goto(𝐼2 ,∗) = {
𝐸 ′ → 𝐸•, } 𝑇 → 𝑇 ∗ •𝐹 ,
𝐸 → 𝐸• + 𝑇 𝐹 → •(𝐸),
} 𝐹 → •id
}
𝐼8 𝐼9 𝐼10 𝐼11
)
CS 335 Swarnendu Biswas CS 335 Swarnendu Biswas
• Not all prefixes of a right sentential form can appear on the stack 𝑋1 𝑋2 𝑋3 can never appear on the stack
• A viable prefix is a prefix of a right sentential form that can appear on
the stack of a shift-reduce parser Suppose there is a production 𝐴 → 𝛽1 𝛽2 , and 𝛼𝛽1 is on the stack.
• 𝛼 is a viable prefix if ∃𝑤 such that 𝛼𝑤 is a right sentential form • 𝛽2 ≠ 𝜖 implies the handle 𝛽1 𝛽2 is not at the top of the stack yet, so shift
• There is no error as long as the parser has viable prefixes on the stack • 𝛽2 = 𝜖 implies then the parser can reduce by the handle 𝐴 → 𝛽1
{ { 𝑆 →𝑆+𝐸|𝐸 𝑆 →𝐸+𝑆|𝐸
{
𝐿 → 𝐿, 𝑆• 𝐿 → 𝑆, 𝐿• 𝐸 → num 𝐸 → num
𝐿 → 𝐿, 𝑆•
𝑆 → 𝑆•, 𝐿 𝐿 → 𝑆•
} Left associative Right associative
} }
Ok Shift-reduce conflict Reduce-reduce conflict
Not 𝑆 → 𝐸• + 𝑆
𝑆 → 𝐸•
• Takes shift/reduce decisions without any lookahead token LR(0) Shift-reduce conflict
• Lacks the power to parse programming language grammars
CS 335 Swarnendu Biswas CS 335 Swarnendu Biswas
}
𝑆 ′ → 𝑆• 𝐼2 = Goto(𝐼0 , 𝐸)= {
𝑺 → 𝑬•+S,
Simple LR Parsing
𝑺 → E•
SLR(1)
Not }
LR(0)
LR(0) Automaton
Canonical Collection of Sets of LR(0) Items
$
Accept 𝐼1 𝑇 𝐼2 𝐹 𝐼3 𝐼5
𝐼6 = Goto(𝐼1 , +)= { 𝐼9 = Goto(𝐼6 , 𝑇)= { 𝐼2 = Goto(𝐼4 , 𝑇) 𝐹 id
𝐸 → 𝐸 + •𝑇, 𝐸 → 𝐸 + 𝑇•, 𝐼3 = Goto(𝐼4 , 𝐹) 𝐸 id id
𝑇 → •𝑇 ∗ 𝐹, 𝑇 → 𝑇• ∗ 𝐹 id
𝑇 → •𝐹, } 𝐼4 = Goto(𝐼4 , "(")
+ ∗
𝐹 → •(𝐸), 𝐼5 = Goto(𝐼4 , id)
𝐹 → •id, 𝐼10 = Goto(𝐼7 , 𝐹)= { (
𝐼3 = Goto(𝐼6 , 𝐹) 𝐼0 𝐼4 + 𝐼6 𝐼7
} 𝑇 → 𝑇 ∗ 𝐹•, ( ∗
} 𝐼4 = Goto(𝐼6 , "(") (
𝐼8 = Goto(𝐼4 , 𝐸)= { 𝐼5 = Goto(𝐼6 , id) 𝑇
( 𝐹
𝐸 → 𝐸• + 𝑇, 𝐼11 = Goto(𝐼8 , ")")= { 𝐼4 = Goto(𝐼7 , "(")
𝐹 → (𝐸•) 𝐹 → (𝐸)• 𝐸
𝐼5 = Goto(𝐼7 , id)
} }
𝐼6 = Goto(𝐼8 , +)
𝐼8 𝐼9 𝐼10 𝐼11
𝐼7 = Goto(𝐼9 ,∗)
)
CS 335 Swarnendu Biswas CS 335 Swarnendu Biswas
SLR Parsing Table
ACTION GOTO
LR Parser Configurations
State
id + ∗ ( ) $ 𝐸 𝑇 𝐹
0 𝑠5 𝑠4 1 2 3 • A LR parser configuration is a pair <𝑠0 , 𝑠1 ,…, 𝑠𝑚 , 𝑎𝑖 𝑎𝑖+1 … 𝑎𝑛 $>
1 𝑠6 𝑎𝑐𝑐 • Left half is stack content, and right half is the remaining input
2 𝑟2 𝑠7 𝑟2 𝑟2 • Configuration represents the right sentential form 𝑋1 𝑋2 … 𝑋𝑚 𝑎𝑖 𝑎𝑖+1 …
3 𝑟4 𝑟4 𝑟4 𝑟4
𝑎𝑛
4 𝑠5 𝑠4 8 2 3
5 𝑟6 𝑟6 𝑟6 𝑟6
6 𝑠5 𝑠4 9 3
7 𝑠5 𝑠4 10
8 𝑠6 𝑠11
9 𝑟1 𝑠7 𝑟1 𝑟1
10 𝑟3 𝑟3 𝑟3 𝑟3
11 𝑟5 𝑟5 𝑟5 𝑟5
CS 335 Swarnendu Biswas CS 335 Swarnendu Biswas
LR Parsing Program
LR Parsing Algorithm Let 𝑎 be the first symbol of input 𝑤$
while (1)
• If ACTION[𝑠𝑚 , 𝑎𝑖 ] = shift 𝑠, new configuration is <𝑠0 , 𝑠1 ,…, 𝑠𝑚 𝑠, 𝑎𝑖+1 … let 𝑠 be the top of the stack
𝑎𝑛 $> if ACTION[𝑎] == shift 𝑡
• If ACTION[𝑠𝑚 , 𝑎𝑖 ] = reduce 𝐴 → 𝛽, new configuration is <𝑠0 , 𝑠1 ,…, push 𝑡 onto the stack
let 𝑎 be the next input symbol
𝑠𝑚−𝑟 , 𝑎𝑖 𝑎𝑖+1 … 𝑎𝑛 $>, where 𝑟 = |𝛽| and 𝑠 = GOTO[𝑠𝑚−𝑟 , 𝐴] else if ACTION[𝑠, 𝑎] == reduce 𝐴 → 𝛽
• If ACTION[𝑠𝑚 , 𝑎𝑖 ] = accept, parsing is successful pop |𝛽| symbols off the stack
push GOTO[𝑡, 𝐴] onto the stack
• If ACTION[𝑠𝑚 , 𝑎𝑖 ] = error, parsing has discovered an error output production 𝐴 → 𝛽
else if ACTION[𝑠, 𝑎] == accept
break
else
invoke error recovery
8 02 𝑇 +id$ Reduce by 𝐸 → 𝑇
9 01 𝐸 +id$ Shift
0 item
id 5 [𝐴 → 𝛼•] and =id$ 𝑎 ∈ FOLLOW(𝐴)
Reduce by 𝐿 → id 0 id 5 =id$ Reduce by 𝐿 → id 0 id 5 =id$ Reduce by 𝐿 → id 0 id 5 =id$ Reduce by 𝐿 → id
SLR parsers cannot remember the left context
0𝐿2 =id$ Reduce by 𝑅 → 𝐿 0𝐿2 =id$ Shift 6 0𝐿2 =id$ Reduce by 𝑅 → 𝐿 0𝐿2 =id$ Shift 6
• SLR(1) states only tell us about the sequence on top of the stack, n
0 •𝑅 3Suppose 𝛽𝐴 is=id$
a viable
Error prefix on top
0 𝐿 2of
= the
6 stack id$ Shift 5 0𝑅3 =id$ Error 0𝐿2=6 id$ Shift 5
ot what is below on the stack
• There may be no right sentential form 6 id 5 𝑎 follows
0 𝐿 2 =where 𝛽𝐴 by 𝐿 → id
$ Reduce 0 𝐿 2 = 6 id 5 $ Reduce by 𝐿 → id
• Parser should not reduce by 𝐴 → 𝛼
0𝐿2=6𝐿8 $ Reduce by 𝑅 → 𝐿 0𝐿2=6𝐿8 $ Reduce by 𝑅 → 𝐿
LR(1) Item
• An LR(1) item of a CFG 𝐺 is a string of the form [𝐴 → 𝛼•𝛽, 𝑎], with 𝑎
as one symbol lookahead
• 𝐴 → 𝛼𝛽 is a production in 𝐺, and 𝑎 ∈ 𝑇 ∪ {$}
9 025 𝐶𝐶 $ Reduce by 𝑆 → 𝐶𝐶
10 01 𝑆 $ Accept
LALR Parsing }
𝐼2 = Goto(𝐼0 , 𝐶)= {
𝐼5 = Goto(𝐼2 , 𝐶) = {
𝐶 → 𝐶𝐶•, $
𝑰𝟖 = Goto(𝑰𝟑 , 𝑪) = {
𝑪 → 𝒄𝑪•, 𝒄/𝒅
𝑆 → 𝐶•𝐶, $, } }
𝐶 → •𝑐𝐶, $,
𝐶 → •𝑑, $ 𝑰𝟗 = Goto(𝑰𝟔 , 𝑪)= {
} 𝐼3 and 𝐼6 , 𝐼4 and 𝐼7 , and 𝐼8 and 𝐼9 𝑪 → 𝒄𝑪•, $
}
only differ in the second components
CS 335 Swarnendu Biswas CS 335 Swarnendu Biswas
Lookahead LR (LALR) Parsing Construction of LALR Parsing Table
• CLR(1) parser has a large number of states • Construct 𝐶 = {𝐼0 , 𝐼1 , … , 𝐼𝑛 }, the collection of sets of LR(1) items
• Lookahead LR (LALR) parser • For each core present in LR(1) items, find all sets having the same
• Merge sets of LR(1) items that have the same core (set of LR(0) items, i.e., core and replace these sets by their union
first component) • Let 𝐶 ′ = {𝐽0 , 𝐽1 , … , 𝐽𝑛 } be the resulting sets of LR(1) items (also called
• LALR parsers have fewer states, same as SLR LALR collection)
• LALR parser is used in many parser generators (e.g., Yacc and Bison) • Construct ACTION table as was done earlier, parsing actions for state 𝑖
is constructed from 𝐽𝑖
• Let 𝐽 = 𝐼1 ∪ 𝐼2 ∪ ⋯ ∪ 𝐼𝑘 , where the cores of 𝐼1 , 𝐼2 , … , 𝐼𝑘 are same
• Cores of Goto(𝐼1 , 𝑋), Goto(𝐼2 , 𝑋), …, Goto(𝐼𝑘 , 𝑋) will also be the same
• Let 𝐾 = Goto(𝐼1 , 𝑋) ∪ Goto(𝐼2 , 𝑋) ∪ … ∪ Goto(𝐼𝑘 , 𝑋), then Goto(𝐽,𝑋) = 𝐾
10 01 𝑆 $ Accept
# Production
Dealing with Errors with LALR Parsing 0 𝑆′ → 𝑆
LR(1) grammar { 𝐴 → 𝑐•, 𝑑 , [𝐵 → 𝑐•, 𝑒]} { 𝐴 → 𝑐•, 𝑒 , [𝐵 → 𝑐•, 𝑑]} CLR Parsing Table LALR Parsing Table
Action Goto Action Goto
𝑆′ → 𝑆 State
𝑐 𝑑 $ 𝑆 𝐶
State
𝑐 𝑑 $ 𝑆 𝐶
𝑆 → 𝑎𝐴𝑑 | 𝑏𝐵𝑑|𝑎𝐵𝑒|𝑏𝐴𝑒 0 𝑠3 𝑠4 1 2 0 𝑠36 𝑠47 1 2
𝐴→𝑐 { 𝐴 → 𝑐•, 𝑑/𝑐 , [𝐵 → 𝑐•, 𝑑/𝑒]} 1 𝑎𝑐𝑐 1 𝑎𝑐𝑐
𝐵→𝑐 2 𝑠6 𝑠7 5 2 𝑠36 𝑠47 5
3 𝑠3 𝑠4 8 36 𝑠36 𝑠47 89
𝑎𝑐𝑑, 𝑎𝑐𝑒, 𝑏𝑐𝑑, 𝑏𝑐𝑒 4 𝑟3 𝑟3 47 𝑟3 𝑟3 𝑟3
5 𝑟1 5 𝑟1
6 𝑠6 𝑠7 9 89 𝑟2 𝑟2 𝑟2
7 𝑟3
8 𝑟2 𝑟2
9 𝑟2
0 36 89 c𝐶 $ Reduce by 𝐶 → c𝐶 0 36 89 c𝐶 $ Reduce by 𝐶 → c𝐶
02 𝐶 $ Error 02 𝐶 $ Error
𝐼1 = Goto(𝐼0 , 𝐸) = {
𝐸 → •𝐸 + 𝐸,
𝐸 → •𝐸 ∗ 𝐸,
𝐸 → •(𝐸),
𝐼7 = Goto(𝐼4 , 𝐸) = {
𝑬 → 𝑬 + 𝑬•,
𝑬 → 𝑬• + 𝑬,
𝐸 ′ → 𝐸•,
𝐸 → 𝐸• + 𝐸, 𝐸 → •id 𝑬 → 𝑬• ∗ 𝑬
𝐸 → 𝐸• ∗ 𝐸 } }
} 𝐼9 = Goto(𝐼6 , ‘)’)= { 𝐼8 = Goto(𝐼5 , 𝐸) = {
𝐸 → (𝐸)• 𝑬 → 𝑬 ∗ 𝑬•,
}
Does not specify the associativity and 𝑬 → 𝑬• + 𝑬,
precedence of the two operators 𝑬 → 𝑬• ∗ 𝑬
CS 335 Swarnendu Biswas CS 335 Swarnendu Biswas
}
SLR(1) Parsing Table Moves of an SLR Parser on id + id ∗ id
ACTION GOTO
State
id + ∗ ( ) $ 𝐸 Stack Symbols Input Action
0 𝑠3 𝑠2 1 1 0 id + id ∗ id$ Shift 3
1 𝑠4 𝑠5 𝑎𝑐𝑐 2 03 id +id ∗ id$ Reduce by 𝐸 → id
2 𝑠3 𝑠2 6
3 01 𝐸 +id ∗ id$ Shift 4
3 𝑟4 𝑟4 𝑟4 𝑟4
4 014 𝐸+ id ∗ id$ Shift 3
4 𝑠3 𝑠2 7
5 0143 𝐸 + id ∗ id$ Reduce by 𝐸 → id
5 𝑠3 𝑠2 8
6 𝑠4 𝑠5 𝑠9 6 0147 𝐸+𝐸 ∗ id$
7 𝑠4, 𝑟1 𝑠5, 𝑟1 𝑟1 𝑟1
8 𝑠4, 𝑟2 𝑠5, 𝑟2 𝑟2 𝑟2
What can the parser do to
resolve the ambiguity?
9 𝑟3 𝑟3 𝑟3 𝑟3
References
• A. Aho et al. Compilers: Principles, Techniques, and Tools, 2nd edition, Chapter 4.5-4.8.
• K. Cooper and L. Torczon. Engineering a Compiler, 2nd edition, Chapter 3.4.