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 and Semantics Structure of programming languages.
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.
Syntax and Semantics Structure of programming languages.
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.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
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.
Syntax Analysis By Noor Dhia Left Recursion: Example1: S → S0s1s | 01 The grammar after eliminate left recursion is: S → 01 S’ S' → 0s1sS’
Syntax and Semantics Structure of programming languages.
Context-free grammars
Parsing #1 Leonidas Fegaras.
Building AST's for RPAL Programs
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
Simplifications of Context-Free Grammars
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
CS 404 Introduction to Compiler Design
Compiler design Bottom-up parsing: Canonical LR and LALR
Top-down derivation tree generation
4 (c) parsing.
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
Bottom-up derivation tree generation
TaBle-driven LL(1) Parsing
R.Rajkumar Asst.Professor CSE
Programming Language Syntax 5
Building AST's for RPAL Programs
Replacing recursion with iteration
Programming Language Principles
Operator precedence and AST’s
Predictive Parsing Program
Operator Precedence and Associativity
Programming Language Principles
Programming Language Concepts
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Bottom-up derivation tree generation Module 08.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

Tree generation 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.

Tree generation Red: Done 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”).