Recursive Descent Parsing

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Grammar and Algorithm }
AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
LR Parsing – The Items Lecture 10 Mon, Feb 14, 2005.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()
Top-Down Parsing.
Syntax and Semantics Structure of programming languages.
Parsing. Goals of Parsing Check the input for syntactic accuracy Return appropriate error messages Recover if possible Produce, or at least traverse,
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Syntax and Semantics Structure of programming languages.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 6, 10/02/2003 Prof. Roy Levow.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
LL(1) Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 7.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Error recovery in predictive parsing An error is detected during the predictive parsing when the terminal on top of the stack does not match the next input.
Programming Languages Translator
LR Parsing – The Items Lecture 10 Fri, Feb 13, 2004.
Lecture #12 Parsing Types.
LL(1) grammars Module 07.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Table-driven parsing Parsing performed by a finite state machine.
Fixing non-ll(1) grammars
Top-down parsing cannot be performed on left recursive grammars.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Compiler design Bottom-up parsing: Canonical LR and LALR
Top-down derivation tree generation
4 (c) parsing.
CS 3304 Comparative Languages
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Definition
Recursive Descent Parsing
Bottom-up AST, original grammar
Top-down derivation tree generation
Top-down parsing Module 06.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Bottom-up derivation tree, original grammar
Bottom-up derivation tree, original grammar
TaBle-driven LL(1) Parsing
Replacing recursion with iteration
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom-up AST, original grammar
Bottom-up derivation tree generation
TaBle-driven LL(1) Parsing
Fixing non-ll(1) grammars
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
LL and Recursive-Descent Parsing
Replacing recursion with iteration
Programming Language Principles
Bottom-up derivation tree generation
Recursive descent parsing
Nonrecursive Predictive Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Programming Language Principles
Programming Language Concepts
Recursive descent parsing
Compiler design Bottom-up parsing: Canonical LR and LALR
Compiler design Review COMP 442/6421 – Compiler Design
Presentation transcript:

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

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}

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 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 : 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 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.

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

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.

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 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 for LL(1) grammars.