Bottom-up AST, original grammar

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)
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
COP4020 Programming Languages Semantics Prof. Xin Yuan.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
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.
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.
Announcements/Reading
Programming Language Concepts
Context-free grammars
Parsing #1 Leonidas Fegaras.
Building AST's for RPAL Programs
A Simple Syntax-Directed Translator
Introduction to Parsing
Parsing & Context-Free Grammars
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)
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
Fixing non-ll(1) grammars
Compiler Construction
Overview of Compilation The Compiler BACK End
An Attribute Grammar for Tiny
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
Top-down derivation tree generation
4 (c) parsing.
Binary Search Tree In order Pre order Post order Search Insertion
CS 3304 Comparative Languages
Syntax-Directed Translation
Top-Down Parsing CS 671 January 29, 2008.
Intermediate Representations Hal Perkins Autumn 2011
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
Bottom-up derivation tree generation
TaBle-driven LL(1) Parsing
Programming Language Syntax 5
Fixing non-ll(1) grammars
Building AST's for RPAL Programs
Replacing recursion with iteration
Programming Language Principles
Recursion and Rpal’s synTax
Operator precedence and AST’s
The Recursive Descent Algorithm
Bottom-up derivation tree generation
Compiler Construction
Operator Precedence and Associativity
Programming Language Principles
Programming Language Concepts
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Bottom-up AST, original grammar Module 09.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

Topics Possibilities: OUR FINAL GOAL ! (“the one”) Red: now Possibilities: Derivation tree, or Abstract Syntax Tree. Top-down, or Bottom-up. For original, or modified grammar ! OUR FINAL GOAL ! (“the one”) This is THE way to build a parser by hand.

AST, Bottom-UP, original grammar proc S int N=1; case Next_Token of T_begin : Read(T_begin); S(); while Next_Token ∊ {T_begin,T_id} do N++; Read(T_end); Build_tree(’block’, N); T_id : Read(T_id); Read (T_:=); E(); Read (T_;); Build_tree(’assign’,2); otherwise Error; end; S → begin S+ end => ’block’ → id := E ; => ’assign’ Build Tree (’x’,n): Pop n trees, Build ’x’ parent node, Push new tree. Read() no longer builds tree nodes, except for <id>, <int>, etc.

AST, Bottom-UP, original grammar proc E; T(); while Next_Token = T_+ do Read (T_+); Build_tree(’+’,2); od; end; proc T; P(); if Next_Token = T_* then Read (T_*); T(); Build_tree(’*’,,2); E → E+T => ’+’ → T T → P*T => ’*’ → P Left branching ! Right branching !

AST, Bottom-UP, original grammar proc P; case Next Token of T_(: Read(T_(); E(); Read(T_)); T_id: Read(T_id); otherwise Error; end; P →(E) → id No Build_tree() necessary

Parser output S → begin S+ end => ’block’ Input String: begin id1 := (id2 + id3) * id4; end Output (Tree-building actions): S → begin S+ end => ’block’ → id := E ; => ’assign’ E → E+T => ’+’ → T T → P*T => ’*’ → P P →(E) → id BT(id1,0) BT(id2,0) BT(id3,0) BT(‘+’,2) BT(id4,0) BT(’*’,2) BT(’assign’,2) BT(’block’,1) All along, THIS was our “model” grammar ! THIS is the technique of choice.

How to write a parser by hand Starting point: A Regular Right-Part, Syntax-Directed Translation Scheme Write parser directly from the grammar. There’s (likely) an LL(1) grammar lurking in there, but Don’t need to write it explicitly. Calculate Select sets, er, well … selectively. Don’t need Derivation Tree. Recognize patterns, build code. This is THE way to build a parser by hand.

First-child, next-sibling trees A binary tree, used to represent n-ary (general) trees. Left child is first child. Right child is next sibling. N-ary tree: First-child, next-sibling tree:

Advantage of first-child, next-sibling trees Pre-order traversal is the same. Useful to print a tree, in indented format. a(3) ... b(0) ... c(3) ...... e(0) ...... f(0) ...... g(0) ... d(0)

The build_tree procedure proc Build_tree(x,n); p=nil; for i=1 to n do c=pop(S); c.right=p; p=c; end; Push(S,node(x,p,nil)); Build_tree(‘x’,3): Result: works with n=0, too.

Topics Possibilities: OUR FINAL GOAL ! (“the one”) Red: done today Possibilities: Derivation tree, or Abstract Syntax Tree. Top-down, or Bottom-up. For original, or modified grammar ! OUR FINAL GOAL ! (“the one”) This is THE way to build a parser by hand.