Bottom-up derivation tree generation

Slides:



Advertisements
Similar presentations
Grammar and Algorithm }
Advertisements

AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
ANTLR with ASTs. Abstract Syntax Trees ANTLR can be instructed to produce ASTs for the output of the parser ANTLR uses a prefix notation for representing.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Syntax Directed Definitions Synthesized Attributes
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
LL(1) Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 7.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Context-free grammars
Parsing #1 Leonidas Fegaras.
Building AST's for RPAL Programs
Introduction to Parsing
Chapter 4 - Parsing CSCE 343.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
CS510 Compiler Lecture 4.
Context-free grammars, derivation trees, and ambiguity
Parsing and Parser Parsing methods: top-down & bottom-up
Unit-3 Bottom-Up-Parsing.
Lecture #12 Parsing Types.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Textbook:Modern Compiler Design
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
Table-driven parsing Parsing performed by a finite state machine.
Recursive Descent Parsing
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.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Syntax-Directed Translation
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
TaBle-driven LL(1) Parsing
R.Rajkumar Asst.Professor CSE
Programming Language Syntax 5
Syntax-Directed Translation
Building AST's for RPAL Programs
Replacing recursion with iteration
Programming Language Principles
Operator precedence and AST’s
Bottom-up derivation tree generation
Predictive Parsing Program
Operator Precedence and Associativity
Programming Language Principles
Programming Language Concepts
Compiler design Bottom-up parsing: Canonical LR and LALR
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Bottom-up derivation tree generation COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

Tree generation Possibilities: Leading up to: Red: now Possibilities: Derivation tree or Abstract Syntax Tree. Top-down, or Bottom-up. For original or modified grammar ! Leading up to: AST, bottom-up, for the original grammar (“the one”).

BOTTOM-UP derivation tree generation In each procedure, For each alternative, Write out the selected production rule A->ω IMMEDIATELY AFTER  is parsed.

BOTTOM-UP derivation tree generation proc S; case Next_Token of T_begin : Read(T_begin); SL(); Read(T_end); Write(S → begin SL end); T_id : Read(T_id); Read(T_:=); E(); Read(T_;); Write(S → id :=E;); otherwise Error; end end;

BOTTOM-UP derivation tree generation proc SL; S(); Z(); Write(SL → SZ); end; proc Z; case Next Token of T_begin, T_id: S();Z(); Write(Z → SZ); T_end: Write(Z → ); otherwise Error;

BOTTOM-UP derivation tree generation proc E; T(); Y(); Write(E → TY); end; proc Y; if Next Token = T_+ then Read(T_+); T(); Y(); Write (Y → +TY); else Write (Y → ) ;

BOTTOM-UP derivation tree generation proc T; P(); X(); Write (T → PX); end; proc X; if Next Token = T_* then Read(T_*); T(); Write (X → *T); else Write (X → );

BOTTOM-UP derivation tree generation proc P; case Next Token of T_(: Read(T_(); E(); Read(T_)); Write (P → (E)); T_id: Read(T_id); Write (P → id); otherwise Error; end;

Parser output P → id X → T → PX Y → Y → +TY E → TY P → (E) P → id X → Input String: begin id := (id + id) * id; end Output: P → id X → T → PX Y → Y → +TY E → TY P → (E) P → id X → T → PX X → *T Y → E → TY S → id:=E; Z → SL → SZ S → begin SL end

Bottom-up derivation tree generation Location of Write() statements: Still obvious: grammar is LL(1). Productions emitted as procedures quit, not as they start. Sequence of productions must be reversed to obtain a right- most derivation. First (of two) ways to build tree: Build tree after parsing concludes. Reverse the list of productions. Top-down, Right-most derivation. Still yucky, need to add lots of code (“current”?).

Top-down right-most derivation (post-parser) Input String: begin id := (id + id) * id; end Output: P → id X → T → PX Y → Y → +TY E → TY P → (E) P → id X → T → PX X → *T Y → E → TY S → id:=E; Z → SL → SZ S → begin SL end

Bottom-up derivation tree generation Second (of two) ways to build tree: Build tree during parsing (using stack of trees). Read(T_t) means Push(Stack,Node(T_t)). “Write(A->ω)” means “reduce” ω to A. (opposite of “derive” ω from A): Pop n=|ω| trees from Stack, Create parent node A for them, Push(S,Node(A)). Sound familiar ?

Bottom-up, stack-based, DT construction Input String: begin id := (id + id) * id; end Output: P → id X → T → PX Y → Y → +TY E → TY P → (E) P → id X → T → PX X → *T Y → E → TY S → id:=E; Z → SL → SZ S → begin SL end

Bottom-up derivation tree generation Only need to modify Read() and Write(). proc Read(T_t); if T_t ≠ Next_Token then Error(); Push(S,Node(T_t)); Next_token = scan(); end; proc Write(A->ω); Node t(A); for i=1 to |ω| do add_child(Pop(S),t,1); end; Push(S,t); Neat.

summary Red: done today Possibilities: Leading up to: Derivation tree or Abstract Syntax Tree. Top-down, or Bottom-up. For original or modified grammar ! Clean implementation, using stack of trees. Leading up to: AST, bottom-up, for the original grammar (“the one”).