Programming Language Principles

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.
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.
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.
Attribute Grammars Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 17.
Prof. Bodik CS 164 Lecture 51 Building a Parser I CS164 3:30-5:00 TT 10 Evans.
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.
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.
LL(1) Parser. What does LL signify ? The first L means that the scanning takes place from Left to right. The first L means that the scanning takes place.
Writing RPAL Programs Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 13.
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.
Context-Free Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.
An Attribute Grammar for Tiny Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 18.
LL(1) Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 7.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
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.
Announcements/Reading
Programming Language Concepts
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.
Parsing and Parser Parsing methods: top-down & bottom-up
Lecture #12 Parsing Types.
Overview of Compilation The Compiler Front End
Table-driven parsing Parsing performed by a finite state machine.
Recursive Descent Parsing
An Attribute Grammar for Tiny
Syntax Analysis Chapter 4.
Compiler design Bottom-up parsing: Canonical LR and LALR
Top-down derivation tree generation
Programming Language Principles
4 (c) parsing.
Programming Language Concepts
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
Bottom-up derivation tree, original grammar
Bottom-up derivation tree, original grammar
TaBle-driven LL(1) Parsing
Replacing recursion with iteration
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Bottom-up AST, original grammar
Bottom-up derivation tree generation
TaBle-driven LL(1) Parsing
R.Rajkumar Asst.Professor CSE
Programming Language Syntax 5
Programming Language Principles
Building AST's for RPAL Programs
Replacing recursion with iteration
Programming Language Principles
Programming Language Principles
Operator precedence and AST’s
Bottom-up derivation tree generation
Expr ( ). Expr ( ) E  T E’ E’  + T E’ | ε T  F T’ T’  * F T’ | ε F  ( E ) | id Orig. Grammar LL(1) Grammar E  E + T | T T  T.
Optimizations for the CSE Machine
Operator Precedence and Associativity
Programming Language Concepts
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Programming Language Principles Tree Generation Programming Language Principles Lecture 5 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida

String-To-Tree Transduction Can obtain derivation or abstract syntax tree. Tree can be generated top-down, or bottom-up. We will show how to obtain Derivation tree top-down AST for the original grammar, bottom-up.

Top-Down Generation of Derivation Tree In each procedure, and for each alternative, write out the appropriate production AS SOON AS IT IS KNOWN

Top-Down Generation of Derivation Tree (cont’d) proc S; {S → begin SL end → id := E; } case Next_Token of T_begin : Write(S → begin SL end); Read(T_begin); SL; Read(T_end);

Top-Down Generation of Derivation Tree (cont’d) T_id : Write(S → id :=E;); Read(T_id); Read (T_:=); E; Read (T_;); otherwise Error end end;

Top-Down Generation of Derivation Tree (cont’d) proc SL; {SL → SZ} Write(SL → SZ); S; Z; end; proc E; {E → TY} Write(E → TY); T; Y;

Top-Down Generation of Derivation Tree (cont’d) proc Z; {Z → SZ → } case Next_Token of T_begin, T_id: Write(Z → SZ); S; Z; T_end: Write(Z → ); otherwise Error; end end;

Top-Down Generation of Derivation Tree (cont’d) proc Y; {Y → +TY → } if Next_Token = T_+ then Write (Y → +TY); Read (T_+); T; Y; else Write (Y → ); end;

Top-Down Generation of Derivation Tree (cont’d) proc T; {T → PX} Write (T → PX); P; X end; proc X;{X → *T → }

Top-Down Generation of Derivation Tree (cont’d) if Next_Token = T_* then Write (X → *T); Read (T_*); T; else Write (X → ); end;

Top-Down Generation of Derivation Tree (cont’d) proc P;{P → (E) → id } case Next_Token of T_(: Write (P → (E)); Read (T_(); E; Read (T_)); T_id: Write (P → id); Read (T_id); otherwise Error; end;

Notes The placement of the Write statements is obvious precisely because the grammar is LL(1). Can build the tree “as we go”, or have it built by a post-processor.

Example 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 →

Bottom-up Generation of the Derivation Tree We could have placed the write statements at the END of each phrase, instead of the beginning. If we do, the tree will be generated bottom-up. In each procedure, and for each alternative, write out the production A   AFTER  is parsed.

Bottom-up Generation of the Derivation Tree (cont’d) proc S;{S → begin SL end → id := E; } 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;

Bottom-up Generation of the Derivation Tree (cont’d) proc SL; {SL → SZ} S; Z; Write(SL → SZ); end; proc E; {E → TY} T; Y; Write(E → TY);

Bottom-up Generation of the Derivation Tree (cont’d) proc Z; {Z → SZ → } case Next_Token of T_begin, T_id: S; Z; Write(Z → SZ); T_end: Write(Z → ); otherwise Error; end end;

Bottom-up Generation of the Derivation Tree (cont’d) proc Y; {Y → +TY → } if Next_Token = T_+ then Read (T_+); T; Y; Write (Y → +TY); else Write (Y → ); end;

Bottom-up Generation of the Derivation Tree (cont’d) proc T; {T → PX } P; X; Write (T → PX) end; proc X;{X → *T → } if Next_Token = T_* then Read (T_*); T; Write (X → *T); else Write (X → ); end

Bottom-up Generation of the Derivation Tree (cont’d) proc P;{P → (E) → id } case Next_Token of T_(: Read (T_(); E; Read (T_)); Write (P → (E)); T_id: Read (T_id); Write (P → id); otherwise Error; end;

Notes The placement of the Write statements is still obvious. The productions are emitted as procedures quit, not as they start.

Notes (cont’d) Productions emitted in reverse order, i.e., the sequence of productions must be used in reverse order to obtain a right-most derivation. Again, can built tree “as we go” (need stack of trees), or later.

Example Input String: begin id := (id + id) * id; end Output: P → id 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

Programming Language Principles Tree Generation Programming Language Principles Lecture 5 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida