CSE 420 Lecture 10-11 1. Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

SYNTAX DIRECTED TRANSLATION 11CS Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
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.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
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.
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 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Context-Free Grammars Lecture 7
Abstract Syntax Tree (AST)
CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Chapter 2 A Simple Compiler
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Copyright © 2005 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
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.
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.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
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.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
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.
Syntax Directed Definition and Syntax directed Translation
UNIT – 5 SYNTAX-DIRECTED TRANSLATION
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
Course Revision.. Contents  Lexical Analysis  Our Compiler Structure  Tokens, Lexemes & Patterns  RE’s & Regular Definitions  Transition Diagrams.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
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.
Semantic Analysis Attribute Grammar. Semantic Analysis  Beyond context free grammar  Is x declared before it is used?  Is x declared but never used?
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Lecture 9 Symbol Table and Attributed Grammars
Semantic analysis Jakub Yaghob
Semantic Analysis Chapter 4.
Syntax-Directed Translation
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
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
COP4020 Programming Languages
COP4020 Programming Languages
Chapter 5 Syntax Directed Translation
Presentation transcript:

CSE 420 Lecture

Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program is syntactically well-formed: ▫Class declarations have the correct structure. ▫Expressions are syntactically valid. Does this mean that the program is legal? 2

3

4

Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier phases: ▫Variables are declared before they're used. ▫Expressions have the right types. ▫Arrays can only be instantiated with NewArray. ▫Classes don't inherit from nonexistent base classes … Once we finish semantic analysis, we know that the user's input program is legal. 5

Reject the largest number of incorrect programs. Accept the largest number of correct programs. Do so quickly. 6

Gather useful information about program for later phases: ▫Determine what variables are meant by each identifier. ▫Build an internal representation of inheritance hierarchies. ▫Count how many variables are in scope at each point 7

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. In typed languages as C, semantic analysis involves adding information to the symbol table and performing type checking. The information to be computed is beyond the capabilities of standard parsing techniques, therefore it is not regarded as syntax. As for Lexical and Syntax analysis, also for Semantic Analysis we need both a Representation Formalism and an Implementation Mechanism. 8

The Principle of Syntax Directed Translation states that the meaning of an input sentence is related to its syntactic structure, i.e., to its Parse-Tree. By Syntax Directed Translations we indicate those formalisms for specifying translations for programming language constructs guided by context-free grammars. – We associate Attributes to the grammar symbols representing the language constructs. – Values for attributes are computed by Semantic Rules associated with grammar productions. 9

Evaluation of Semantic Rules may: –Generate Code; –Insert information into the Symbol Table; –Perform Semantic Check; –Issue error messages; etc. 10

There are two notations for attaching semantic rules: 1.Syntax Directed Definitions. High-level specification hiding many implementation details (also called Attribute Grammars). 2.Translation Schemes. More implementation oriented: Indicate the order in which semantic rules are to be evaluated. 11

Syntax Directed Definitions are a generalization of context-free grammars in which: 1.Grammar symbols have an associated set of Attributes; 2.Productions are associated with Semantic Rules for computing the values of attributes. Such formalism generates Annotated Parse-Trees where each node of the tree is a record with a field for each attribute (e.g., X.a indicates the attribute a of the grammar symbol X ). 12

Both semantic analysis and (intermediate) code generation can be described in terms of annotation, or "decoration" of a parse or syntax tree ATTRIBUTE GRAMMARS provide a formal framework for decorating such a tree 13

E  E + T E  E – T E  T T  T * F T  T / F T  F F  - F F  (E) F  const This says nothing about what the program MEANS 14

We can turn this into an attribute grammar as follows : E  E + TE1.val = E2.val + T.val E  E – TE1.val = E2.val - T.val E  TE.val = T.val T  T * FT1.val = T2.val * F.val T  T / FT1.val = T2.val / F.val T  FT.val = F.val F  - FF1.val = - F2.val F  (E)F.val = E.val F  constF.val = C.val 15

The attribute grammar serves to define the semantics of the input program Attribute rules are best thought of as definitions, not assignments They are not necessarily meant to be evaluated at any particular time, or in any particular order, though they do define their left-hand side in terms of the right-hand side The process of evaluating attributes is called annotation, or DECORATION, of the parse tree The code fragments for the rules are called SEMANTIC FUNCTIONS 16

17

The value of an attribute of a grammar symbol at a given parse- tree node is defined by a semantic rule associated with the production used at that node. We distinguish between two kinds of attributes: 1.Synthesized Attributes. They are computed from the values of the attributes of the children nodes. 2.Inherited Attributes. They are computed from the values of the attributes of both the siblings and the parent nodes. 18

A synthesized attribute for a non-terminal A at a parse-tree node N is defined by ▫a semantic rule associated with the production at N. Note that the production must have A as its head. A synthesized attribute at node N is defined only in terms of attribute values at the children of N and at N itself. 19

An inherited attribute for a non-terminal B at a parse- tree node N is defined by ▫A semantic rule associated with the production at the parent of N. Note that the production must have B as a symbol in its body. An inherited attribute at node N is defined only in terms of attribute values at N's parent, N itself, and N's siblings. 20

Terminals can have synthesized attributes, which are given to it by the lexer (not the parser). There are no rules in an SDD giving values to attributes for terminals. Terminals do not have inherited attributes. A non-terminal can have both inherited and synthesized attributes. 21

Parse tree helps us to visualize the translation specified by SDD. The rules of an SDD are applied by first constructing a parse tree ▫then using the rules to evaluate all of the attributes at each of the nodes of the parse tree. A parse tree, showing the value(s) of its attribute(s) is called an annotated parse tree. 22

Annotated parse tree: 3*5 + 4 n With synthesized attributes, we can evaluate attributes in any bottom-up order, such as that of a postorder traversal of the parse tree. val and lexval are synthesized attributes 23

Inherited attributes are useful when the structure of a parse tree does not match the abstract syntax of the source code. They can be used to overcome the mismatch due to grammar designed for parsing rather than translation. 24

An SDD with both inherited and synthesized attributes does not ensure any guaranteed order; even it may not have an order at all. Annotated parse tree: 3*5 25

S-Attributed Definitions An SDD is S-attributed if every attribute is synthesized. Attributes of an S-attributed SDD can be evaluated in bottom-up order of the nodes of parse tree. Evaluation is simple using post-order traversal. postorder(N) { for (each child C of N, from the left) postorder(C); evaluate attributes associated with node N; } S-attributed definitions can be implemented during bottom- up parsing as ▫bottom-up parse corresponds to a postorder traversal ▫postorder corresponds to the order in which an LR parser reduces a production body to its head 26

L-Attributed Definitions Each attribute must be either ▫ Synthesized, or ▫ Inherited, but with the rules limited as follows. Suppose that there is a production A X 1 X 2 X n, there is an inherited attribute X i.a computed by a rule associated with this production. Then the rule may use only: Inherited attributes associated with the head A. Either inherited or synthesized attributes associated with the occurrences of symbols X 1 X 2 X i-1 located to the left of X i Inherited or synthesized attributes associated with this occurrence of X i itself, but only in such a way that there are no cycles in a dependency graph formed by the attributes of this X i. 27

28

Evaluation Orders for SDD's "Dependency graphs" are a useful tool for determining an evaluation order for the attribute instances in a given parse tree. While an annotated parse tree shows the values of attributes ▫A dependency graph helps us determine how those values can be computed. 29

Evaluation Orders for SDD's Each attribute is associated to a node. If a semantic rule associated with a production p defines the value of synthesized attribute A.b in terms of the value of X.c, then graph has an edge from X.c to A.b If a semantic rule associated with a production p defines the value of inherited attribute B.c in terms of value of X.a, then graph has an edge from X.a to B.c 30

Evaluation Orders for SDD's At every node N labeled E with children correspond to the body of production, ▫The synthesized attribute val at N is computed using the values of val at the two childr.en, labeled E and T 31

Evaluation Orders for SDD's Dependency graph for the annotated parse tree for 3*5 32

L-Attributed Definitions-Example 33

SDD For Simple Type Declarations The purpose of L.inh is to pass the declared type down the list of identifiers, so that it can be the appropriate symbol-table entries. Productions 2 and 3 each evaluate the synthesized attribute T.type, giving it the appropriate value, integer or float. Productions 4 and 5 also have a rule in which a function addType is called with two arguments: 1. id.entry, a lexical value that points to a symbol-table object, and 2. L.inh, the type being assigned to every identifier on the list. The function addType properly installs the type L.inh as the type of the represented identifier. 34

Dependency Graph For Simple Type Declarations A dependency graph for the input string float id1, id 2, id3 35

Construction of Syntax Trees SDDs are useful for is construction of syntax trees. A syntax tree is a condensed form of parse tree. Syntax trees are useful for representing programming language constructs like expressions and statements. They help compiler design by decoupling parsing from translation. Each node of a syntax tree represents a construct; ▫The children of the node represent the meaningful components of the construct. 36

Construction of Syntax Trees e.g. a syntax-tree node representing an expression E 1 + E 2 has label + and two children representing the sub expressions E 1 and E 2 Each node is implemented by objects with suitable number of fields; each object will have an op field that is the label of the node with additional fields as follows: ▫If the node is a leaf, an additional field holds the lexical value for the leaf. This is created by function Leaf(op, val) ▫ If the node is an interior node, there are as many fields as the node has children in the syntax tree. This is created by function Node(op, c1, c2,...,ck). 37

Construction of Syntax Trees 38

Constructing Syntax Trees during Top- Down Parsing With a grammar designed for top-down parsing, ▫the same syntax trees are constructed, ▫using the same sequence of steps, ▫even though the structure of the parse trees differs significantly from that of syntax trees. 39

Constructing Syntax Trees during Top- Down Parsing 40

Constructing Syntax Trees during Top- Down Parsing 41

The structure of a TYPE In C, the type int [2][3] can be read as, "array of 2 arrays of 3 integers." The corresponding type expression array(2, array(3, integer)) is represented by the tree as shown below. 42

The structure of a TYPE An annotated parse tree for the input string int a[2][3] is shown below. 43

SDTs with Actions inside Productions Action can be placed at any position in the production body. Action is performed immediately after all symbols left to it are processed. Given B —> X { a } Y, an action a is done after ▫we have recognized X (if X is a terminal), or ▫all terminals derived from X (if X is a nonterminal). If bottom-up parser is used, then action a is performed as soon as X appears on top of the stack. If top-down parser is used, then action a is performed ▫ just before Y is expanded (if Y is nonterminal), or ▫check Y on input (if Y is a terminal). 44

SDTs with Actions inside Productions Any SDT can be implemented as follows: ▫Ignoring actions, parse input and produce parse tree. ▫Add additional children to node N for action in α, where A-> α. ▫Perform preorder traversal of the tree, and as soon as a node labeled by an action is visited, perform that action. 45

SDTs with Actions inside Productions 46

Any Question? 47