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

Slides:



Advertisements
Similar presentations
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.
Advertisements

Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Context-Free Grammars Lecture 7
Parsing III (Eliminating left recursion, recursive descent parsing)
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Syntax and Semantics Structure of programming languages.
Attribute Grammars Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 17.
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
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.
Introduction to Parsing
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Writing RPAL Programs Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 13.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
An Attribute Grammar for Tiny Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 18.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
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 and Semantics Structure of programming languages.
Programming Language Concepts
Parsing #1 Leonidas Fegaras.
Parsing — Part II (Top-down parsing, left-recursion removal)
Building AST's for RPAL Programs
A Simple Syntax-Directed Translator
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
Lecture #12 Parsing Types.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
Recursive Descent Parsing
An Attribute Grammar for Tiny
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Top-down derivation tree generation
Programming Language Principles
Recursive Descent Parsing
Bottom-up AST, original grammar
Top-down derivation tree generation
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
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
Programming Language Syntax 5
Parsing IV Bottom-up Parsing
Parsing — Part II (Top-down parsing, left-recursion removal)
Building AST's for RPAL Programs
Syntax Analysis - Parsing
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
OPERATOR GRAMMAR No Ɛ-transition. No two adjacent non-terminals. Eg.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

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

Replacing Recursion with Iteration Not all the nonterminals are needed. The recursion in SL, X, Y and Z can be replaced with iteration.

Replacing Recursion with Iteration (cont’d) proc S; {S → begin SL end → id := E; case Next_Token of T_begin :Read(T_begin); repeat S; until Next_Token  {T_begin,T_id}; Read(T_end); T_id : Read(T_id); Read (T_:=); E; Read (T_;); otherwiseError; end end; Replaces recursion on Z. Replaces call to SL. SL SL → S Z Z → S Z → }

Replacing Recursion with Iteration (cont’d) proc E; {E → TY Y → +TY → } T; while Next_Token = T_+ do Read (T_+); T; od end; Replaces recursion on Y.

Replacing Recursion with Iteration (cont’d) proc T; {T → PX X → *T → } P; if Next_Token = T_* thenRead (T_*); T; end; Replaces call to X.

Replacing Recursion with Iteration (cont’d) proc P;{P → (E) → id } case Next_Token of T_(: Read (T_(); E; Read (T_)); T_id: Read (T_id); otherwise Error; end end;

Construction of Derivation Tree for the Original Grammar (Bottom Up) proc S; { (1)S → begin SL end (2)S → begin SL end → id := E; → id := E; SL → SZ SL → SL S Z → SZ → S → } case Next_Token of T_begin : Read(T_begin); S; Write (SL → S); while Next_Token in {T_begin,T_id} do S; Write (SL → SL S); od Read(T_end); Write (S → begin SL end);

Construction of Derivation Tree for the Original Grammar (Bottom Up) (cont’d) T_id : Read(T_id); Read (T_:=); E; Read (T_;); Write (S → id :=E ;); otherwise Error; end end;

Construction of Derivation Tree for the Original Grammar (Bottom Up) (cont’d) proc E; {(1)E → TY (2) E → E+T Y → +TY → T → } T; Write (E → T); while Next_Token = T_+ do Read (T_+); T; Write (E → E+T); od end

Construction of Derivation Tree for the Original Grammar (Bottom Up) (cont’d) proc T; {(1)T → PX (2) T → P*T X → *T → P → } P; if Next_Token = T_* thenRead (T_*); T; Write (T → P*T) else Write (T → P); end;

Construction of Derivation Tree for the Original Grammar (Bottom Up) (cont’d) proc P;{(1)P → (E) (2)P → (E) → id → id } // SAME AS BEFORE end;

Example Input String : begin id := (id + id) * id; end Output : P → id T → P E → T P → id T → P E → E+T P → (E) P → id T → P T → P*T E → T S → id:=E; SL → S S → begin SL end

Generating the Abstract Syntax Tree, Bottom Up, for the Original Grammar proc S; { S → begin S+ end  'block' → id := E;  'assign' var N:integer; case Next_Token of T_begin :Read(T_begin); S; N:=1; while Next_Token in {T_begin,T_id} do S; N:=N+1; od Read(T_end); Build Tree ('block',N); T_id : Read(T_id); Read (T_:=); E; Read (T_;); Build Tree ('assign',2); otherwise Error end end; Assume this builds a node. Build Tree (‘x’,n) pops n trees from the stack, builds an ‘x’ node as their parent, and pushes the resulting tree.

Generating the Abstract Syntax Tree, Bottom Up, for the Original Grammar (cont’d) proc E; {E → E+T  '+' → T } T; while Next_Token = T_+ do Read (T_+) T; Build Tree ('+',2); od end; Left branching in tree!

Generating the Abstract Syntax Tree, Bottom Up, for the Original Grammar (cont’d) proc T; {T → P*T  '*' → P } P; if Next_Token = T_* thenRead (T_*) T; Build Tree ('*',2); end; Right branching in tree!

Generating the Abstract Syntax Tree, Bottom Up, for the Original Grammar (cont’d) proc P;{P → (E) → id } // SAME AS BEFORE, // i.e.,no trees built end;

Example Input String : begin id 1 := (id 2 + id 3 ) * id 4 ; end Sequence of events : id 1 id 2 id 3 id 4 BT( ' + ',2) BT( ' * ',2) BT( ' assign ',2) BT( ' block ',1)

Summary Bottom-up or top-down tree construction. Original or modified grammar. Derivation Tree or Abstract Syntax Tree. Technique of choice (Hint: project) Top-down, recursive descent parser. Bottom-up tree construction for the original grammar.

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