topdown parsing

10
A . Antony Alex MCA Dr G R D College of Science – CBE Tamil Nadu - India

Upload: antony-alex

Post on 10-Jun-2015

542 views

Category:

Technology


2 download

DESCRIPTION

Top-Down Parsing

TRANSCRIPT

Page 1: Topdown parsing

A . Antony Alex MCA

Dr G R D College of Science – CBE

Tamil Nadu - India

Page 2: Topdown parsing

Recursive Descent Parsing

• Attempt to find a leftmost derivation for the

input streaminput stream

• Attempt to construct a parse tree for the input

starting from the root and creating the nodes

of the parse tree in preorder

• A general form of top down parsing called • A general form of top down parsing called

Recursive Descent Parsing that involve

Backtracking

Page 3: Topdown parsing

Predictive Parsers

• Eliminating left recursion and left factoring the

resulting grammar, a new grammar can be resulting grammar, a new grammar can be

obtained that can be parsed by a recursive-

descent parser without backtracking

stmt if expr then stmt else stmtstmt if expr then stmt else stmt

while expr do stmt

begin stmt_list end

Page 4: Topdown parsing

Transition Diagrams for predictive

parsers

• Create an initial and final (return) state• Create an initial and final (return) state

• For each production A → X1 X2 ….. X n

create a path from the initial to the final

state with edges labeled X1 , X2 , …… X n

Page 5: Topdown parsing

Transition Diagrams for Predictive

Parsers

Example

Page 6: Topdown parsing

Grammer

• E → TE’

• E’ → +TE’ | ε• E’ → +TE’ | ε

• T → FT’

• T’ → *FT’ | ε

• F → (E) | id

Page 7: Topdown parsing

0 1

E → TE’

2E:T E’

E’ → +TE’ | ε

4 5 6E’:T E’

3+

E’:

ε

Page 8: Topdown parsing

7 8

T → FT’

9T:F T’

T’ → *FT’ | ε

11 12 13T’:F T’

10*

T’:

ε

Page 9: Topdown parsing

F → (E) | id

15 16 17F:E )

14(

15 16F: 14

id

Page 10: Topdown parsing

Thank You