Spring 2014Jim Hogg - UW - CSE P501H-1 CSE P501 – Compiler Construction Representing ASTs as Java objects Parser actions Operations on ASTs Modularity.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Abstract Syntax Mooly Sagiv html:// 1.
1 Compiler Construction Intermediate Code Generation.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Compiler Construction
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
10/22/2002© 2002 Hal Perkins & UW CSEG-1 CSE 582 – Compilers Intermediate Representations Hal Perkins Autumn 2002.
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
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Context-Free Grammars Lecture 7
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
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.
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 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
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.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
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.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
CPS 506 Comparative Programming Languages Syntax Specification.
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.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Chapter 1 Introduction Major Data Structures in Compiler
12/18/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Winter 2008.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
MiniJava Compiler A multi-back-end JIT compiler of Java.
CSE P501 – Compilers Parsing Context Free Grammars (CFG) Ambiguous Grammars Next Spring 2014Jim Hogg - UW - CSE - P501C-1.
Lecture 9 Symbol Table and Attributed Grammars
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compiler Construction (CS-636)
Semantic Analysis with Emphasis on Name Analysis
Implementing ASTs (in Java) Hal Perkins Autumn 2009
CS 536 / Fall 2017 Introduction to programming languages and compilers
Parsing & Context-Free Grammars Hal Perkins Autumn 2011
Lecture 2: General Structure of a Compiler
Intermediate Representations Hal Perkins Autumn 2011
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Metacircular Evaluator
CSE401 Introduction to Compiler Construction
Implementing ASTs (in Java) Hal Perkins Autumn 2011
Intermediate Representations Hal Perkins Autumn 2005
Focus of the Course Object-Oriented Software Development
Compiler Construction
Compiler Construction
Presentation transcript:

Spring 2014Jim Hogg - UW - CSE P501H-1 CSE P501 – Compiler Construction Representing ASTs as Java objects Parser actions Operations on ASTs Modularity and encapsulation Visitor pattern MiniJava Project: more info

Spring 2014Jim Hogg - UW - CSE - P501H-2 Source Target Front End Back End Scan chars tokens AST IR AST = Abstract Syntax Tree IR = Intermediate Representation ‘Middle End’ Optimize Select Instructions Parse Semantics Allocate Registers Emit Machine Code IR Convert AST

Spring 2014Jim Hogg - UW - CSE P501H-3 Scan chars tokens AST Parse Semantics Machine Code MiniJava Project Compiler No IR, Optimization or sophisticated instruction selection, or register allocation Assembler Text Assembler

Spring 2014Jim Hogg - UW - CSE - P501H-4 Recap: AST AST captures essential structure of a program, without the extra concrete grammar details needed to guide the parser So, no punctuation of grammar symbols used to define precedence & associativity (remember: E, T, F in Expression Grammar) Each node is a small Java object While while (n > 0) { n = n – 1; } > Id:nIlit:0 = Id:n - Ilit:1

Spring 2014Jim Hogg - UW - CSE - P501H-5 Representation in Java Basic idea: use small classes as records to represent AST nodes Simple data structures, not too smart “pod” = "plain old data" Take advantage of type system Use inheritance to treat related nodes polymorphically Following slides sketch the ideas; no need to use literally

AST Node Hierarchy in MiniJava Spring 2014Jim Hogg - UW - CSE - P501 H-6 Exp And Stm ArrAsg ArrLength ArrLookup Asg Block Type BoolType Block Call ClassDec ClassDecExt ClassDecList ClassDecSim ExpList False Formal FormalList Id IdExp IdType If IntArrType IntLit IntType Less MainClassMethDecMethDecList Minus NewArr NewObj Not Plus Print Program StmList This Times True VarDec While ??? Abstract Base Class Concrete Sub Class

Spring 2014Jim Hogg - UW - CSE - P501H-7 AST Nodes - Sketch // Base class of AST node hierarchy public abstract class Ast { // constructors (for convenience) // operations // string representation public abstract String toString(); // visitor methods, etc }

Spring 2014Jim Hogg - UW - CSE - P501H-8 Some Statement Nodes // Base class for all statements public abstract class Stm extends Ast { … } public class While extends Stm { public Exp exp; public Stm stm; public While(Exp exp, Stm stm) { this.exp = exp; this.stm = stm; } public String toString() { return "While(" + exp + ")" + this.stm; } Note: most times we’ll want to print the tree in a separate traversal, so no point defining a toString method

Spring 2014Jim Hogg - UW - CSE - P501H-9 More Statement Nodes public class If extends Stm { public Exp exp; public Stm thenStm, elseStm; public If(Exp exp, Stm thenStm, Stm elseStm) { this.exp = exp; this.thenStm = thenStm; this.elseStm = elseStm; } public If(Exp exp, Stm thenStm) { this(exp, thenStm, null); } } Coding style: indentation & braces style chosen as compact for slides. Feel free to use any other, consistent style in your code

Spring 2014Jim Hogg - UW - CSE - P501H-10 Expressions public abstract class Exp extends AST { … } public class Plus extends Exp { public Exp e1, e2;// operands public Plus(Exp e1, Exp e2) { this.e1 = e1; this.e2 = e2; }

Spring 2014Jim Hogg - UW - CSE - P501H-11 More Expressions public class Call extends Exp { public Exp id;// method name public List args; // list of arg expressions public Call(Exp id, List args) { this.id = id; this.args = args; } }

Etc These examples are meant to illustrate the ideas; invent your own, if you prefer Eg, maybe better to have a specific AST node for “argument list” that encapsulates the List of arguments You’ll also need nodes for class and method declarations, parameter lists, and so forth But … strongly suggest using the AST classes in the starter code, taken from the MiniJava website Modify if you need to, and are confident you know what you’re doing Spring 2014Jim Hogg - UW - CSE - P501H-12

Spring 2014Jim Hogg - UW - CSE - P501H-13 Position Information in Nodes To produce useful error messages, record the source location ("source co-ordinates") in that node Most scanner/parser generators have a hook for this, usually storing source position information in tokens Included in the MiniJava starter code – good idea to take advantage of it in your code

AST Generation: Idea: each time the parser recognizes a complete production, it creates an AST node That AST node links to the subtrees that are its components in the grammar production When we finish parsing, the result of the goal symbol is the complete AST for the program Spring 2014Jim Hogg - UW - CSE - P501H-14 nonterminal Exp Exp; Exp ::= Exp:e1 PLUS Exp:e2 {: RESULT = new Plus(e1, e2, e1left); :} Note: aliases and line # in CUP: e1left = line number of e1 Parser Actions

AST Generation in YACC/CUP A result type can be specified for each item in the grammar specification Each parser rule can be annotated with a semantic action - a piece of Java code that returns a value of the result type The semantic action is executed when that rule is reduced Spring 2014Jim Hogg - UW - CSE - P501H-15

Spring 2014Jim Hogg - UW - CSE - P501H-16 YACC/CUP Parser Specification nonterminal Stm stm, while; nonterminal Exp exp;... stm ::=... | WHILE LPAREN exp:e RPAREN stm:s {: RESULT = new While(e,s); :} ; See the starter code for version with line numbers NonTerminal Return type

Spring 2014Jim Hogg - UW - CSE - P501H-17 Once we have the AST, we may want to: Print a readable dump of the tree ("pretty print") Useful to check & debug your code Do static semantic analysis: Type checking/inference Check variables are declared, and initialized, before use Etc, etc Perform optimizing transformations on tree Generate IR for further processing Generate code (eg: assembler text; machine binary) Operations on ASTs

Spring 2014Jim Hogg - UW - CSE - P501H-18 Where do the Operations Go? Pure “object-oriented” style would advocate: Really, really smart AST nodes Each node knows how to perform every operation on itself: public class While extends Stm { public While(...); public typeCheck(...); public strengthReduce(...); public genCode(...); public prettyPrint(...);... }

Spring 2014Jim Hogg - UW - CSE - P501H-19 Critique This is nicely encapsulated – all details about a While node are hidden in that class But it shows poor modularity: How to add a new optimization? Modify every node class Worse - details of any operation (eg: optimization, type- checking) are scattered across all node classes

Spring 2014Jim Hogg - UW - CSE - P501H-20 Modularity Issues Smart nodes make sense if set of operations is relatively fixed, but we expect to to add new kinds of nodes Example: graphics system Operations: draw, move, iconify, highlight Objects: textbox, scrollbar, canvas, menu, dialog box, plus new objects defined as the system evolves

Spring 2014Jim Hogg - UW - CSE - P501H-21 Modularity in a Compiler Abstract syntax does not change often over time ie, language syntax does not change often So, kinds of nodes are relatively fixed But, as a compiler evolves, it may be common to modify or add operations on the AST nodes Want to modularize each operation (type check, optimize, codegen) so its components are together, rather than spread across many class definitions (eg: 44 for MiniJava) Want to avoid having to change node classes when we modify or add an operation on the tree Especially true if compiler is "extensible" - supports plugins from 3rd parties that provide: usage, model conformance, compat warnings, API availability warnings, etc

Spring 2014Jim Hogg - UW - CSE - P501H-22 Two Views of Modularity ClasstypeCheckoptimizegenX86CodeFfattenprint? IdXXXXX? ExpXXXXX? WhileXXXXX? IfXXXXX? BinopXXXXX? Classdrawmoveiconifyhighlighttransmogrify CircleXXXXX TextXXXXX CanvasXXXXX ScrollXXXXX DialogXXXXX ?????? Compiler ASTsGraphics Package

Spring 2014Jim Hogg - UW - CSE - P501H-23 Idea: package each operation (optimization, print, codegen, etc) into its own class Create one instance of this visitor class Sometimes called a “function object” Contains all of the methods for that particular operation, one for each kind of AST node Include a generic “accept visitor” method in every node class To perform the operation, pass the “visitor object” around the AST during a traversal Visitor Pattern

Avoiding instanceof Want to avoid huge if-elseif nests in the visitor to discover the node types Spring 2014Jim Hogg - UW - CSE - P501H-24 void prettyPrint(Ast p) { if (p instanceof While) {... } else if (p instanceof If) {... } else if (p instanceof Plus) {... }... } Inelegant Inefficient - need to lookup object's type for each test OO provides a better way

Spring 2014Jim Hogg - UW - CSE - P501H-25 Visitor Interface public interface Visitor { public void visit(While n); public void visit(If n); public void visit(Plus n);... } Result type can be whatever is convenient; void is common parameter 'n' stands for "node"

Visitor Pattern: AST node In every AST node class, define 'accept' method. Eg: Spring 2014Jim Hogg - UW - CSE - P501H-26 public class If extends Stm { public Exp cond; public Stm ifStm, elseStm; public If(Exp c, Stm s1, Stm s2) { cond = c; ifStmt = s1; elseStm = s2; } public void accept(Visitor v) { v.visit(this); } }

Visitor Pattern: PrettyPrinter Spring 2014Jim Hogg - UW - CSE - P501H-27 In any class that walks the AST, such as PrettyPrint: public class PrettyPrinter implements Visitor { public void visit(If n) { this.print("If"); n.cond.accept(this); n.ifStm.accept(this); n.elseStm.accept(this); } public void visit(While n) {... }... }

Spring 2014Jim Hogg - UW - CSE - P501H-28 Encapsulation A visitor object often needs to access state in the AST nodes So, may need to expose more node state (ie, "public" fields) than we would normally want Overall, however, a good tradeoff – better modularity plus, the nodes are pods so not much to hide

References Visitor Pattern Design Patterns: Elements of Reusable Object-Oriented Software Gamma, Helm, Johnson, and Vlissides ("GOF") Addison-Wesley, 1995 Object-Oriented Design & Patterns Horstmann, A.W, 2e, 2006 Spring 2014Jim Hogg - UW - CSE - P501H-29 Modern Compiler Implementation in Java; Appel; 2e; 2013 Website: MiniJava AST and visitors:

Spring 2014Jim Hogg - UW - CSE - P501H-30 Next Static Analysis Type-checking How to represent Types Context-Sensitive Rules declare-before-use; type-matching; etc Symbol Tables