cs419 lec8 top-down parsing
DESCRIPTION
TRANSCRIPT
![Page 1: Cs419 lec8 top-down parsing](https://reader036.vdocuments.us/reader036/viewer/2022082804/546ce02baf7959ea368b479a/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082804/546ce02baf7959ea368b479a/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082804/546ce02baf7959ea368b479a/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082804/546ce02baf7959ea368b479a/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082804/546ce02baf7959ea368b479a/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082804/546ce02baf7959ea368b479a/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082804/546ce02baf7959ea368b479a/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082804/546ce02baf7959ea368b479a/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082804/546ce02baf7959ea368b479a/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082804/546ce02baf7959ea368b479a/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082804/546ce02baf7959ea368b479a/html5/thumbnails/11.jpg)
THANK YOU
Dr. Hussien M. Sharaf