


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



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))*
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'? ε