Compiler Construction Abstract Syntax Tree Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.

Slides:



Advertisements
Similar presentations
JavaCUP JavaCUP (Construct Useful Parser) is a parser generator
Advertisements

Using Ada 95 in a Compiler Course SIGAda 2001 S. Tucker Taft CTO AverCom Corp., a Titan Company October 3, 2001 Bloomington, MN.
ANALYSIS OF PROG. LANG. PROGRAM ANALYSIS Instructors: Crista Lopes Copyright © Instructors. 1.
Abstract Syntax Mooly Sagiv html:// 1.
1 Compiler Construction Intermediate Code Generation.
Compilation (Semester A, 2013/14) Lecture 6b: Context Analysis (aka Semantic Analysis) Noam Rinetzky 1 Slides credit: Mooly Sagiv and Eran Yahav.
1 JavaCUP JavaCUP (Construct Useful Parser) is a parser generator Produce a parser written in java, itself is also written in Java; There are many parser.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Compilation Encapsulation Or: Why Every Component Should Just Do Its Damn Job.
Mooly Sagiv and Roman Manevich School of Computer Science
Compiler Construction LR(0) + Visitor Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Abstract Syntax Trees Compiler Baojian Hua
Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Parsing Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Abstract Syntax Tree Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
Environments and Evaluation
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Compiler Construction Semantic Analysis II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Chapter 2 A Simple Compiler
Compiler Construction Semantic Analysis I Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Parsing II Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Parsing I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
The Interpreter Pattern. Defining the Interpreter Intent Given a language, define a representation for its grammar along with an interpreter that uses.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Semantic Analysis (Generating An AST) CS 471 September 26, 2007.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Language Translators - Lee McCluskey LANGUAGE TRANSLATORS: WEEK 21 LECTURE: Using JavaCup to create simple interpreters
Automated Parser Generation (via CUP)CUP 1. High-level structure JFlexjavac Lexer spec Lexical analyzer text tokens.java CUPjavac Parser spec.javaParser.
Grammar Variation in Compiler Design Carl Wu. Three topics Syntax Grammar vs. AST Component(?)-based grammar Aspect-oriented grammar.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
CS453 LectureIntroduction1 CS453 Compiler Construction Instructor:Wim Bohm Computer Science Building 470 TA: tba
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
Abstract Syntax Trees Compiler Baojian Hua
11/25/2015© Hal Perkins & UW CSEH-1 CSE P 501 – Compilers Implementing ASTs (in Java) Hal Perkins Winter 2008.
Winter Compiler Construction T5 – AST Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
Programming Languages
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
Compiler Construction Compiler Construction Semantic Analysis I.
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
Mooly Sagiv and Roman Manevich School of Computer Science
The Visitor Design Pattern and Java Tree Builder
Compiler Principles Fall Compiler Principles Lecture 5: Parsing part 4 Roman Manevich Ben-Gurion University.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Winter Compiler Construction Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
Abstract Syntax Tree. 2 Compiler IC Program ic x86 executable exe Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code.
Spring 2014Jim Hogg - UW - CSE P501H-1 CSE P501 – Compiler Construction Representing ASTs as Java objects Parser actions Operations on ASTs Modularity.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
MiniJava Compiler A multi-back-end JIT compiler of Java.
Lecture 9 Symbol Table and Attributed Grammars
Winter Compiler Construction
Constructing Precedence Table
Parsing & Context-Free Grammars
Implementing ASTs (in Java) Hal Perkins Autumn 2009
Fall Compiler Principles Lecture 4: Parsing part 3
Basic Program Analysis: AST
CSE401 Introduction to Compiler Construction
Winter Compiler Construction T5 – AST
Implementing ASTs (in Java) Hal Perkins Autumn 2011
Fall Compiler Principles Lecture 4: Parsing part 3
Presentation transcript:

Compiler Construction Abstract Syntax Tree Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2 Compiler IC Program ic x86 executable exe Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation IC compiler

3 Abstract Syntax Tree More useful representation of syntax tree Less clutter Actual level of detail depends on your design Basis for semantic analysis Later annotated with various information Type information Computed values

4 Parse tree vs. AST + expr 12+3 ()()

5 1 + (2) + (3) expr + (expr) + (3) + expr 12+3 expr + (3) expr ()() expr + (expr) expr expr + (2) + (3) int_const val = 1 plus e1e2 int_const val = 2 int_const val = 3 plus e1e2 expr ::= expr:e1 PLUS expr:e2 {: RESULT = new plus(e1,e2); :} | LPAREN expr:e RPAREN {: RESULT = e; :} | INT_CONST:i {: RESULT = new int_const(…, i); :} AST construction expr expr + expr + (3) expr expr + expr

6 PA2 Write parser for IC Write parser for libic.sig Check syntax Emit either “Parsed [file] successfully!” or “Syntax error in [file]: [details]” -print-ast option Prints one AST node per line

7 PA2 – step 1 Understand IC grammar in the manual Don’t touch the keyboard before understanding spec Write a debug JavaCup spec for IC grammar A spec with “debug actions” : print-out debug messages to understand what’s going on Try “debug grammar” on a number of test cases Keep a copy of “debug grammar” spec around

8 PA2 – step 2 Flesh out AST class hierarchy Don’t touch the keyboard before you understand the hierarchy Keep in mind that this is the basis for later stages Web-site contains an AST adapted with permission from Tovi Almozlino Change CUP actions to construct AST nodes

9 Abstract Syntax Trees Intermediate program representation Defines a tree Preserves program hierarchy Node types defined by class hierarchy Generated by parser Keywords and punctuation symbols not stored Provides clear interface to other compiler phases

10 Partial AST hierarchy for IC ASTNode ProgramICClassStatement LocalVariable Statement StatementBlockIfWhile... Expression LiteralNew... Call Method VirtualMethodStaticMethod... Field LibraryMethod declaration

11 AST node contents class ICClass extends ASTNode { String name; List fields; List methods;... } abstract class ASTNode { int getLine();... } used for error reporting class Program extends ASTNode { List classes;... } Java 1.5 Generics

12 non terminal Program program; non terminal ICClass class; non terminal List class_list; program ::= class_list:cl {: RESULT = new Program(getLine(), cl); :} ; class_list ::= class:c {: RESULT = new LinkedList (); RESULT.add(c); :} | class_list:cl class:c {: cl.add(c); RESULT = cl; :} ; Actions part of IC.cup Reduce class_list ::= class_list:cl class:c {: RESULT = cl.add(c); :} object stackSymbol stack class_list class cl c class_list RESULT Symbol stack object stack pop class pop class_list push class_list In reality, use value field of Symbol

13 non terminal Program program; non terminal ICClass class; non terminal List class_list; program ::= class_list:cl {: RESULT = new Program(getLine(), cl); :} ; class_list ::= class:c {: RESULT = new LinkedList (); RESULT.add(c); :} | class_list:cl class:c {: cl.add(c); RESULT = cl; :} ; Actions part of IC.cup Reduce class_list ::= class_list:cl class:c {: RESULT = cl.add(c); :} class_list RESULT Symbol stack object stack

14 AST traversal Once AST stable want to operate on tree AST traversal for type-checking AST traversal for transformation (IR) AST traversal for pretty-printing (-dump-ast) Each operation in separate pass

15 Non-Object Oriented approach prettyPrint(ASTNode node) { if (node instanceof Program) { Program prog = (Program) node; for (ICClass icc : prog.classes) { prettyPrint(icc); } } else if (node instanceof ICClass) { ICClass icc = (ICClass) node; printClass(icc); } else if (node instanceof BinaryExpression) { BinaryExpression be = (BinaryExpression) node; prettyPrint(be.lhs); System.out.println(be.operator); prettyPrint(be.rhs); }... } Messy code instanceof + down-casting error-prone Not extensible

16 Single dispatch - polymorphism class A { void op() {…} } class B extends A void op() {…} } class C extends A void op() {…} } op A B C index conceptually, one-dimensional table

17 What if we need more operations? class A { void op1() {…} void op2() {…} void op3() {…} } class B extends A void op1() void op2() void op3() {…} } class C extends A void op1() void op2() void op3() {…} } Want to separate complicated operations from data structures

18 class A { } class B extends A { } class C extends A { } class op1 extends op{ … // lots of code } class op2 extends op{ … // lots of code } class op3 extends op{ … // lots of code } × Problem: OO languages support only single-polymorphism. We seem to need double-polymorphism What if we need more operations?

19 class A { } class B extends A { } class C extends A { } class op1 extends op{ doOp(A a){ … // lots of code } } doOp(B b){ … // lots of code } } What if we need more operations? Overloading is static class op2 extends op{ doOp(A a){ … // lots of code } } doOp(B b){ … // lots of code } } class op3 extends op{ doOp(A a){ … // lots of code } } doOp(B b){ … // lots of code } }

20 Separate operations on objects of a data structure from object representation Each operation (pass) may be implemented as separate visitor Use double-dispatch to find right method for object Instance of a design patterndesign pattern Visitor Pattern

21 Visitor pattern in Java interface Visitor { visit(A a); visit(B b); visit(C c); } class A { A x; accept(Visitor v) { v.visit(this); } } class B extends A { accept(Visitor v) { v.visit(this); } } class op1 implements Visitor { visit(A a) {…} visit(B b) {…} visit(C c) {…} } × class op2 implements Visitor { visit(A a) {…} visit(B b) {…} visit(C c) {…} } class op3 implements Visitor { visit(A a) {…} visit(B b) {…} visit(C c) {…} } class C extends A { accept(Visitor v) { v.visit(this); } }

22 Double dispatch example Visitor v = new op1(); // op1/2/3 A x = new B(); // x can be A/B/C x.accept(v); class op1 implements Visitor { visit(A a) { } visit(B b) { … } } class B { accept(Visitor v) { // always calls visit(B b) v.visit(this); } } 1 st dispatch 2 nd dispatch

23 Double dispatch example Visitor v = new op1(); // op1/2/3 A x = new B(); // x can be A/B/C x.accept(v); class op1 implements Visitor { visit(A a) { } visit(B b) { … } } class B { accept(Visitor v) { // always calls visit(B b) v.visit(this); } } 1 st dispatch 2 nd dispatch op1op2op3 A B C 1 st dispatch x.accept(v) v.visit(this) 2 nd dispatch op1.visit(B b) Visitor pattern conceptually implements two-dimensional table

24 Straight Line Program example prog  stmt_list stmt_list  stmt stmt_list  stmt_list stmt stmt  var = expr; stmt  print(expr); expr  expr + expr expr  expr - expr expr  expr * expr expr  expr / expr expr  - expr expr  ( expr ) expr  number expr  readi() expr  var ASTNode StmtExpr PrintStmt AssignStmt BinaryOpExpr UnaryOpExpr NumberExpr ReadIExpr StmtList VarExpr (Code available on web site. Demonstrates scanning, parsing, AST + visitors)web site

25 Printing visitor example interface Visitor { void visit(StmtList stmts); void visit(Stmt stmt); void visit(PrintStmt stmt); void visit(AssignStmt stmt); void visit(Expr expr); void visit(ReadIExpr expr); void visit(VarExpr expr); void visit(NumberExpr expr); void visit(UnaryOpExpr expr); void visit(BinaryOpExpr expr); } public class PrettyPrinter implements Visitor { public void print(ASTNode root) { root.accept(this); } public void visit(StmtList stmts) { for (Stmt s : stmts.statements) { s.accept(this); System.out.println(); } } // x = 2*7 public void visit(AssignStmt stmt) { stmt.varExpr.accept(this); System.out.print("="); stmt.rhs.accept(this); System.out.print(";"); } // x public void visit(VarExpr expr) { System.out.print(expr.name); } // 2*7 public void visit(BinaryOpExpr expr) { expr.lhs.accept(this); System.out.print(expr.op); expr.rhs.accept(this); }... }

26 Visitor variations interface PropagatingVisitor { /** Visits a statement node with a given * context object (book-keeping) * and returns the result * of the computation on this node. */ Object visit(Stmt st, Object context); Object visit(Expr e, Object context); Object visit(BinaryOpExpr e, Object context);... } Propagate values down the AST (and back)

27 Evaluating visitor example public class SLPEvaluator implements PropagatingVisitor { public void evaluate(ASTNode root) { root.accept(this); } /** x = 2*7 */ public Object visit(AssignStmt stmt, Object env) { Expr rhs = stmt.rhs; Integer expressionValue = (Integer) rhs.accept(this, env); VarExpr var = stmt.varExpr; ((Environment)env).update(var, expressionValue); return null; } /** expressions like 2*7 and 2*y */ public Object visit(BinaryOpExpr expr, Object env) { Integer lhsValue = (Integer) expr.lhs.accept(this, env); Integer rhsValue = (Integer) expr.rhs.accept(this, env); int result; switch (expr.op) { case PLUS: result = lhsValue.intValue() + rhsValue.intValue() ;... } return new Integer(result); }... } class Environment { Integer get(VarExpr ve) {…} void update(VarExpr ve, int value) {…} }

28 NumberExpr value = 1 BinaryOpExpr + leftright NumberExpr value= 2 VarExpr name = x BinaryOpExpr + leftright class BinaryOpExpr extends Expression { Object accept(Visitor v) { return v.visit(this); } Expression lhs, rhs; } class NumberExpr extends Expression { Object accept(Visitor v) { return v.visit(this); } int val; } SLPEvaluator ev = new SLPEvaluator(); Integer result = (Integer)root.accept(ev); root visit (lhs) visit (rhs) AST traversal public class SLPEvaluator … { public Object visit(BinaryOpExpr e, Object env) { Integer lhsValue=(Integer)e.lhs.accept(this,env); Integer rhsValue=(Integer)e.rhs.accept(this,env); int result; switch (expr.op) { case PLUS: result=lhsValue.intValue()+rhsValue.intValue();... } return new Integer(result); } public Object visit(NumberExpr e,Object env) { return e.value; } public Object visit(VarExpr e, Object env) { return ((Environment)env).get(e); } 1+2+x

29 Visitor + Generics interface PropagatingVisitor { UpType visit(Stmt st, DownType d); UpType visit(Expr e, DownType d); UpType visit(VarExpr ve, DownType d);... } public class SLPEvaluator implements PropagatingVisitor { public Integer visit(VarExpr expr, Environment env) { return env.get(expr); }... }