Docsity

Nonrecursive Predictive Parsing - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Nonrecursive Predictive Parsing - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

1

Nonrecursive Predictive Parsing

 It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack

2

Nonrecursive Predictive Parsing

 It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack

4

Table-driven Parsers

LL(1) table:  One dimension for current non-terminal to expand  One dimension for next token  Table entry contains one production

5

Table-driven Parsers

LL(1) table:  One dimension for current non-terminal to expand  One dimension for next token  Table entry contains one production

7

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

8

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 ) Columns for next token^ Rows for current non-terminal to expand

10

Predictive Parsers

 The predictive parser uses an explicit stack to keep track of pending non- terminals

 It can thus be implemented without recursion.

11

Predictive Parsers

 The predictive parser uses an explicit stack to keep track of pending non- terminals

 It can thus be implemented without recursion.

13

LL(1) Parsing Algorithm

 The input buffer contains the string to be parsed; $ is the end-of-input marker  The stack contains a sequence of grammar symbols

14

LL(1) Parsing Algorithm

 Initially, the stack contains the start symbol of the grammar on the top of $.

16

LL(1) Parsing Algorithm

 The program considers X , the symbol on top of the stack, and a, the current input symbol.

17

LL(1) Parsing Algorithm

 These two symbols, X and a determine the action of the parser.

 There are three possibilities.

19

LL(1) Parsing Algorithm

  1. X = a = $, the parser halts and annouces successful completion.

20

LL(1) Parsing Algorithm

  1. X = a ≠ $ the parser pops X off the stack and advances input pointer to next input symbol