Top-down derivation tree generation

Slides:



Advertisements
Similar presentations
AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
Advertisements

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.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Context-Free Grammars Lecture 7
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
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.
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.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
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.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
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.
Context-Free Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
LL(1) Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 7.
Context-free grammars
Building AST's for RPAL Programs
Introduction to Parsing
Chapter 4 - Parsing CSCE 343.
Programming Languages Translator
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.
LL(1) grammars Module 07.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
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
Recursive Descent Parsing
Fixing non-ll(1) grammars
An Attribute Grammar for Tiny
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
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
Overview of Compilation The Compiler BACK End
Bottom-up AST, original grammar
Bottom-up derivation tree generation
TaBle-driven LL(1) Parsing
R.Rajkumar Asst.Professor CSE
Programming Language Syntax 5
Fixing non-ll(1) grammars
First, Follow and Select sets
Regular Expression to NFA
Building AST's for RPAL Programs
Replacing recursion with iteration
Programming Language Principles
Lesson Objectives Aims Understand Syntax Analysis.
Operator precedence and AST’s
Bottom-up derivation tree generation
Compiler Construction
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Operator Precedence and Associativity
Programming Language Principles
Programming Language Concepts
Presentation transcript:

Top-down derivation tree generation COP4620 – Programming Language Translators 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”).

Top-down derivation tree generation In each procedure, For each alternative, Write out the selected production rule AS SOON AS IT IS KNOWN

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

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

Top-down derivation tree generation proc E; Write(E → TY); T(); Y(); end; proc Y; if Next Token = T_+ then Write (Y → +TY); Read(T_+); T(); Y(); else Write (Y → ) ; // new: else clause

Top-down derivation tree generation proc T; Write (T → PX); P(); X(); end; proc X; if Next Token = T_* then Write (X → *T); Read(T_*); T(); else Write (X → ); // new: else clause T: Could have checked for T_( and T_id.. X: Could have used a case statement.

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

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

Top-down derivation tree generation Obvious locations of the Write() statements: Locations where PT Lookups would take place. Works because grammar is LL(1). Two ways to build tree: As parsing proceeds (“as we go”). “Write(A->ω)” (or process A->ω) means: Create tree nodes for ω under “current”; Update current (complicated: need “current” for each procedure). Yuck. Better way ? Yep: Bottom-up ! (Coming Soon …) Build tree after parsing concludes.

summary Red: done Possibilities: Leading up to: 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”).