Abstract Syntax Cheng-Chia Chen. 2 Concrete v.s. Abstract Syntax Parsing results can generally be represented in two forms: –concrete syntax tree (CST.

Slides:



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

Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Abstract Syntax Mooly Sagiv html:// 1.
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.
Modern Compiler Implementation in Java Semantic Analysis Chapter 5 CMSC 431 Spring, 2003.
Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
1 Semantic Analysis (Symbol Table and Type Checking) Chapter 5.
1 Semantic Analysis (Symbol Table and Type Checking) Chapter 5.
Abstract Syntax Trees Compiler Baojian Hua
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.
Semantic Analysis (Symbol Table and Type Checking)
Professor Yihjia Tsai Tamkang University
28-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
Winter 2003/4Pls – syntax – Catriel Beeri1 SYNTAX Syntax: form, structure The syntax of a pl: The set of its well-formed programs The rules that define.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 3 Lexical and Syntactic Analysis Syntactic.
Dr. Philip Cannata 1 Programming Languages. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
Attribute Grammars They extend context-free grammars to give parameters to non-terminals, have rules to combine attributes Attributes can have any type,
Grammars and Parsing. Sentence  Noun Verb Noun Noun  boys Noun  girls Noun  dogs Verb  like Verb  see Grammars Grammar: set of rules for generating.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Syntax Specification and BNF © Allan C. Milne Abertay University v
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.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
PART I: overview material
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
MIT Intermediate Formats Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Compiler Construction Compiler Construction Semantic Analysis I.
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.
Dr. Philip Cannata 1 Lexical and Syntactic Analysis Chomsky Grammar Hierarchy Lexical Analysis – Tokenizing Syntactic Analysis – Parsing Hmm Concrete Syntax.
BUILD ON THE POLYGLOT COMPILER FRAMEWORK MIHAL BRUMBULLI 7th Workshop “SEERE” Montenegro-Risan 9-14 September 2007 SimJ Programming Language.
A Rule of While Language Syntax // Where things work very nicely for recursive descent! statmt ::= println ( stringConst, ident ) | ident = expr | if (
COMP Parsing 3 of 4 Lectures 23. Using the Scanner Break input into tokens Use Scanner with delimiter: public void parse(String input ) { Scanner.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Prof. Fateman CS 164 Lecture 111 Syntax  Simple Semantics Lecture 11.
CPS 506 Comparative Programming Languages Syntax Specification.
Abstract Syntax Trees Compiler Baojian Hua
22-Nov-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
11/25/2015© Hal Perkins & UW CSEH-1 CSE P 501 – Compilers Implementing ASTs (in Java) Hal Perkins Winter 2008.
CSE 5317/4305 L6: Semantic Analysis1 Semantic Analysis Leonidas Fegaras.
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
The Visitor Design Pattern and Java Tree Builder
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Bernd Fischer COMP2010: Compiler Engineering Abstract Syntax Trees.
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.
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Chapter 3 – Describing Syntax
COMP261 Lecture 18 Parsing 3 of 4.
Programming Languages 2nd edition Tucker and Noonan
Parsing & Context-Free Grammars
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Syntax (1).
CS 614: Theory and Construction of Compilers
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Implementing ASTs (in Java) Hal Perkins Autumn 2009
Syntax versus Semantics
Syntax Analysis Sections :.
Compiler Design 4. Language Grammars
C H A P T E R T W O Syntax.
Syntax-Directed Translation
Implementing ASTs (in Java) Hal Perkins Autumn 2011
Interpreter Pattern.
Presentation transcript:

Abstract Syntax Cheng-Chia Chen

2 Concrete v.s. Abstract Syntax Parsing results can generally be represented in two forms: –concrete syntax tree (CST or Parse Tree) –abstract syntax tree (AST or syntax tree) Concrete syntax trees (parse tree) is a record of the rules (and tokens) used to match some input text. Abstract syntax tree records the structure of the input and is insensitive to the grammar that produced it. AST is superior to CST since –CST reflects the structure of a sentence in a grammar AST reflects the structure of a sentence in the language. But a language can have a lot of grammars. –AST is generally more compact. –AST is usually more convenient for later analysis and processing.

Cheng-Chia Chen3 Example Given the grammar : Expr  Expr + Term Term  Term * Factor Factor  Number | (E) and the input ‘2 + 5 ‘ : We should have the following CST and AST:

Cheng-Chia Chen4 PlusExpr NumExpr :5 NumExpr :2 e1 e2 BinExpr op: plus NumExpr :5 NumExpr :2 e1 e2 Expr Factor Plus :+ Term Number:2 Term Factor Number :5 Expr The parse tree for ‘2+5’ Possible ASTs for ‘2+5’

Cheng-Chia Chen5 Abstract Syntax v.s. Concrete Syntax of Expressions Abstract Syntax E  E + E E  E – E E  E * E E  E / E E  id E  num Notes: 1.reflect the meaning of expressions; resulting AST more convenient for later processing 2.ambiguous; impractical to parsing Concrete Syntax (LALR(1)) E  E + F E  E – F F  F * T F  T / T T  id T  num T  (E) Notes: 1.quick parse possible 2.resulting CST is highly redundant Conclusion: generate AST (instead of CST) during concrete syntax parsing

Cheng-Chia Chen6 Building Syntax Tree for Expressions (using JavaCC) First devise the Concrete Syntax (LL(K)) in EBNF Form. Start  Exp Exp  Term ( “+” Term | “-” Tem)* Term  Factor (“*” Factor | “/” Factor)* Factor  | | “(“ Exp “)”

Cheng-Chia Chen7 Design the AST classes : Class Hierarchy for expressions Exp PlusExp MinusExp TimesExp DivideExp Identifier IntergerLiteral or simply a BinaryExp

Cheng-Chia Chen8 AST classes for expressions pubic abstract class Exp { pubic abstract int eval() } public class PlusExp extends Exp { private Exp e1,e2 ; public PlusExp(Exp a1, Exp a2) { e1=a1;e2=a2;} public int eval() { return e1.eval() + e2.eval(); } } …/* similar to PlusExp for class MinusExp, timesExp and DivideExp. Omitted here */ public class Identifier extends Exp { pubic String f0; public Identifier(String a0) { f0 = a0 ; } public int eval() { return symbolTable.lookup(f0); }}

Cheng-Chia Chen9 AST classes public class IntergerLiteral extends Exp { pubic String f0; public Identifier(String a0) { f0 = a0 ; } public int eval() { return Integer.parseInt(f0); } }

Cheng-Chia Chen10 Add actions to build syntax tree : Using the available AST classes Exp Start() : {Exp e;} { e = Exp() { return e;} } Exp Exp() : {Exp e1,e2;} { e1 = Term() ( “+” e2 = Term() { e1 = new PlusExp(e1,e2) ;} | “-” e2 = Term() { e1 = new MinusExp(e1,e2) ;} )* { return e1; } }

Cheng-Chia Chen11 Building AST Exp Term() : {Exp e1,e2;} { e1 = Factor() ( “*” e2 = Factor() { e1 = new TimesExp(e1,e2) ;} | “/” e2 = Factor() { e1 = new DivideExp(e1,e2) ;} )* { return e1; } } Exp Factor() : {Token t; Exp e;} { ( t = { e = new Identifier(t.image); } | t= { e = new IntegerLiteral(t.image); } | “(“ e = Exp() “)” ) { return e; } }

Cheng-Chia Chen12 ExpParser.jj options { STATIC = false; … } PARSER_BEGIN(ExpParser) public class ExpParser { … public static void main( String[] args) { ExpParser p = new ExpParser(System.in); Exp ast = p.start(); System.out.prinln( “result = “ + ast.eval() ); } Token : { } SKIP : { … } …

Cheng-Chia Chen13 Tools for automated generation of AST classes definitions from abstract syntax definition –sableCC version 3.X –JTB Disadvantage –The syntax tree and relevant classes generated may be less abstract than one could desire.

Cheng-Chia Chen14 ASTs for MiniJava at package syntaxtree;

Cheng-Chia Chen15 Abstract Syntax for MiniJava (I) Package syntaxtree; Program(MainClass m, ClassDecList c1) MainClass(Identifier i1, Identifier i2, Statement s) abstract class ClassDecl ClassDeclSimple(Identifier i, VarDeclList vl, methodDeclList m1) ClassDeclExtends(Identifier i, Identifier j, VarDecList vl, MethodDeclList ml) VarDecl(Type t, Identifier i) MethodDecl(Type t, Identifier I, FormalList fl, VariableDeclList vl, StatementList sl, Exp e) Formal(Type t, Identifier i)

Cheng-Chia Chen16 Abstract Syntax for MiniJava (II) abstract class type IntArrayType() BooleanType() IntegerType() IndentifierType(String s) abstract class Statement Block(StatementList sl) If(Exp e, Statement s1, Statement s2) While(Exp e, Statement s) Print(Exp e) Assign(Identifier i, Exp e) ArrayAssign(Identifier i, Exp e1, Exp e2)

Cheng-Chia Chen17 Abstract Syntax for MiniJava (III) abstract class Exp And(Exp e1, Exp e2) LessThan(Exp e1, Exp e2) Plus(Exp e1, Exp e2) Minus(Exp e1, Exp e2) Times(Exp e1, Exp e2) Not(Exp e) ArrayLookup(Exp e1, Exp e2) ArrayLength(Exp e) Call(Exp e, Identifier i, ExpList el) IntegerLiteral(int i) True() False() IdentifierExp(String s) This() NewArray(Exp e) NewObject(Identifier i) Identifier(Sting s) --list classes ClassDecList() ExpList() FormalList() MethodDeclList() StatementLIst() VarDeclList()

Cheng-Chia Chen18 Syntax Tree Nodes - Details package syntaxtree; import visitor.Visitor; import visitor.TypeVisitor; public class Program { public MainClass m; public ClassDeclList cl; public Program(MainClass am, ClassDeclList acl) { m=am; cl=acl; } public void accept(Visitor v) { v.visit(this); } public Type accept(TypeVisitor v) { return v.visit(this); }

Cheng-Chia Chen19 ClassDecl.java package syntaxtree; import visitor.Visitor; import visitor.TypeVisitor; public abstract class ClassDecl { public abstract void accept(Visitor v); public abstract Type accept(TypeVisitor v); }

Cheng-Chia Chen20 ClassDeclExtends.java package syntaxtree; import visitor.Visitor; import visitor.TypeVisitor; public class ClassDeclExtends extends ClassDecl { public Identifier i; public Identifier j; public VarDeclList vl; public MethodDeclList ml; public ClassDeclExtends(Identifier ai, Identifier aj, VarDeclList avl, MethodDeclList aml) { i=ai; j=aj; vl=avl; ml=aml; } public void accept(Visitor v) { v.visit(this); } public Type accept(TypeVisitor v) { return v.visit(this); }

Cheng-Chia Chen21 StatementList.java package syntaxtree; import java.util.Vector; public class StatementList { private Vector list; public StatementList() { list = new Vector(); } public void addElement(Statement n) { list.addElement(n); } public Statement elementAt(int i) { return (Statement)list.elementAt(i); } public int size() { return list.size(); }

Cheng-Chia Chen22 Package visitor/Visitor.java package visitor; import syntaxtree.*; public interface Visitor { public void visit(Program n); public void visit(MainClass n); public void visit(ClassDeclSimple n); public void visit(ClassDeclExtends n); public void visit(VarDecl n); public void visit(MethodDecl n); public void visit(Formal n); public void visit(IntArrayType n); public void visit(BooleanType n); public void visit(IntegerType n); public void visit(IdentifierType n); public void visit(Block n); public void visit(If n); public void visit(While n); public void visit(Print n); public void visit(Assign n); public void visit(ArrayAssign n); public void visit(And n); public void visit(LessThan n); public void visit(Plus n); public void visit(Minus n); public void visit(Times n); public void visit(ArrayLookup n); public void visit(ArrayLength n); public void visit(Call n); public void visit(IntegerLiteral n); public void visit(True n); public void visit(False n); public void visit(IdentifierExp n); public void visit(This n); public void visit(NewArray n); public void visit(NewObject n); public void visit(Not n); public void visit(Identifier n); }

Cheng-Chia Chen23 AST for statement : X = y.m(1,4+5) Statement s Assign (Identifier,Exp) Identifier(“x”)Call(Exp,Identifier,ExpList) IdentifierExp(“y”)Identifier(“m”) IntegerLiteral(1) ExpList:[, ] Plus(Exp,Exp) IntegerLiteral(4)(IntegerLiteral(5)

Cheng-Chia Chen24 MiniJava : Grammar(I) Program -> MainClass ClassDecl * Program(MainClass, ClassDeclList) Program Goal() : { MainClass m; ClassDeclList cl = new ClassDeclList(); ClassDecl c; } { m = MainClass() ( c = ClassDecl() {cl.addElement(c);})* {return new Program(m,cl) }

Cheng-Chia Chen25 MiniJava : Grammar(II) MainClass -> class id { public static void main ( String [] id ) { Statement } } MainClass( Identifier, Identifier, Statement) ClassDecl -> class id { VarDecl * MethodDecl * } -> class id extends id { VarDecl* MethodDecl * } ClassDeclSimple(…), ClassDecExtends(…) VarDecl -> Type id ; VarDecl(Type, Identifier) MethodDecl -> public Type id ( FormalList ) { VarDecl * Statement* return Exp ; } MethodDecl(Type,Identifier,FormalList,VarDeclList StatementList, Exp)

Cheng-Chia Chen26 MiniJava : Grammar(III) FormalList -> Formal ( “,” Formal)* Formal -> Type id Formal(Type, Identifier ) Type -> int [] IntArrayType() -> boolean -> int -> id IdentifierType()

Cheng-Chia Chen27 MiniJava : Grammar(IV) Statement -> { Statement * } -> if ( Exp ) Statement else Statement -> while ( Exp ) Statement -> System.out.println ( Exp ) ; -> id = Exp ; -> id [ Exp ] = Exp ; ExpList -> Exp (“,” Exp ) *

Cheng-Chia Chen28 MiniJava : Grammar(V) Exp -> Exp op Exp -> Exp [ Exp ] -> Exp. length -> Exp. Id ( ExpList ) -> INTEGER_LITERAL -> true -> false -> id -> this -> new int [ Exp ] -> new id ( ) -> ! Exp -> ( Exp )