Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Cs419 lec8 top-down parsing

876
views

Published on

Published in: Education

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
876
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
40
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
• 2. Dr. Hussien M. Sharaf 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. 
• 3. Dr. Hussien M. Sharaf 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. Scanner Next token Parser driver Parsing table Output Parsing stack
• 4. Dr. Hussien M. Sharaf PROBLEMS FACING LL(1) PARSERS 1. 2. Left recursion. Left factoring. Both problems prevents any LL parser from deciding deterministically which rule should be fired.
• 5. Dr. Hussien M. Sharaf 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.
• 6. Dr. Hussien M. Sharaf EXAMPLE 1 Consider the following grammar: S  ( S ) | to parse (()), we follow these steps: Parser stack Input Parser action S (())\$ Predict S (S) (S) (())\$ Match ( S) ())\$ Predict S (S) (S)) ())\$ Match ( S)) ))\$ Predict S )) ))\$ Match ) ) )\$ Match ) Empty \$ Accept
• 7. Dr. Hussien M. Sharaf EXAMPLE 2 Consider the following grammar: 1.E 2.Q 3.Q 4.Q 5.T TQ +TQ -TQ FR 6.R 7.R 8.R 9.F 10.F *FR /FR (E) id
• 8. Dr. Hussien M. Sharaf EXAMPLE 2 (CONT.) The parsing table for id*(id+id)\$ is: + E Q T R F - 2 * / ( 1 3 ) 4 5 8 8 id 1 6 7 4 5 8 9 \$ 8 10
• 9. Dr. Hussien M. Sharaf EXAMPLE 2 Consider the following grammar: 1.E 2.Q 3.Q 4.Q 5.T TQ +TQ -TQ FR 6.R 7.R 8.R 9.F 10.F *FR /FR (E) id
• 10. Dr. Hussien M. Sharaf EXAMPLE 2 (CONT.) To parse id*(id+id)\$, we follow these steps: stack Input Parser action id RQ)RQ id+id)\$ E id*(id+id)\$ Predict E TQ RQ)RQ +id)\$ Predict R TQ id*(id+id)\$ Predict T FR Q)RQ +id)\$ Predict Q +TQ FRQ id*(id+id)\$ Predict F id +TQ)RQ +id)\$ Match + id RQ id*(id+id)\$ Match id TQ)RQ id)\$ Predict T FR RQ *(id+id)\$ Predict R*FR FRQ)RQ id)\$ Predict Fid *FRQ *(id+id)\$ Match * id RQ)RQ id)\$ Match id FRQ (id+id)\$ Predict F(E) RQ)RQ )\$ Predict R (E)RQ (id+id)\$ Match ( Q)RQ )\$ Predict Q E)RQ id+id)\$ Predict ETQ )RQ )\$ Match ) TQ)RQ id+id)\$ Predict T FR RQ \$ Predict R FRQ)RQ id+id)\$ Predict F id Q \$ Predict Q id RQ)RQ id+id)\$ Match id Empty \$ Accept Match id
• 11. Dr. Hussien M. Sharaf THANK YOU