cs 335: top-down parsing - cse - iit kanpur...cs 335: top-down parsing swarnendu biswas semester...

Post on 23-Jul-2020

24 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS 335: Top-down ParsingSwarnendu Biswas

Semester 2019-2020-II

CSE, IIT Kanpur

Content influenced by many excellent references, see References slide for acknowledgements.

Example Expression Grammar

𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ → đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿ − 𝑇𝑒𝑟𝑚 𝑇𝑒𝑟𝑚

𝑇𝑒𝑟𝑚 → 𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚 Ă· đč𝑎𝑐𝑡𝑜𝑟 đč𝑎𝑐𝑡𝑜𝑟

đč𝑎𝑐𝑡𝑜𝑟 → đžđ‘„đ‘đ‘Ÿ | num | name

CS 335 Swarnendu Biswas

pri

ori

ty

Derivation of name + name × nameSentential Form Input

đžđ‘„đ‘đ‘Ÿ ↑ name + name × name

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚 ↑ name + name × name

𝑇𝑒𝑟𝑚 + 𝑇𝑒𝑟𝑚 ↑ name + name × name

đč𝑎𝑐𝑡𝑜𝑟 + 𝑇𝑒𝑟𝑚 ↑ name + name × name

name + 𝑇𝑒𝑟𝑚 ↑ name + name × name

name + 𝑇𝑒𝑟𝑚 name ↑ +name × name

name + 𝑇𝑒𝑟𝑚 name +↑ name × name

name + 𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟 name +↑ name × name

name + đč𝑎𝑐𝑡𝑜𝑟 × đč𝑎𝑐𝑡𝑜𝑟 name +↑ name × name

name + name × đč𝑎𝑐𝑡𝑜𝑟 name +↑ name × name

name + name × đč𝑎𝑐𝑡𝑜𝑟 name + name ↑× name

name + name × đč𝑎𝑐𝑡𝑜𝑟 name + name ×↑ name

name + name × name name + name ×↑ name

name + name × name name + name × name ↑

CS 335 Swarnendu Biswas

Derivation of name + name × nameSentential Form Input

đžđ‘„đ‘đ‘Ÿ ↑ name + name × name

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚 ↑ name + name × name

𝑇𝑒𝑟𝑚 + 𝑇𝑒𝑟𝑚 ↑ name + name × name

đč𝑎𝑐𝑡𝑜𝑟 + 𝑇𝑒𝑟𝑚 ↑ name + name × name

name + 𝑇𝑒𝑟𝑚 ↑ name + name × name

name + 𝑇𝑒𝑟𝑚 name ↑ +name × name

name + 𝑇𝑒𝑟𝑚 name +↑ name × name

name + 𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟 name +↑ name × name

name + đč𝑎𝑐𝑡𝑜𝑟 × đč𝑎𝑐𝑡𝑜𝑟 name +↑ name × name

name + name × đč𝑎𝑐𝑡𝑜𝑟 name +↑ name × name

name + name × đč𝑎𝑐𝑡𝑜𝑟 name + name ↑× name

name + name × đč𝑎𝑐𝑡𝑜𝑟 name + name ×↑ name

name + name × name name + name ×↑ name

name + name × name name + name × name ↑

CS 335 Swarnendu Biswas

The current input terminal being scanned is called the lookahead symbol

Derivation of name + name × name

CS 335 Swarnendu Biswas

𝑙𝑚𝑆𝑡𝑎𝑟𝑡

đ‘™đ‘šđžđ‘„đ‘đ‘Ÿ đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

𝑙𝑚 đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

𝑇𝑒𝑟𝑚

𝑙𝑚 đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

Term

đč𝑎𝑐𝑡𝑜𝑟

𝑙𝑚 đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

Term

đč𝑎𝑐𝑡𝑜𝑟

name

Derivation of name + name × name

CS 335 Swarnendu Biswas

𝑙𝑚 đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

Term

đč𝑎𝑐𝑡𝑜𝑟

name

𝑙𝑚 đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

Term

đč𝑎𝑐𝑡𝑜𝑟

name

𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟

𝑙𝑚 đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

Term

đč𝑎𝑐𝑡𝑜𝑟

name

𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟

đč𝑎𝑐𝑡𝑜𝑟

𝑙𝑚 đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

Term

đč𝑎𝑐𝑡𝑜𝑟

name

𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟

đč𝑎𝑐𝑡𝑜𝑟

name

Derivation of name + name × name

CS 335 Swarnendu Biswas

𝑙𝑚 đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

Term

đč𝑎𝑐𝑡𝑜𝑟

name

𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟

đč𝑎𝑐𝑡𝑜𝑟

name

𝑙𝑚 đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

Term

đč𝑎𝑐𝑡𝑜𝑟

name

𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟

đč𝑎𝑐𝑡𝑜𝑟

name

name

General Idea of Top-down Parsing

Start with the root (start symbol) of the parse tree

Grow the tree downwards by expanding productions at the lower levels of the tree

‱ Select a nonterminal and extend it by adding children corresponding to the right side of some production for the nonterminal

Repeat till

‱ Lower fringe consists only terminals and the input is consumed

Top-down parsing basically finds a leftmost derivation for an input string

CS 335 Swarnendu Biswas

General Idea of Top-down Parsing

Start with the root of the parse tree

Grow the tree by expanding productions at the lower levels of the tree

‱ Extend a nonterminal by adding children corresponding to the right side of some production for the nonterminal

Repeat till

‱ Lower fringe consists only terminals and the input is consumed

‱ Mismatch in the lower fringe and the remaining input stream

‱ Selection of a production may involve trial-and-error

‱ Wrong choice of productions while expanding nonterminals

‱ Input character stream is not part of the language

CS 335 Swarnendu Biswas

Leftmost Top-down Parsing Algorithmroot = node for Start symbol

curr = root

push(null) // Stack

word = nextWord()

while (true):

if curr ∈ Nonterminal:

pick next rule 𝐮 ⟶ đ›œ1đ›œ2â€Šđ›œđ‘› to expand curr

create nodes for đ›œ1, đ›œ2, 
, đ›œđ‘› as children of curr

push(đ›œđ‘›, đ›œđ‘›âˆ’1, đ›œ1)

curr = đ›œ1

if curr == word:

word = nextWord()

curr = pop()

if word == eof and curr == null:

accept input

else

backtrack

CS 335 Swarnendu Biswas

Implementing Backtracking

‱ Extend the previous algorithm to backtrack‱ Set curr to parent and delete the children

‱ Expand the node curr with untried rules if any‱ Create child nodes for each symbol in the right hand of the production

‱ Push those symbols onto the stack in reverse order

‱ Set curr to the first child node

‱ Move curr up the tree if there are no untried rules

‱ Report a syntax error when there are no more moves

CS 335 Swarnendu Biswas

Example of Top-down ParsingRule # Sentential Form Input

đžđ‘„đ‘đ‘Ÿ ↑ name+ name × name

1 đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚 ↑ name+ name × name

3 𝑇𝑒𝑟𝑚 + 𝑇𝑒𝑟𝑚 ↑ name+ name × name

6 đč𝑎𝑐𝑡𝑜𝑟 + 𝑇𝑒𝑟𝑚 ↑ name+ name × name

9 name+ 𝑇𝑒𝑟𝑚 ↑ name+ name × name

name+ 𝑇𝑒𝑟𝑚 name ↑ +name × name

name+ 𝑇𝑒𝑟𝑚 name+↑ name × name

4 name+ 𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟 name+↑ name × name

6 name+ đč𝑎𝑐𝑡𝑜𝑟 × đč𝑎𝑐𝑡𝑜𝑟 name+↑ name × name

9 name+ name × đč𝑎𝑐𝑡𝑜𝑟 name+↑ name × name

name+ name × đč𝑎𝑐𝑡𝑜𝑟 name+ name ↑× name

name+ name × đč𝑎𝑐𝑡𝑜𝑟 name+ name ×↑ name

9 name+ name × name name+ name ×↑ name

name+ name × name name+ name × name ↑

CS 335 Swarnendu Biswas

Rule # Production

0 𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

1 đžđ‘„đ‘đ‘Ÿ → đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

2 đžđ‘„đ‘đ‘Ÿ → đžđ‘„đ‘đ‘Ÿ − 𝑇𝑒𝑟𝑚

3 đžđ‘„đ‘đ‘Ÿ → 𝑇𝑒𝑟𝑚

4 𝑇𝑒𝑟𝑚 → 𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟

5 𝑇𝑒𝑟𝑚 → 𝑇𝑒𝑟𝑚 Ă· đč𝑎𝑐𝑡𝑜𝑟

6 𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟

7 đč𝑎𝑐𝑡𝑜𝑟 → (đžđ‘„đ‘đ‘Ÿ)

8 đč𝑎𝑐𝑡𝑜𝑟 → num

9 đč𝑎𝑐𝑡𝑜𝑟 → name

Example of Top-down ParsingRule # Sentential Form Input

đžđ‘„đ‘đ‘Ÿ ↑ name+ name × name

1 đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚 ↑ name+ name × name

3 𝑇𝑒𝑟𝑚 + 𝑇𝑒𝑟𝑚 ↑ name+ name × name

6 đč𝑎𝑐𝑡𝑜𝑟 + 𝑇𝑒𝑟𝑚 ↑ name+ name × name

9 name+ 𝑇𝑒𝑟𝑚 ↑ name+ name × name

name+ 𝑇𝑒𝑟𝑚 name ↑ +name × name

name+ 𝑇𝑒𝑟𝑚 name+↑ name × name

4 name+ 𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟 name+↑ name × name

6 name+ đč𝑎𝑐𝑡𝑜𝑟 × đč𝑎𝑐𝑡𝑜𝑟 name+↑ name × name

9 name+ name × đč𝑎𝑐𝑡𝑜𝑟 name+↑ name × name

name+ name × đč𝑎𝑐𝑡𝑜𝑟 name+ name ↑× name

name+ name × đč𝑎𝑐𝑡𝑜𝑟 name+ name ×↑ name

9 name+ name × name name+ name ×↑ name

name+ name × name name+ name × name ↑

CS 335 Swarnendu Biswas

Rule # Production

0 𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

1 đžđ‘„đ‘đ‘Ÿ → đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

2 đžđ‘„đ‘đ‘Ÿ → đžđ‘„đ‘đ‘Ÿ − 𝑇𝑒𝑟𝑚

3 đžđ‘„đ‘đ‘Ÿ → 𝑇𝑒𝑟𝑚

4 𝑇𝑒𝑟𝑚 → 𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟

5 𝑇𝑒𝑟𝑚 → 𝑇𝑒𝑟𝑚 Ă· đč𝑎𝑐𝑡𝑜𝑟

6 𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟

7 đč𝑎𝑐𝑡𝑜𝑟 → (đžđ‘„đ‘đ‘Ÿ)

8 đč𝑎𝑐𝑡𝑜𝑟 → num

9 đč𝑎𝑐𝑡𝑜𝑟 → name

How does a top-down parser choose which rule to apply?

Example of Top-down Parsing

Rule # Sentential Form Input

đžđ‘„đ‘đ‘Ÿ ↑ name+ name × name

1 đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚 ↑ name+ name × name

1 đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚 + 𝑇𝑒𝑟𝑚 ↑ name+ name × name

1 đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚 + 𝑇𝑒𝑟𝑚 +⋯ ↑ name+ name × name

1 
 ↑ name+ name × name

1 
 ↑ name+ name × name

CS 335 Swarnendu Biswas

Rule # Production

0 𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

1 đžđ‘„đ‘đ‘Ÿ → đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

2 đžđ‘„đ‘đ‘Ÿ → đžđ‘„đ‘đ‘Ÿ − 𝑇𝑒𝑟𝑚

3 đžđ‘„đ‘đ‘Ÿ → 𝑇𝑒𝑟𝑚

4 𝑇𝑒𝑟𝑚 → 𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟

5 𝑇𝑒𝑟𝑚 → 𝑇𝑒𝑟𝑚 Ă· đč𝑎𝑐𝑡𝑜𝑟

6 𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟

7 đč𝑎𝑐𝑡𝑜𝑟 → (đžđ‘„đ‘đ‘Ÿ)

8 đč𝑎𝑐𝑡𝑜𝑟 → num

9 đč𝑎𝑐𝑡𝑜𝑟 → name

Example of Top-Down Parsing

Rule # Sentential Form Input

đžđ‘„đ‘đ‘Ÿ ↑ name+ name × name

1 đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚 ↑ name+ name × name

1 đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚 + 𝑇𝑒𝑟𝑚 ↑ name+ name × name

1 đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚 + 𝑇𝑒𝑟𝑚 +⋯ ↑ name+ name × name

1 
 ↑ name+ name × name

1 
 ↑ name+ name × name

CS 335 Swarnendu Biswas

Rule # Production

0 𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

1 đžđ‘„đ‘đ‘Ÿ → đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

2 đžđ‘„đ‘đ‘Ÿ → đžđ‘„đ‘đ‘Ÿ − 𝑇𝑒𝑟𝑚

3 đžđ‘„đ‘đ‘Ÿ → 𝑇𝑒𝑟𝑚

4 𝑇𝑒𝑟𝑚 → 𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟

5 𝑇𝑒𝑟𝑚 → 𝑇𝑒𝑟𝑚 Ă· đč𝑎𝑐𝑡𝑜𝑟

6 𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟

7 đč𝑎𝑐𝑡𝑜𝑟 → (đžđ‘„đ‘đ‘Ÿ)

8 đč𝑎𝑐𝑡𝑜𝑟 → num

9 đč𝑎𝑐𝑡𝑜𝑟 → name

A top-down parser can loop indefinitely with left-recursive grammar

Left Recursion

‱ A grammar is left-recursive if it has a nonterminal 𝐮 such that there is

a derivation 𝐮 ֜+đŽđ›Œ for some string đ›Œ

‱ Direct left recursion: There is a production of the form 𝐮 → đŽđ›Œ

‱ Indirect left recursion: First symbol on the right-hand side of a rule can derive the symbol on the left

CS 335 Swarnendu Biswas

We can often reformulate a grammar to avoid left recursion

Remove Left Recursion

CS 335 Swarnendu Biswas

𝐮 → đŽđ›Œ1 đŽđ›Œ2 
 |đŽđ›Œđ‘š đ›œ1 
 |đ›œđ‘›

𝐮 → đ›œ1𝐮â€Č|đ›œ2𝐮

â€Č|
| đ›œđ‘›đŽâ€Č

𝐮â€Č → đ›Œ1𝐮â€Č đ›Œ2𝐮

â€Č 
 |đ›Œđ‘šđŽâ€Č|𝜖

Remove Left Recursion

CS 335 Swarnendu Biswas

𝐾 → 𝐾 + 𝑇 | 𝑇𝑇 → 𝑇 ∗ đč | đčđč → 𝐾 | id

𝐾 → 𝑇𝐾â€Č

𝐾â€Č → +𝑇𝐾â€Č

𝑇 → đč𝑇â€Č

𝑇â€Č →∗ đč𝑇â€Č

đč → 𝐾 |id

Non-Left-Recursive Expression Grammar

CS 335 Swarnendu Biswas

Rule # Production

0 𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

1 đžđ‘„đ‘đ‘Ÿ → đžđ‘„đ‘đ‘Ÿ + 𝑇𝑒𝑟𝑚

2 đžđ‘„đ‘đ‘Ÿ → đžđ‘„đ‘đ‘Ÿ − 𝑇𝑒𝑟𝑚

3 đžđ‘„đ‘đ‘Ÿ → 𝑇𝑒𝑟𝑚

4 𝑇𝑒𝑟𝑚 → 𝑇𝑒𝑟𝑚 × đč𝑎𝑐𝑡𝑜𝑟

5 𝑇𝑒𝑟𝑚 → 𝑇𝑒𝑟𝑚 Ă· đč𝑎𝑐𝑡𝑜𝑟

6 𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟

7 đč𝑎𝑐𝑡𝑜𝑟 → (đžđ‘„đ‘đ‘Ÿ)

8 đč𝑎𝑐𝑡𝑜𝑟 → num

9 đč𝑎𝑐𝑡𝑜𝑟 → name

Rule # Production

0 𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

1 đžđ‘„đ‘đ‘Ÿ → 𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č

2 đžđ‘„đ‘đ‘Ÿâ€Č → + 𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č

3 đžđ‘„đ‘đ‘Ÿâ€Č → − 𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č

4 đžđ‘„đ‘đ‘Ÿâ€Č → 𝜖

5 𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č

6 𝑇𝑒𝑟𝑚â€Č →× đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č

7 𝑇𝑒𝑟𝑚â€Č →÷ đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č

8 𝑇𝑒𝑟𝑚â€Č → 𝜖

9 đč𝑎𝑐𝑡𝑜𝑟 → (đžđ‘„đ‘đ‘Ÿ)

10 đč𝑎𝑐𝑡𝑜𝑟 → num

11 đč𝑎𝑐𝑡𝑜𝑟 → name

Indirect Left Recursion

‱ There is a left recursion because 𝑆 → 𝐮𝑎 → 𝑆𝑑𝑎

CS 335 Swarnendu Biswas

𝑆 → 𝐮𝑎 | 𝑏𝐮 → 𝐮𝑐 𝑆𝑑 𝜖

Eliminating Left Recursion

‱ Input: Grammar đș with no cycles or 𝜖−productions

‱ AlgorithmArrange nonterminals in some order 𝐮1, 𝐮2, 
 , 𝐮𝑛for 𝑖 ← 1
𝑛

for 𝑗 ← 1 to 𝑖 − 1

If ∃ a production 𝐮𝑖 → đŽđ‘—đ›Ÿ

Replace 𝐮𝑖 → đŽđ‘—đ›Ÿ with one or more productions that expand 𝐮𝑗

Eliminate the immediate left recursion among the 𝐮𝑖 productions

CS 335 Swarnendu Biswas

Eliminating Left Recursion

‱ Input: Grammar đș with no cycles or 𝜖−productions

‱ AlgorithmArrange nonterminals in some order 𝐮1, 𝐮2, 
 , 𝐮𝑛for 𝑖 ← 1
𝑛

for 𝑗 ← 1 to 𝑖 − 1

If ∃ a production 𝐮𝑖 → đŽđ‘—đ›Ÿ

Replace 𝐮𝑖 → đŽđ‘—đ›Ÿ with one or more productions that expand 𝐮𝑗

Eliminate the immediate left recursion among the 𝐮𝑖 productions

CS 335 Swarnendu Biswas

Loop invariant at the start of outer iteration 𝑖

∀𝑘 < 𝑖, no production expanding 𝐮𝑘 has 𝐮𝑙 in its righthand side for all 𝑙 < 𝑘

Eliminating Indirect Left Recursion

CS 335 Swarnendu Biswas

𝑆 → 𝐮𝑎 | 𝑏𝐮 → 𝐮𝑐 𝑆𝑑 𝜖

𝑆 → 𝐮𝑎 | 𝑏𝐮 → 𝑏𝑑𝐮â€Č | 𝐮â€Č

𝐮â€Č → 𝑐𝐮â€Č 𝑎𝑑𝐮â€Č 𝜖

Cost of Backtracking

Backtracking is expensive

‱ Parser expands a nonterminal with the wrong rule

‱ Mismatch between the lower fringe of the parse tree and the input is detected

‱ Parser undoes the last few actions

‱ Parser tries other productions if any

CS 335 Swarnendu Biswas

Avoid Backtracking

‱ Parser is to select the next rule ‱ Compare the curr symbol and the next input symbol called the lookahead

‱ Use the lookahead to disambiguate the possible production rules

‱ Backtrack-free grammar is a CFG for which the leftmost, top-down parser can always predict the correct rule with one word lookahead ‱ Also called a predictive grammar

CS 335 Swarnendu Biswas

FIRST Set

‱ Intuition‱ Each alternative for the leftmost nonterminal leads to a distinct terminal

symbol

‱ Which rule to choose becomes obvious by comparing the next word in the input stream

‱ Given a string đ›Ÿ of terminal and nonterminal symbols, FIRST(đ›Ÿ) is the set of all terminal symbols that can begin any string derived from đ›Ÿâ€ą We also need to keep track of which symbols can produce the empty string

‱ FIRST: (𝑁𝑇 âˆȘ 𝑇 âˆȘ 𝜖, EOF ) → (𝑇 âˆȘ 𝜖, EOF )

CS 335 Swarnendu Biswas

Steps to Compute FIRST Set

1. If 𝑋 is a terminal, then FIRST 𝑋 = {𝑋}

2. If 𝑋 → 𝜖 is a production, then 𝜖 ∈ FIRST(𝑋)

3. If 𝑋 is a nonterminal and 𝑋 → 𝑌1𝑌2
 𝑌𝑘 is a productionI. Everything in FIRST(𝑌1) is in FIRST 𝑋

II. If for some 𝑖, 𝑎 ∈ FIRST(𝑌𝑖) and ∀1 ≀ 𝑗 < 𝑖, 𝜖 ∈ FIRST(𝑌𝑗), then 𝑎 ∈FIRST(𝑋)

III. If 𝜖 ∈ FIRST(𝑌1
𝑌𝑘), then 𝜖 ∈ FIRST(𝑋)

CS 335 Swarnendu Biswas

FIRST Set

‱ Generalize FIRST relation to string of symbols

FIRST đ‘‹đ›Ÿ → FIRST 𝑋 if 𝑋 ↛ 𝜖

FIRST đ‘‹đ›Ÿ → FIRST 𝑋 âˆȘ FIRST đ›Ÿ if 𝑋 → 𝜖

CS 335 Swarnendu Biswas

Compute FIRST Set

CS 335 Swarnendu Biswas

𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ → 𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č

đžđ‘„đ‘đ‘Ÿâ€Č → +𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č

−𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č 𝜖

𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č

𝑇𝑒𝑟𝑚â€Č →× đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č

Ă· đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č 𝜖

đč𝑎𝑐𝑡𝑜𝑟 → (đžđ‘„đ‘đ‘Ÿ) | num | name

Compute FIRST Set

FIRST đžđ‘„đ‘đ‘Ÿ = {(, name, num}

FIRST đžđ‘„đ‘đ‘Ÿâ€Č = {+,−, 𝜖}

FIRST 𝑇𝑒𝑟𝑚 = {(, name, num}

FIRST 𝑇𝑒𝑟𝑚â€Č = {𝜖 ×,Ă·}

FIRST đč𝑎𝑐𝑡𝑜𝑟 = {(, name,num}

CS 335 Swarnendu Biswas

𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ → 𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č

đžđ‘„đ‘đ‘Ÿâ€Č → +𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č

−𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č 𝜖

𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č

𝑇𝑒𝑟𝑚â€Č →× đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č

Ă· đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č 𝜖

đč𝑎𝑐𝑡𝑜𝑟 → (đžđ‘„đ‘đ‘Ÿ) | num | name

FOLLOW Set

‱ FOLLOW(𝑋) is the set of terminals that can immediately follow 𝑋‱ That is, 𝑡 ∈ FOLLOW(𝑋) if there is any derivation containing 𝑋𝑡

CS 335 Swarnendu Biswas

𝑆

𝐮 𝑎 đ›œđ›Œ

𝑐 đ›Ÿâ€Š

Terminal 𝑐 is in FIRST(𝐮) and 𝑎is in FOLLOW(𝐮)

Steps to Compute FOLLOW Set

1. Place $ in FOLLOW(𝑆) where 𝑆 is the start symbol and $ is the end marker

2. If there is a production 𝐮 → đ›Œđ”đ›œ, then everything in FIRST(đ›œ)except 𝜖 is in FOLLOW(đ”)

3. If there is a production 𝐮 → đ›Œđ”, or a production 𝐮 → đ›Œđ”đ›œ where FIRST(đ›œ) contains 𝜖, then everything in FOLLOW(𝐮) is in FOLLOW(đ”)

CS 335 Swarnendu Biswas

Compute FOLLOW Set

CS 335 Swarnendu Biswas

𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ → 𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č

đžđ‘„đ‘đ‘Ÿâ€Č → +𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č

−𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č 𝜖

𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č

𝑇𝑒𝑟𝑚â€Č →× đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č

Ă· đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č 𝜖

đč𝑎𝑐𝑡𝑜𝑟 → (đžđ‘„đ‘đ‘Ÿ) | num | name

Compute FOLLOW Set

FOLLOW đžđ‘„đ‘đ‘Ÿ = {$, )}

FOLLOW đžđ‘„đ‘đ‘Ÿâ€Č = {$,)}

FOLLOW 𝑇𝑒𝑟𝑚 = {$, +,−, )}

FOLLOW 𝑇𝑒𝑟𝑚â€Č = {$,+,−, )}

FOLLOW đč𝑎𝑐𝑡𝑜𝑟 = {$, +,−,×,Ă·, )}

CS 335 Swarnendu Biswas

𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ → 𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č

đžđ‘„đ‘đ‘Ÿâ€Č → +𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č

−𝑇𝑒𝑟𝑚 đžđ‘„đ‘đ‘Ÿâ€Č 𝜖

𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č

𝑇𝑒𝑟𝑚â€Č →× đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č

Ă· đč𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚â€Č 𝜖

đč𝑎𝑐𝑡𝑜𝑟 → (đžđ‘„đ‘đ‘Ÿ) | num | name

Conditions for Backtrack-Free Grammar

‱ Consider a production 𝐮 → đ›œ

FIRST+ = ቊFIRST đ›œ if 𝜖 ∉ FIRST(đ›œ)

FIRST đ›œ âˆȘ FOLLOW 𝐮 otherwise

‱ For any nonterminal 𝐮 where 𝐮 → đ›œ1|đ›œ2|
| đ›œđ‘›, a backtrack-free grammar has the property FIRST+ 𝐮 → đ›œđ‘– ∩ FIRST+ 𝐮 → đ›œđ‘— = 𝜙, ∀1 ≀ 𝑖, 𝑗 ≀ 𝑛, 𝑖 ≠ 𝑗

CS 335 Swarnendu Biswas

Backtracking

𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ → đ‘‡đ‘’đ‘Ÿđ‘šđžđ‘„đ‘đ‘Ÿâ€Č

đžđ‘„đ‘đ‘Ÿâ€Č → +đ‘‡đ‘’đ‘Ÿđ‘šđžđ‘„đ‘đ‘Ÿâ€Č

âˆ’đ‘‡đ‘’đ‘Ÿđ‘šđžđ‘„đ‘đ‘Ÿâ€Č 𝜖

𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟𝑇𝑒𝑟𝑚â€Č

𝑇𝑒𝑟𝑚â€Č →× đč𝑎𝑐𝑡𝑜𝑟𝑇𝑒𝑟𝑚â€Č

Ă· đč𝑎𝑐𝑡𝑜𝑟𝑇𝑒𝑟𝑚â€Č 𝜖

đč𝑎𝑐𝑡𝑜𝑟 → name

| name [ 𝐮𝑟𝑔𝑙𝑖𝑠𝑡 ]

| name ( 𝐮𝑟𝑔𝑙𝑖𝑠𝑡 )

𝐮𝑟𝑔𝑙𝑖𝑠𝑡 → đžđ‘„đ‘đ‘Ÿ 𝑀𝑜𝑟𝑒𝐮𝑟𝑔𝑠

𝑀𝑜𝑟𝑒𝐮𝑟𝑔𝑠 → , đžđ‘„đ‘đ‘Ÿ 𝑀𝑜𝑟𝑒𝐮𝑟𝑔𝑠

| 𝜖

CS 335 Swarnendu Biswas

Backtracking

𝑆𝑡𝑎𝑟𝑡 → đžđ‘„đ‘đ‘Ÿ

đžđ‘„đ‘đ‘Ÿ → đ‘‡đ‘’đ‘Ÿđ‘šđžđ‘„đ‘đ‘Ÿâ€Č

đžđ‘„đ‘đ‘Ÿâ€Č → +đ‘‡đ‘’đ‘Ÿđ‘šđžđ‘„đ‘đ‘Ÿâ€Č

âˆ’đ‘‡đ‘’đ‘Ÿđ‘šđžđ‘„đ‘đ‘Ÿâ€Č 𝜖

𝑇𝑒𝑟𝑚 → đč𝑎𝑐𝑡𝑜𝑟𝑇𝑒𝑟𝑚â€Č

𝑇𝑒𝑟𝑚â€Č →× đč𝑎𝑐𝑡𝑜𝑟𝑇𝑒𝑟𝑚â€Č

Ă· đč𝑎𝑐𝑡𝑜𝑟𝑇𝑒𝑟𝑚â€Č 𝜖

đč𝑎𝑐𝑡𝑜𝑟 → name

| name [ 𝐮𝑟𝑔𝑙𝑖𝑠𝑡 ]

| name ( 𝐮𝑟𝑔𝑙𝑖𝑠𝑡 )

𝐮𝑟𝑔𝑙𝑖𝑠𝑡 → đžđ‘„đ‘đ‘Ÿ 𝑀𝑜𝑟𝑒𝐮𝑟𝑔𝑠

𝑀𝑜𝑟𝑒𝐮𝑟𝑔𝑠 → , đžđ‘„đ‘đ‘Ÿ 𝑀𝑜𝑟𝑒𝐮𝑟𝑔𝑠

| 𝜖

CS 335 Swarnendu Biswas

Not all grammars are backtrack free

Left Factoring

‱ Left factoring is the process of extracting and isolating common prefixes in a set of productions

‱ Algorithm

CS 335 Swarnendu Biswas

đč𝑎𝑐𝑡𝑜𝑟 → 𝑛𝑎𝑚𝑒 𝐮𝑟𝑔𝑱𝑚𝑒𝑛𝑡𝑠𝐮𝑟𝑔𝑱𝑚𝑒𝑛𝑡𝑠 → 𝐮𝑟𝑔𝐿𝑖𝑠𝑡 𝐮𝑟𝑔𝐿𝑖𝑠𝑡 𝜖

𝐮 → đ›Œđ›œ1 đ›Œđ›œ2 
 đ›Œđ›œđ‘› đ›Ÿ1 đ›Ÿ2 
 |đ›Ÿđ‘—

𝐮 → đ›Œđ”|đ›Ÿ1 đ›Ÿ2 
 |đ›Ÿđ‘—đ” → đ›œ1 đ›œ2 
 |đ›œđ‘›

Key Insight in Using Top-Down Parsing

‱ Efficiency depends on the accuracy of selecting the correct production for expanding a nonterminal‱ Parser may not terminate in the worst case

‱ A large subset of the context-free grammars can be parsed without backtracking

CS 335 Swarnendu Biswas

Recursive-Descent Parsing

CS 335 Swarnendu Biswas

Recursive-Descent Parsing

‱ Recursive-descent parsing is a form of top-down parsing that mayrequire backtracking

‱ Consists of a set of procedures, one for each nonterminal

CS 335 Swarnendu Biswas

void A() {Choose an A-production 𝐮 → 𝑋1𝑋2
𝑋𝑘for 𝑖 ← 1
𝑘

if 𝑋𝑖 is a nonterminalcall procedure 𝑋𝑖()

else if 𝑋𝑖 equals the current input symbol 𝑎advance the input to the next symbol

else // error

}

Limitations with Recursive-Descent Parsing

‱ Consider a grammar with two productions 𝑋 → đ›Ÿ1 and 𝑋 → đ›Ÿ2‱ Suppose FIRST(đ›Ÿ1) ∩ FIRST(đ›Ÿ2) ≠ 𝜙

‱ Say 𝑎 is the common terminal symbol

‱ Function corresponding to 𝑋 will not know which production to use on input token 𝑎

CS 335 Swarnendu Biswas

Recursive-Descent Parsing with Backtracking

‱ To support backtracking ‱ All productions should be tried in some order

‱ Failure for some production implies we need to try remaining productions

‱ Report an error only when there are no other rules

CS 335 Swarnendu Biswas

Predictive Parsing

‱ Special case of recursive-descent parsing that does not require backtracking‱ Lookahead symbol unambiguously determines which production rule to use

‱ Advantage is that the algorithm is simple and the parser can be constructed by hand

CS 335 Swarnendu Biswas

𝑠𝑡𝑚𝑡 → expr ;| if đ‘’đ‘„đ‘đ‘Ÿ 𝑠𝑡𝑚𝑡| for đ‘œđ‘đ‘Ąđ‘’đ‘„đ‘đ‘Ÿ ; đ‘œđ‘đ‘Ąđ‘’đ‘„đ‘đ‘Ÿ ; đ‘œđ‘đ‘Ąđ‘’đ‘„đ‘đ‘Ÿ 𝑠𝑡𝑚𝑡| other

đ‘œđ‘đ‘Ąđ‘’đ‘„đ‘đ‘Ÿ → 𝜖 | expr

Pseudocode for a Predictive Parser

void stmt() {switch(lookahead) {

case expr:match(expr); match(‘;’); break;

case if:match(if); match(‘(‘); match(expr); match(‘)’); stmt(); break;

case for:match(for); match(‘(‘); optexpr(); match(‘;’); optexpr(); match(‘;’); optexpr(); match(‘)’); stmt(); break;

case other:match(other); break;

default:report(“syntax error”);

}}

CS 335 Swarnendu Biswas

LL(1) Grammars

‱ Class of grammars for which no backtracking is required‱ First L stands for left-to-right scan, second L stands for leftmost derivation

‱ There is one lookahead token

‱ No left-recursive or ambiguous grammar can be LL(1)

‱ In LL(k), k stands for k lookahead tokens‱ Predictive parsers accept LL(k) grammars

‱ Every LL(1) grammar is a LL(2) grammar

CS 335 Swarnendu Biswas

Nonrecursive Table-Driven Predictive Parser

CS 335 Swarnendu Biswas

Parsing Table 𝑀

Predictive Parsing Program

a + b $Input

OutputStack X

Y

Z

$

Predictive Parsing Algorithm‱ Input: String đ‘€ and parsing table 𝑀 for grammar đș

‱ Algorithm:Let 𝑎 be the first symbol in đ‘€Let 𝑋 be the symbol at the top of the stack while 𝑋 ≠ $:

if 𝑋 == 𝑎:pop the stack and advance the input

else if 𝑋 is a terminal or 𝑀[𝑋, 𝑎] is an error entry:error

else if 𝑀 𝑋, 𝑎 == 𝑋 → 𝑌1𝑌2
𝑌𝑘:output the production pop the stackpush 𝑌𝑘𝑌𝑘−1
𝑌1 onto the stack

𝑋 ← top stack symbol

CS 335 Swarnendu Biswas

Predictive Parsing Table

CS 335 Swarnendu Biswas

Nonterminal id + * ( ) $

𝐾 𝐾 → 𝑇𝐾â€Č 𝐾 → 𝑇𝐾â€Č

𝐾â€Č 𝐾â€Č → +𝑇𝐾â€Č 𝐾â€Č → 𝜖 𝐾â€Č → 𝜖

𝑇 𝑇 → đč𝑇â€Č 𝑇 → đč𝑇â€Č

𝑇â€Č 𝑇â€Č → 𝜖 𝑇â€Č →∗ đč𝑇â€Č 𝑇â€Č → 𝜖 𝑇â€Č → 𝜖

đč đč → id đč → (𝐾)

𝐾 → 𝑇𝐾â€Č

𝐾â€Č → +𝑇𝐾â€Č | 𝜖𝑇 → đč𝑇â€Č

𝑇â€Č →∗ đč𝑇â€Č | 𝜖đč → 𝐾 | id

Construction of a Predictive Parsing Table

‱ Input: Grammar đș

‱ Algorithm:‱ For each production 𝐮 → đ›Œ in đș,

‱ For each terminal 𝑎 in FIRST đ›Œ , add 𝐮 → đ›Œ to 𝑀[𝐮, 𝑎]

‱ If 𝜖 is in FIRST đ›Œ , then for each terminal 𝑏 in FOLLOW(𝐮), add 𝐮 → đ›Œ to 𝑀 𝐮, 𝑏

‱ If 𝜖 is in FIRST đ›Œ and $ is in FOLLOW(𝐮), add 𝐮 → đ›Œ to 𝑀[𝐮, $]

‱ No production in 𝑀[𝐮, 𝑎] indicates error

CS 335 Swarnendu Biswas

Working of Predictive ParserMatched Stack Input Action

𝐾$ id+ id ∗ id$

𝑇𝐾â€Č$ id+ id ∗ id$ Output 𝐾 → 𝑇𝐾â€Č

đč𝑇â€Č𝐾â€Č$ id+ id ∗ id$ Output 𝑇 → đč𝑇â€Č

id𝑇â€Č𝐾â€Č$ id+ id ∗ id$ Output đč → id

id 𝑇â€Č𝐾â€Č$ +id ∗ id$ Match id

id 𝐾â€Č$ +id ∗ id$ Output 𝑇â€Č → 𝜖

id +𝑇𝐾â€Č$ +id ∗ id$ Output 𝐾â€Č → +𝑇𝐾â€Č

id+ 𝑇𝐾â€Č$ id ∗ id$ Match +

id+ đč𝑇â€Č𝐾â€Č$ id ∗ id$ Output 𝑇 → đč𝑇â€Č

id+ id𝐓â€Č𝐾â€Č$ id ∗ id$ Output đč → id

CS 335 Swarnendu Biswas

Working of Predictive ParserMatched Stack Input Action




id+ id𝑇â€Č𝐾â€Č$ id ∗ id$ Output đč → id

id+ id 𝑇â€Č𝐾â€Č$ ∗ id$ Match id

id+ id ∗ đč𝑇â€Č𝐾â€Č$ ∗ id$ Output 𝑇â€Č →∗ đč𝑇â€Č

id+ id∗ đč𝑇â€Č𝐾â€Č$ id$ Match ∗

id+ id∗ id𝑇â€Č𝐾â€Č$ id$ Output đč → id

id+ id∗id 𝑇â€Č𝐾â€Č$ $ Match id

id+ id∗id 𝐾â€Č$ $ Output 𝑇â€Č → 𝜖

id+ id∗id $ $ Output 𝐾â€Č → 𝜖

CS 335 Swarnendu Biswas

Predictive Parsing

‱ Grammars whose predictive parsing tables contain no duplicate entries are called LL(1)

‱ If grammar đș is left-recursive or is ambiguous, then parsing table 𝑀will have at least one multiply-defined cell

‱ Some grammars cannot be transformed into LL(1)‱ The adjacent grammar is ambiguous

CS 335 Swarnendu Biswas

𝑆 → 𝑖𝐾𝑡𝑆𝑆â€Č | 𝑎𝑆â€Č → 𝑒𝑆 | 𝜖𝐾 → 𝑏

Predictive Parsing Table

CS 335 Swarnendu Biswas

Nonterminal a b e i t $𝑆 𝑆 → 𝑎 𝑆 → 𝑖𝐾𝑡𝑆𝑆â€Č

𝑆â€Č 𝑆â€Č → 𝜖𝑆â€Č → 𝑒𝑆

𝑆â€Č → 𝜖

𝐾 𝐾 → 𝑏 𝑇 → đč𝑇â€Č

𝑆 → 𝑖𝐾𝑡𝑆𝑆â€Č| 𝑎𝑆â€Č → 𝑒𝑆 | 𝜖𝐾 → 𝑏

Error Recovery in Predictive Parsing

‱ Error conditions‱ Terminal on top of the stack does not match the next input symbol

‱ Nonterminal 𝐮 is on top of the stack, 𝑎 is the next input symbol, and 𝑀[𝐮, 𝑎]is error

‱ Choices‱ Raise an error and quit parsing

‱ Print an error message, try to recover from the error, and continue with compilation

CS 335 Swarnendu Biswas

Error Recovery in Predictive Parsing

‱ Panic mode – skip over symbols until a token in a set of synchronizing (synch) tokens appears‱ Add all tokens in FOLLOW(𝐮) to the synch set for 𝐮

‱ Add symbols in FIRST(𝐮) to the synch set for 𝐮

‱ Add keywords that can begin sentences

‱ 


CS 335 Swarnendu Biswas

Predictive Parsing Table with Synchronizing Tokens

CS 335 Swarnendu Biswas

Nonterminal id + * ( ) $

𝐾 𝐾 → 𝑇𝐾â€Č 𝐾 → 𝑇𝐾â€Č synch synch

𝐾â€Č 𝐾â€Č → +𝑇𝐾â€Č 𝐾â€Č → 𝜖 𝐾â€Č → 𝜖

𝑇 𝑇 → đč𝑇â€Č synch 𝑇 → đč𝑇â€Č synch synch

𝑇â€Č 𝑇â€Č → 𝜖 𝑇â€Č →∗ đč𝑇â€Č 𝑇â€Č → 𝜖 𝑇â€Č → 𝜖

đč đč → id synch synch đč → (𝐾) synch synch

𝐾 → 𝑇𝐾â€Č

𝐾â€Č → +𝑇𝐾â€Č | 𝜖𝑇 → đč𝑇â€Č

𝑇â€Č →∗ đč𝑇â€Č | 𝜖đč → 𝐾 | id

Error Recover Moves by Predictive ParserStack Input Remark

𝐾$ )id ∗ +id$ Error, skip )

𝐾$ id ∗ +id$ id is in FIRST(𝐾)

𝑇𝐾â€Č$ id ∗ +id$

đč𝑇𝐾â€Č$ id ∗ +id$

id𝑇𝐾â€Č$ id ∗ +id$

𝑇â€Č𝐾â€Č$ ∗ +id$

∗ đč𝑇â€Č𝐾â€Č$ ∗ +id$

đč𝑇â€Č𝐾â€Č$ +id$ Error, 𝑀 đč,+ = synch

𝑇â€Č𝐾â€Č$ +id$ đč has been popped

𝐾â€Č$ +id$

CS 335 Swarnendu Biswas

Error Recover Moves by Predictive ParserStack Input Remark

+𝑇𝐾â€Č$ +id$

𝑇𝐾â€Č$ id$

đč𝑇â€Č𝐾â€Č$ id$

id𝑇â€Č𝐾â€Č$ id$

𝑇â€Č𝐾â€Č$ $

𝐾â€Č$ $

$ $

CS 335 Swarnendu Biswas

References

‱ A. Aho et al. Compilers: Principles, Techniques, and Tools, 2nd edition, Chapter 4.4.

‱ K. Cooper and L. Torczon. Engineering a Compiler, 2nd edition, Chapter 3.3.

CS 335 Swarnendu Biswas

top related