Bernd Fischer COMP2010: Compiler Engineering Attribute Grammars and Attribute Evaluation.

Slides:



Advertisements
Similar presentations
SYNTAX DIRECTED TRANSLATION 11CS Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal.
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
Compilation (Semester A, 2013/14) Lecture 6b: Context Analysis (aka Semantic Analysis) Noam Rinetzky 1 Slides credit: Mooly Sagiv and Eran Yahav.
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
1 Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what.
Honors Compilers Semantic Analysis and Attribute Grammars Mar 5th 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
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.
Abstract Syntax Tree (AST)
Attribute Grammars Recall the yacc program Parse a given expression
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.
Compiler construction 2002
Topic 5 -Semantic Analysis Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Compiler Construction Sohail Aslam Lecture Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Syntax Directed Definitions Synthesized Attributes
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
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 Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Topic: Syntax Directed Translations
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
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.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Chapter 3 Part II Describing Syntax and Semantics.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
Overview of Previous Lesson(s) Over View  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values of.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
國立台灣大學 資訊工程學系 薛智文 98 Spring Syntax-Directed Translation (textbook ch#5.1–5.6, 4.8, 4.9 )
Syntax Directed Definition and Syntax directed Translation
UNIT – 5 SYNTAX-DIRECTED TRANSLATION
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
Compiler Syntactic Analysis r Two general classes of parsing techniques m Bottom-up (Operator-Precedence parsing) Begin with the terminal nodes.
SDTs used to implement SDDs A non-cyclic SDD (having definitions of attributes) can always be implemented by a SDT (having actions that assign values to.
Compiler Principle and Technology Prof. Dongming LU Apr. 15th, 2015.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
Lecture 9 Symbol Table and Attributed Grammars
A Simple Syntax-Directed Translator
Constructing Precedence Table
Chapter 5 Syntax Directed Translation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Syntax-Directed Translation Part I
Syntax-Directed Translation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
SYNTAX DIRECTED DEFINITION
COP4020 Programming Languages
COP4020 Programming Languages
Lecture 16 Boolean Expressions and Control Flow
Chapter 5 Syntax Directed Translation
Presentation transcript:

Bernd Fischer COMP2010: Compiler Engineering Attribute Grammars and Attribute Evaluation

Attributes store extra information in AST nodes. type:int val:3 code: iconst_3... type:int val:4 code: iconst_4... type:int val:4 env: offs:1 code: a_load1... type:double val:1.0 code: dconst_1... '*' '+' IntLit '+''+' RealLitId... xintconst4... type:double val:35.0 env: code: swap i2d dmul... Typical attributes: type value environment code type:int val:7 code: iadd... IntLit type:double...

Attribute Evaluation Conceptually, attributes are functions on entire ASTs: attr: AST × Node → AVal ⇒ need restrictions for efficient evaluation In practice, attributes are evaluated by scanner: attached to tokens (e.g., source locations) –too restricted parser: integrated with AST construction –common attribute evaluator: extra AST traversals –common in later phases (e.g., code generation)

Attribute Evaluation Conceptually, attributes are functions on entire ASTs: attr: AST × Node → AVal ⇒ need restrictions for efficient evaluation An attribute at a node can only be computed from attributes at parent, siblings, and children.

Synthesized attributes are computed from children. type:int val:3... type:int val:4... type:int val:4... type:double val: '*' '+' IntLit '+''+' Id type:int val:7... IntLit type:double val: RealLit type:double val: ⇒ evaluation functions still depend on current node

Inherited attributes are computed from parent and siblings. '*' '+' IntLit '+''+' IdIntLitRealLit Id... xintconst4 env: offs:2 env: offs:1 env:... xintconst4 ydoubleconst- ConstDecl

Attribute Evaluation Order inherited synthesized... computed available Careful: cyclic dependencies A → BA.s = B.i B.i = A.s + 1 A A.s B B.i cycle detection in grammar is NP complete!

Attribute Evaluation Rules Attribute evaluation can be added to parse rules decl returns [String type, List vars] : t = type ids+=ID (',' ids+=ID)* {$type = $t.text; $vars = $ids} ; synthesized attributes evaluation functions

Attribute Evaluation Rules (II) Difference between synthesized and inherited attributes: Synthesized attribute for non-terminal A at a node N –defined by evaluation rule associated with production at N –production must have A as its head –defined in terms of N’s children and N itself Inherited attribute for non-terminal B at a node N –defined by evaluation rule associated with production at parent of N –production must have B in its body –defined in terms of N’s parent and siblings, and N itself

Attributed Grammars We can limit the use synthesized and inherited attributes to guarantee that the attribute evaluation is cycle-free: A grammar is S-attributed if every attribute is synthesized. A grammar is L-attributed if every attribute is 1.synthesized, or 2.inherited, with the restriction that the evaluation rule uses only  attributes inherited from the parent, or  attributes (synthesized or inherited) from left siblings

Attribute Evaluation Order vs. Parsing S-attributed grammars are well-suited for LR-parsing (bottom-up): –when applying a reduce operation, all attribute values are available to calculate the attributes of the parent. L-attributed grammars are well-suited for LL-parsing (top-down, left-to-right): –when calling a parse rule function, all attribute values are available to calculate the attributes of the non- terminal. L-attributed grammars can be used for LR-parsing –inheritance from above is tricky

Summary (Or: What to know for the exam) synthesized vs. inherited attributes attributed grammars attribute evaluation and parsing