RECURSIVE DESCENT PARSING COP 4620 Programming Language Translators

  • Slides: 11
Download presentation
RECURSIVE DESCENT PARSING COP 4620 – Programming Language. Translators Dr. Manuel E. Bermudez

RECURSIVE DESCENT PARSING COP 4620 – Programming Language. Translators Dr. Manuel E. Bermudez

GRAMMARS (SO FAR) Our “model” PL grammar. S → → SL → → E

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

PARSE TABLE Can parse using this table. Tedious (Both parsing and PT construction)

PARSE TABLE Can parse using this table. Tedious (Both parsing and PT construction)

RECURSIVE DESCENT PARSING • Top-down parsing strategy, for LL(1) grammars. • One procedure per

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.

RECURSIVE DESCENT PARSER proc S; case Next_Token of T_begin : Read(T_begin); SL(); Read(T_end); T_id

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.

RECURSIVE DESCENT PARSER proc SL; S(); Z(); end; proc Z; case Next Token of

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

RECURSIVE DESCENT PARSER proc E; T(); Y(); end; proc Y; if Next Token =

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

RECURSIVE DESCENT PARSER proc T; P(); X(); end; T: Could have checked for T_(

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

RECURSIVE DESCENT PARSER proc P; case Next Token of T_(: Read(T_(); E(); Read(T_)); T_id:

RECURSIVE DESCENT PARSER proc P; case Next Token of T_(: Read(T_(); E(); Read(T_)); T_id: Read(T_id); otherwise Error; end;

TRACING THE PARSER Not so easy (as table-driven) In table-driven parser, stack keeps all

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.

SUMMARY Recursive Descent Parsing Manually coded parser. PT information built into the code. Suitable

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