Abstract Syntax Tree Discrete Mathematics and Its Applications Baojian Hua

Slides:



Advertisements
Similar presentations
C and Data Structures Baojian Hua
Advertisements

Data Structure & Abstract Data Type
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
C Module System C and Data Structures Baojian Hua
Data Structure & Abstract Data Type C and Data Structures Baojian Hua
C Intro.
Abstract Syntax Mooly Sagiv html:// 1.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 5 LECTURE: DENOTIONAL SEMANTICS OF A SIMPLE LANGUAGE : INTERPRETATION IN HASKELL.
Extensible Array C and Data Structures Baojian Hua
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Parsing Discrete Mathematics and Its Applications Baojian Hua
Functions C and Data Structures Baojian Hua
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
Virtual Machines and Compilers Discrete Mathematics and Its Applications Baojian Hua
Abstract Syntax Trees Compiler Baojian Hua
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C and Data Structures Baojian Hua
Relation Discrete Mathematics and Its Applications Baojian Hua
Lexing Discrete Mathematics and Its Applications Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Linked List C and Data Structures Baojian Hua
Tree C and Data Structures Baojian Hua
Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Functional List C and Data Structures Baojian Hua
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Graph Discrete Mathematics and Its Applications Baojian Hua
Abstract Syntax Mooly Sagiv html://
Automata and Regular Expression Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
Induction & Recursion Discrete Mathematics and Its Applications Baojian Hua
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
COMP Parsing 3 of 4 Lectures 23. Using the Scanner Break input into tokens Use Scanner with delimiter: public void parse(String input ) { Scanner.
Abstract Syntax Trees Compiler Baojian Hua
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 12 – C: structs, linked lists, and casts.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
Bernd Fischer COMP2010: Compiler Engineering Abstract Syntax Trees.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
Day 3: The Command and Visitor Patterns. Preliminaries The Java static type system uses simple rules to infer types for Java expressions. The inferred.
CS 603: Programming Language Organization Lecture 6 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
COMP261 Lecture 18 Parsing 3 of 4.
Programming Languages Dan Grossman 2013
CS 614: Theory and Construction of Compilers
To understand recursion, you have to understand recursion!
CSE 374 Programming Concepts & Tools
Discrete Mathematics and
null, true, and false are also reserved.
Java Programming Language
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2017.
Programming Language C Language.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
Presentation transcript:

Abstract Syntax Tree Discrete Mathematics and Its Applications Baojian Hua

Grammar Consider this simple grammar: exp -> exp + exp | exp-exp | num Sample program: The set of nums in this exp is {4, 3, 5}

Abstract Syntax Tree (AST) Consider this simple grammar: exp -> exp + exp | exp-exp | num Sample program: Our goal: represent such kind of trees in a computer!

Abstract Syntax Tree In C

Union Recall the definition of a union: union uuu { int i; double f; }; union uuu u; u.i = 99; u.f? // Dangerous! i f

Tagged Union struct tu { enum {INT, DOUBLE} kind; union uuu { int i; double f; } u; }; struct tu x; x.u.i = 99; // atomic operations x.kind = INT; if (x.kind==INT) … // Perfect! :-P i f kind

AST Recall the grammar for expression: exp -> exp+exp | exp-exp | num Oh, it ’ s just a kind of (tagged) union! :-)

The Interface #ifndef MINIC_H #define MINIC_H typedef struct exp *exp; struct exp { enum {ADD, SUB, NUM} kind; // +, -, integer union { struct{ exp e1; exp e2; } binop; // left binop right int num; // num } u; }; // functions on next slide

The Interface // miniC interface continued // constructors for building the tree exp newExpAdd (exp e1, exp e2); exp newExpSub (exp e1, exp e2); exp newExpNum (int i); // return the set of numbers in expression e set numsSet (exp e); #endif

Implementation #include “miniC.h” exp newExpAdd (exp e1, exp e2) { exp e = malloc (sizeof (*e)); e->kind = ADD; (e->u).binop.e1 = e1; (e->u).binop.e2 = e2; return e; } // others are similar, leave to you

Implementation #include “miniC.h” // The definiton of numsSet. For simplicity, we // write “f” for this function. { f(e1)  f(e2), if e is e1+e2; f(e)= { f(e1)  f(e2), if e is e1-e2; { {num}, otherwise. (e1 is num)

Implementation #include “miniC.h” // From definition to code. This function // familiarize you with recursion functions. set numsSet (exp e){ switch (e->kind){ case ADD: { return setUnion (numsSet ((e->u).binop.e1), numsSet ((e->u).binop.e2)); } case SUB: … // similar case NUM: return setInsert (newSet(), (e->u).i); }

Client Code int main () { // exp e1 = newExpNum (4); exp e2 = newExpNum (3); exp e3 = newExpNum (5); exp e4 = newExpNum (4); exp e5 = newExpAdd (e1, e2); exp e6 = newExpSub (e5, e3); exp e7 = newExpAdd (e6, e4); // the final tree numsSet (e7); // number set return 0; }

Client Code int main () { // exp e1 = newExpNum (4); exp e2 = newExpNum (3); exp e3 = newExpNum (5); exp e4 = newExpNum (4); exp e5 = newExpAdd (e1, e2); exp e6 = newExpSub (e5, e3); exp e7 = newExpAdd (e6, e4); // the final tree numsSet (e7); // number set return 0; }

Client Code int main () { // exp e1 = newExpNum (4); exp e2 = newExpNum (3); exp e3 = newExpNum (5); exp e4 = newExpNum (4); exp e5 = newExpAdd (e1, e2); exp e6 = newExpSub (e5, e3); exp e7 = newExpAdd (e6, e4); // the final tree numsSet (e7); // number set return 0; }

Client Code int main () { // exp e1 = newExpNum (4); exp e2 = newExpNum (3); exp e3 = newExpNum (5); exp e4 = newExpNum (4); exp e5 = newExpAdd (e1, e2); exp e6 = newExpSub (e5, e3); exp e7 = newExpAdd (e6, e4); // the final tree numsSet (e7); // number set return 0; }

Client Code int main () { // exp e1 = newExpNum (4); exp e2 = newExpNum (3); exp e3 = newExpNum (5); exp e4 = newExpNum (4); exp e5 = newExpAdd (e1, e2); exp e6 = newExpSub (e5, e3); exp e7 = newExpAdd (e6, e4); // the final tree numsSet (e7); // number set return 0; }

Client Code int main () { // exp e1 = newExpNum (4); exp e2 = newExpNum (3); exp e3 = newExpNum (5); exp e4 = newExpNum (4); exp e5 = newExpAdd (e1, e2); exp e6 = newExpSub (e5, e3); exp e7 = newExpAdd (e6, e4); // the final tree numsSet (e7); // number set return 0; }

Client Code int main () { // exp e1 = newExpNum (4); exp e2 = newExpNum (3); exp e3 = newExpNum (5); exp e4 = newExpNum (4); exp e5 = newExpAdd (e1, e2); exp e6 = newExpSub (e5, e3); exp e7 = newExpAdd (e6, e4); // the final tree numsSet (e7); // number set return 0; }

Client Code int main () { // exp e1 = newExpNum (4); exp e2 = newExpNum (3); exp e3 = newExpNum (5); exp e4 = newExpNum (4); exp e5 = newExpAdd (e1, e2); exp e6 = newExpSub (e5, e3); exp e7 = newExpAdd (e6, e4); // the final tree numsSet (e7); // number set return 0; }

Abstract Syntax Tree In Java

Union in Java? Java does not support explicit unions directly :-( However, the class hierarchy of Java implicitly support this :-) Consider again: union { int i; double f; } x;

Union in Java? // A mimic of unions in Java using two classes: class MyInt{ int i; … } class MyDouble{ double f; … } // Why?

Local Class Hierarchy // However, this hierarchy is too loose, so we // want to build a local class hierarchy: abstract class Exp{} class ExpAdd extends Exp { Exp e1; Exp e2; … // constructors and methods } class ExpSub extends Exp { // similar } class ExpNum extends Exp { int i; … // constructors and methods }

In Picture ExpNumExpSubExpAdd Exp

Local Class Hierarchy // Operations: (ugly version) Set numsSet (Exp e){ if (e instanceof ExpAdd){ … } else if (e instanceof ExpSub) { … } else if (e instanceof ExpNum) { … } else throw new Error (…); } // Or, use the visitor pattern.

Client Code // In main method: Exp e1 = new ExpNum (4); Exp e2 = new ExpNum (3); Exp e3 = new ExpNum (5); Exp e4 = new ExpNum (4); Exp e5 = new ExpAdd (e1, e2); Exp e6 = new ExpSub (e5, e3); Exp e7 = new ExpAdd (e6, e4); Set set = e7.numsSet ();