cs419 lec8 top-down parsing

11
Compilers WELCOME TO A JOURNEY TO CS419 Lecture 8 Top-Down Parsing Cairo University FCI Dr. Hussien Sharaf Computer Science Department [email protected]

Upload: arab-open-university-and-cairo-university

Post on 20-Nov-2014

555 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Cs419 lec8    top-down parsing

Compilers

WELCOME TO A JOURNEY TO

CS419 Lecture 8

Top-Down Parsing

Cairo UniversityFCI

Dr. Hussien SharafComputer Science [email protected]

Page 2: Cs419 lec8    top-down parsing

TOP-DOWN PARSING

A parser is a top-down if it discovers a parse tree top to bottom.

A top-down parse corresponds to a preorder traversal of the parse tree.

A left most derivation is applied to each step.

Dr. Hussien M. Sharaf

S

dc

A ba

Page 3: Cs419 lec8    top-down parsing

LL PARSING LL parsing is a technique of top-down parsing. Consists of:

Parser stack: that holds grammar symbols: non-terminals and tokens.

Parsing table: that specifies the parser actions (Match, Predict, Accept, Error).

Driver function: that interacts with parser stack, parsing table, and scanner.

Dr. Hussien M. Sharaf

ScannerParser driver

Parsing table

OutputNexttoken

Parsing stack

Page 4: Cs419 lec8    top-down parsing

PROBLEMS FACING LL(1) PARSERS

1. Left recursion.2. Left factoring.Both problems prevents any LL parser from deciding deterministically which rule should be fired.

Dr. Hussien M. Sharaf

Page 5: Cs419 lec8    top-down parsing

TOP-DOWN PARSER ACTIONS

Match: to match top of parser stack with next input token.

Predict: to predict a production and apply it in a derivation step.

Accept: parsed successfully. Error: failure.

Dr. Hussien M. Sharaf

Page 6: Cs419 lec8    top-down parsing

EXAMPLE 1Consider the following grammar: S ( S ) | to parse (()), we follow these steps:

Dr. Hussien M. Sharaf

Parser stack Input Parser action

S (())$ Predict S (S)

(S) (())$ Match (

S) ())$ Predict S (S)

(S)) ())$ Match (

S)) ))$ Predict S

)) ))$ Match )

) )$ Match )

Empty $ Accept

Page 7: Cs419 lec8    top-down parsing

EXAMPLE 2Consider the following grammar:

Dr. Hussien M. Sharaf

1.E TQ 6.R *FR2.Q +TQ 7.R /FR3.Q -TQ 8.R 4.Q 9.F (E)5.T FR 10.F id

Page 8: Cs419 lec8    top-down parsing

EXAMPLE 2 (CONT.)

Dr. Hussien M. Sharaf

+ - * / ( ) id $

E 1 1

Q 2 3 4 4

T 5 5

R 8 8 6 7 8 8

F 9 10

The parsing table for id*(id+id)$ is:

Page 9: Cs419 lec8    top-down parsing

EXAMPLE 2Consider the following grammar:

Dr. Hussien M. Sharaf

1.E TQ 6.R *FR2.Q +TQ 7.R /FR3.Q -TQ 8.R 4.Q 9.F (E)5.T FR 10.F id

Page 10: Cs419 lec8    top-down parsing

EXAMPLE 2 (CONT.)

Dr. Hussien M. Sharaf

stack Input Parser action

E id*(id+id)$

Predict E TQ

TQ id*(id+id)$

Predict T FR

FRQ id*(id+id)$

Predict F id

id RQ id*(id+id)$

Match id

RQ *(id+id)$ Predict R*FR

*FRQ *(id+id)$ Match *

FRQ (id+id)$ Predict F(E)

(E)RQ (id+id)$ Match (

E)RQ id+id)$ Predict ETQ

TQ)RQ id+id)$ Predict T FR

FRQ)RQ id+id)$ Predict F id

id RQ)RQ id+id)$ Match id

id RQ)RQ id+id)$ Match id

RQ)RQ +id)$ Predict R

Q)RQ +id)$ Predict Q +TQ

+TQ)RQ +id)$ Match +

TQ)RQ id)$ Predict T FR

FRQ)RQ id)$ Predict Fid

id RQ)RQ id)$ Match id

RQ)RQ )$ Predict R

Q)RQ )$ Predict Q

)RQ )$ Match )

RQ $ Predict R

Q $ Predict Q

Empty $ Accept

To parse id*(id+id)$, we follow these steps:

Page 11: Cs419 lec8    top-down parsing

THANK YOU

Dr. Hussien M. Sharaf