Docsity

Predictive Parsing-Compiler Construction-Lecture Notes, Study notes of Compiler Construction

Toggle sidebar
bg1
L
Le
ec
ct
tu
ur
re
e
1
16
6
Here is the output trace for the expression : 2+4*6
>> Expr::isPresent()
>> Term::isPresent()
>> Factor::isPresent()
token: 2 (257)
<< Factor::isPresent() return true
>> Tprime::isPresent()
token: + (267)
<< Tprime::isPresent() return false
<< Term::isPresent() return true
>> Eprime::isPresent()
token: + (267)
>> Term::isPresent()
>> Factor::isPresent()
token: 4 (257)
<< Factor::isPresent() return true
>> Tprime::isPresent()
token: * (269)
>> Factor::isPresent()
token: 6 (257)
<< Factor::isPresent() return true
>> Tprime::isPresent()
token: (0)
<< Tprime::isPresent() return false
<< Tprime::isPresent() return true
<< Term::isPresent() return true
>> Eprime::isPresent()
token: (0)
<< Eprime::isPresent() return false
<< Eprime::isPresent() return true
<< Expr::isPresent() return true
** AST **
(2+(4*6))
4 6
*
2
+
pf3
pf4

Partial preview of the text

Download Predictive Parsing-Compiler Construction-Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

LeLeccttuurree 1 166

Here is the output trace for the expression : 2+4*

Expr::isPresent() Term::isPresent() Factor::isPresent() token: 2 (257) << Factor::isPresent() return true Tprime::isPresent() token: + (267) << Tprime::isPresent() return false << Term::isPresent() return true Eprime::isPresent() token: + (267) Term::isPresent() Factor::isPresent() token: 4 (257) << Factor::isPresent() return true Tprime::isPresent() token: * (269) Factor::isPresent() token: 6 (257) << Factor::isPresent() return true Tprime::isPresent() token: (0) << Tprime::isPresent() return false << Tprime::isPresent() return true << Term::isPresent() return true Eprime::isPresent() token: (0) << Eprime::isPresent() return false << Eprime::isPresent() return true << Expr::isPresent() return true

**** AST ** (2+(46))*

Non-recursive Predictive Parsing

It is possible to build a non-recursive predictive parser. This is done by maintaining an explicit stack and using a table. Such a parser is called a table-driven parser. The non- recursive LL(1) parser looks up the production to apply by looking up a parsing table. The LL(1) table has one dimension for current non-terminal to expand and another dimension for next token. Each table cell contains one production.

Consider the expression grammar

1 E? T E' 2 E'? + T E' 3 | (^) ε 4 T? F T' 5 T'? * F T' 6 | (^) ε 7 F? ( E ) 8 | id

Using the table construction algorithm that will be discussed later, we have the predictive parsing table

id + * ( ) $ E E? TE' E? TE' E' E'? + TE' (^) E'? ε E'? ε

T T? FT' T? FT' T' (^) T'? ε T? * FT' (^) T'? ε T'? ε F F? id F? ( E )

The rows are non-terminals and the columns are the terminals of the expression grammar. The predictive parser uses an explicit stack to keep track of pending non-terminals. It can thus be implemented without recursion.

a (^) + b $

Predictive parser

stack

X Y Z $ Parsing table M

input

output

Stack Input Ouput $ E id+id∗id$ $ E' T id+id∗id$ E? TE' $ E' T' F (^) id+id∗id$ T? FT' $ E'T' id (^) id+id∗id$ F? id $ E' T' (^) +id∗ id$ $ E' (^) +id∗ id$ T'? ε $ E' T + (^) +id∗ id$ E'? + TE' $ E' T (^) id∗ id$ $ E' T' F (^) id∗ id$ T? FT' $ E' T' id (^) id∗ id$ F? id $ E' T' ∗id$ $ E' T' F ∗ ∗id$ T? ∗ FT' $ E' T' F id$ $ E'T' id id$ F? id $ E' T' $ $ E' $ (^) T'? ε $ $ (^) E'? ε