Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Descent Parsing

Similar presentations


Presentation on theme: "Recursive Descent Parsing"— Presentation transcript:

1 Recursive Descent Parsing
COP4620 – Programming LanguageTranslators Dr. Manuel E. Bermudez

2 Grammars (So FAR) Our “model” PL grammar. Modified, LL(1)) grammar.
S → begin SL end {begin} → id := E; {id} SL → SL S {begin,id} → S {begin,id} E → E+T {(,id} → T {(,id} T → P*T {(,id} → P {(,id} P → (E) {(} → id {id} S → begin SL end {begin} → id := E ; {id} SL → S Z {begin,id} Z → S Z {begin,id} → {end} E → T Y {(,id} Y → + T Y {+} → {;,)} T → P X {(,id} X → * T {*} → {;,+,)} P → (E) {(} → id {id}

3 Parse table Can parse using this table.
Tedious (Both parsing and PT construction)

4 Recursive Descent Parsing
Top-down parsing strategy, for LL(1) grammars. One procedure per nonterminal. Stack contents embedded in recursive call sequence. Each procedure “commits” to one production, based on the next input symbol, and the select sets. Good technique for hand-written parsers.

5 Recursive descent parser
proc S; case Next_Token of T_begin : Read(T_begin); SL(); Read(T_end); T_id : Read(T_id); Read(T_:=); E(); Read(T_;); otherwise Error; end; “Next_Token” is the upcoming token. Assume it is initialized. “Read (T_X)” verifies that the upcoming token is X, and consumes it.

6 Recursive descent parser
proc SL; S(); Z(); end; proc Z; case Next Token of T_begin, T_id: S();Z(); T_end: ; otherwise Error; SL: Technically, should insist Next Token ∊ { T_begin, T_id}, but S will do that anyway. Checking earlier or later ? Aid error recovery.

7 Recursive descent parser
proc E; T(); Y(); end; proc Y; if Next Token = T_+ then Read(T_+); E: Technically, should insist Next Token ∊ { T_id, T_( }, but T will do that anyway. Y: Could have used a case statement

8 Recursive descent parser
proc T; P(); X(); end; proc X; if Next Token = T_* then Read(T_*); T(); T: Could have checked for T_( and T_id.. X: Could have used a case statement.

9 Recursive descent parser
proc P; case Next Token of T_(: Read(T_(); E(); Read(T_)); T_id: Read(T_id); otherwise Error; end;

10 Tracing the parser Not so easy (as table-driven)
In table-driven parser, stack keeps all future (predicted) symbols. Stack (towards the end): Next item is obvious. In RD parser, stack keeps recursive calling sequence.

11 summary Recursive Descent Parsing Manually coded parser.
PT information built into the code. Suitable for LL(1) grammars.


Download ppt "Recursive Descent Parsing"

Similar presentations


Ads by Google