Syntax-Directed Definitions and Attribute Evaluation 66.648 Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.

Slides:



Advertisements
Similar presentations
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
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.
Lecture # 17 Syntax Directed Definition. 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”)
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Department.
Abstract Syntax Tree (AST)
Attribute Grammars Recall the yacc program Parse a given expression
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Syntax-Directed Translation
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
Parser construction tools: YACC
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
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.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
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.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Semantic Analysis1 Checking what parsers cannot.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Chapter 3 Part II Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
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.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
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.
Lexical Analysis - Scanner- Contd Computer Science Rensselaer Polytechnic Compiler Design Lecture 3(01/21/98)
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
1 Syntax-Directed Translation We associate information with the programming language constructs by attaching attributes to grammar symbols. 2.Values.
Syntax Directed Definition and Syntax directed Translation
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
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.
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
Compiler Construction
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Syntax-Directed Translation Part I
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Mini Language Interpreter Programming Languages (CS 550)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4 Action Routines.
Syntax Analysis - Parsing
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation Part I
Presentation transcript:

Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic

Lecture Outline Syntax directed definitions Syntax directed definitions Attribute Evaluations Attribute Evaluations Administration Administration

Phases of a compiler We are in chapter 5 of the text book. Please read sections So far, we have covered lexical analyzer and parser. The output of the parser is a rightmost derivation (parse tree) Our next step is to study “Semantic Analyzer” which creates an abstract syntax tree with attributes.

Syntax-Directed Definition Syntax-directed definitions extend context-free grammars by including attributes for each grammar symbol and semantic rules that specify semantic actions to be performed during parsing. In fact one can carry out this within our parser tool. Syntax-directed definitions extend context-free grammars by including attributes for each grammar symbol and semantic rules that specify semantic actions to be performed during parsing. In fact one can carry out this within our parser tool.Attributes Consider a context-free grammar production of the form Y--> X_1 X_2…. X_n. Attributes are associated

With each instance of a grammar symbol I.e., each node of the parse tree. Example attributes could be value, type, font-size, base line etc. For each separate instance of the grammar symbol, there will be separate instance of the attribute. Attributes can either be inherited or synthesized. Attributes - Contd

An attribute of a grammar symbol is synthesized if it is computed from the attributes of its children in the parse tree corresponding to the rightmost derivation. Example: E --> E + num | num E2.val = E1.val + num.val Other examples could be base-line in latex. An attribute of a grammar symbol is inherited, if it is computed from the attributes of its parents and siblings. Attributes - Contd

Example: Example: Declarations--> Type varlist varlist --> varlist, var | var int x, y; var1. Type = int; var2.. Type = var1. Type = int The start symbol has no inherited attributes.

Attributes -Contd The synthesized attribute values for tokens/terminals are provided by the lexical analyzer. Tokens are assumed to have no inherited attributes. Statement: assignmentstmt assignmentstmt: var ‘=‘ expr ; The type of expr is inherited from the type of var. eg. In java the following is illegal: Iamaninteger = ;

Semantic Rules A grammar production Y --> X_1 … X_n may have zero or more associated semantic rules. Each semantic rules has the form, b=f(c_1,…,c_k) are attributes of the grammar symbol in the production such that: 1. b is a synthesized attribute and c_1,…,c_k are the attributes of the grammar symbols on the rhs or 2. b is an inherited attribute of one of the RHS grammar symbols and c_1,…,c_k are any other attributes in the production.

Example A syntax-directed definition in which semantic rules have no side effects is an attribute grammar. Example of Syntax-Directed Definition ProductionSemantic Rules S --> Eprint(E.val) E_1 --> E_2+TE_1.val = E_2.val+T.val E--> TE.val =T.val T_1--> T_2* numT_1.val=T_2.val*num.val T --> numT.val=num.val

Example-Contd Comment: A semantic rule with a side effect such as print(E.val) can be thought as computing a synthesized attribute for S as S.val = print(E.val); Evaluation Order of attributes: Semantic Rules define dependencies among attribute instances, which in turn define legal evaluation order (orders in which an attribute value is computed before it is used).

Evaluation of Attributes If the parsing order is consistent with some legal evaluation order, then attribute evaluation can be done on-the-fly while parsing without explicitly building a parse tree. S-attributed Definition An S-attributed definition is a syntax directed definition that contains only synthesized attributes. The synthesized attributes can be stored in extra symbols for each symbol on the parse stack.

YACC Examples

Comments and Feedback Project 2 is out. We have finished chapter 5. We will do some more examples in the next class. Please keep reading chapter 4 and understand the material. Work out as many exercises as you can. We move onto chapter 6 next.