1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Honors Compilers Semantic Analysis and Attribute Grammars Mar 5th 2002.
ISBN Chapter 3 More Syntax –BNF –Derivations –Practice.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Context-Free Grammars Lecture 7
Abstract Syntax Tree (AST)
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial)
Chapter 2 A Simple Compiler
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Attribute Grammars They extend context-free grammars to give parameters to non-terminals, have rules to combine attributes Attributes can have any type,
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.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Syntax Directed Definitions Synthesized Attributes
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Automated Parser Generation (via CUP)CUP 1. High-level structure JFlexjavac Lexer spec Lexical analyzer text tokens.java CUPjavac Parser spec.javaParser.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial) 1.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
Introduction to Compiling
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
LESSON 04.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Compiler Principles Fall Compiler Principles Lecture 5: Parsing part 4 Roman Manevich Ben-Gurion University.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Semantic analysis Jakub Yaghob
Describing Syntax and Semantics
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Constructing Precedence Table
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
SYNTAX DIRECTED DEFINITION
Presentation transcript:

1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure of the grammar e.g. intermediate non-terminals Idea –strip the unnecessary parts of the tree, simplify it. –keep track only of important information AST –Conveys the syntactic structure of the program while providing abstraction. –Can be easily annotated with semantic information (attributes) such as type, numerical value, etc. –Can be used as intermediate representation. E EE+ EE* id )(

2 AST vs. parse tree if-statement IFcondTHEN statement if-statement condstatement E EE+ EE* id )( E EE+ EE*

3 Calc example assignment ::= ID:e1 EQUAL expr:e2 {: RESULT = new Assignment(e1, e2); :} ; expr ::= expr:e1 PLUS:e expr:e2 {: RESULT = new Expr(e1, e2, e); :} |expr:e1 MULTI:e expr:e2 {: RESULT = new Expr(e1, e2, e); :} | LPAREN expr:e RPAREN {: RESULT = e; :} | NUMBER:e {: RESULT= new Expr(e); :} | ID:e {: RESULT = new Expr(e); :} E EE+ EE* id

4 Interpreter and translator example What is abstract is dependent on the application expr ::= expr:e1 PLUS expr:e2 {: RESULT=e1+"+"+e2; :} | expr:e1 MINUS expr:e2 {: RESULT=e1+"-"+e2; :} | expr:e1 TIMES expr:e2 {: RESULT=e1+"*"+e2; :} | expr:e1 DIVIDE expr:e2 {: RESULT=e1+"/"+e2; :} | LPAREN expr:e RPAREN {: RESULT="("+e+")"; :} | NUMBER:e {: RESULT=e; :} | ID:e {: RESULT=e; :} | fctCall:e {: RESULT=e; :} expr ::= expr:e1 PLUS expr:e2 {: RESULT = new Integer(e1.intValue()+ e2.intValue()); :} | expr:e1 MINUS expr:e2 {: RESULT = new Integer(e1.intValue()- e2.intValue()); :} | expr:e1 TIMES expr:e2 {: RESULT = new Integer(e1.intValue()* e2.intValue()); :} | expr:e1 DIVIDE expr:e2 {: RESULT = new Integer(e1.intValue()/ e2.intValue()); :} | LPAREN expr:e RPAREN {: RESULT = e; :} | NUMBER:e {: RESULT= e; :} ;

5 Attribute grammar Formal framework based on grammar and parse tree “attribute” the tree –Can add attributes (fields) to each node –augment grammar with rules defining attribute values –high-level specification, independent of evaluation scheme –Note: translation scheme has evaluation order –both inherited and synthesized attributes Attribute grammars are very general. Can be used for –infix to postfix translation of arithmetic expressions –type checking (context-sensitive analysis) –construction of intermediate representation (AST) –desk calculator (interpreter) –code generation (compiler) Another name for syntax directed translation

6 Dependencies among attributes values are computed from constants & other attributes synthesized attribute - value computed from children –attribute of left-hand side is computed from attributes in the right-hand side – bottom-up propagation inherited attribute - value computed from siblings & parent –attribute of symbol on right-hand is computed from attributes of left-hand side, or from attributes of other symbols on right-hand side –top-down propagation of information E EE+ EE* N N N)( expr ::= expr:e1 PLUS expr:e2 {: RESULT = new Integer(e1.intValue()+ e2.intValue()); :} | expr:e1 MINUS expr:e2 {: RESULT = new Integer(e1.intValue()- e2.intValue()); :} | expr:e1 TIMES expr:e2 {: RESULT = new Integer(e1.intValue()* e2.intValue()); :} | expr:e1 DIVIDE expr:e2 {: RESULT = new Integer(e1.intValue()/ e2.intValue()); :} | LPAREN expr:e RPAREN {: RESULT = e; :} | NUMBER:e {: RESULT= e; :} ;

7 Attributes For terminal: define some computable properties –e.g. the value of NUMBER For non-terminal (production): give computation rules for the properties of all symbols –e.g. the value of a sum is the sum of the values of the operands The rule is local: only refers to symbols in the same production The evaluation of the attributes can require an arbitrary number of traversals of the AST: arbitrary context dependence (.e.g. the value of a declared constant is found in the constant declaration) Attribute definitions may be cyclic; checking whether an attribute grammar has cycles is decidable but potentially expensive In practice inherited attributes are handled by means of global data structures (symbol table) expr ::= expr:e1 PLUS expr:e2 {: RESULT = new Integer(e1.intValue()+ e2.intValue()); :} | expr:e1 MINUS expr:e2 {: RESULT = new Integer(e1.intValue()- e2.intValue()); :} | expr:e1 TIMES expr:e2 {: RESULT = new Integer(e1.intValue()* e2.intValue()); :} | expr:e1 DIVIDE expr:e2 {: RESULT = new Integer(e1.intValue()/ e2.intValue()); :} | LPAREN expr:e RPAREN {: RESULT = e; :} | NUMBER:e {: RESULT= e; :} ;

8 Some examples of attributes For expressions: type For overloaded calls: candidate interpretations For identifiers: entity (defining_occurrence) For definitions: scope For data/function members: visibility (public, protected, private) …

9 Syntactic and semantic analysis Syntactic analysis generates a parse tree –Syntax analysis can not capture all the errors –Some rules are beyond context free grammar e.g., a variable declaration needs to occur before the use of the variable Semantic analysis enforce context-dependent language rules that are not reflected in the BNF Semantic analysis adds semantic information to the parse tree/AST – e.g. determine types of all expressions General framework: compute attributes

10 Examples of semantic rules Variables –must be defined before being used –should not be defined multiple times Types –In an assignment stmt, the variable and the expression must have the same type –The test exprssion of an if statement must have boolean type Classes –can be defined only once –Inheritance relationship –Methods only defined once Reserved words can not be used as variable or function or class names Scope of variables etc., Variable initialization … Semantic analysis requirements are language dependent

11 Type checking One major category semantic analysis Steps –Type synthesis – assigning a type to each expression in the language –Type checking – making sure that these types are used in contexts where they are legal, catching type-related errors What is a type –Differs from language to language –A set of values and a set of operations on the values –A class is also a type Type checking –Ensures that operations are used with the correct types Why type checking –int x, y; y= x*x is fine –String x,y; y= x*x does not make sense –Should catch such type error –When to catch type error?