2301373Semantic Analysis1 Checking what parsers cannot.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
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.
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.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Syntax-Directed Translation.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
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-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
Syntax-Directed Translation
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.
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.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
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.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
COMPILER CONSTRUCTION
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
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.
ISBN Chapter 3 Describing Semantics.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Syntax Directed Definition and 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.
CSC 4181 Compiler Construction
6. Semantic Analysis. Semantic Analysis Phase – Purpose: compute additional information needed for compilation that is beyond the capabilities of Context-
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
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.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Lecture 9 Symbol Table and Attributed Grammars
A Simple Syntax-Directed Translator
Compiler Construction
Chapter 5 Syntax Directed Translation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Syntax-Directed Translation Part I
Semantic Analysis Chapter 6.
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Chapter 6 Intermediate-Code Generation
Compilers B V Sai Aravind (11CS10008).
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation Part I
COP4020 Programming Languages
Lecture 16 Boolean Expressions and Control Flow
Chapter 5 Syntax Directed Translation
Presentation transcript:

Semantic Analysis1 Checking what parsers cannot

Semantic Analysis2 Outline  Overview  Attribute grammar Attribute Semantic rule  Computing attributes Evaluation order Synthesized and Inherited attribute Computation of attributes during parsing  Type checking Type declaration Type inference  Symbol table

Semantic Analysis3 Overview  Semantics Static semantics  Data type (in some languages e.g. C, FORTRAN)  Names Run-time semantics  Data value  Data type (in other languages e.g. Perl)  Semantic analyzer Determine if the semantic of the program is correct according to the definition of the language Concern only static semantics

Semantic Analysis4 Overview (cont’d)  How to describe semantics Associated to syntax  Syntax-directed semantics An attribute represents a semantic concept. A semantic rule (or attribute equation) is associated to a syntactic rule. A semantic rule describes the relationship between attributes of symbols in a syntactic rule.

Semantic Analysis5 Attribute Grammar  Language construct Variables, function declarations, statements  Attribute Property of language construct  Name, address, type, value, and scope are attributes of variables.  Return type, parameter types, return address, and scope are attributes of functions.  Syntax tree is an attributes of code sections.  Attribute equation (semantic rule) Associated with grammar production Specify the relationship between attributes of construct in a syntactic rule.  Attribute grammar Set of attribute equations

Semantic Analysis6 Example of Attribute Grammar  Construct: num digit  Attribute: val  Grammar num -> num digit num -> digit digit -> 0|1|…|9 Grammar ruleSemantic rule num 1 -> num 2 digit num 1.val= num 2.val*10+ digit.val num -> digitnum.val = digit.val digit -> 0digit.val = 0 digit -> 1digit.val = 1 digit -> 2digit.val = 2 digit -> 3digit.val = 3 digit -> 4digit.val = 4 digit -> 5digit.val = 5 digit -> 6digit.val = 6 digit -> 7digit.val = 7 digit -> 8digit.val = 8 digit -> 9digit.val = 9

Semantic Analysis7 Parse Tree with Attributes num val = 45*10+5 num val = 4*10+5 digit val = 5 num val = 4 digit val = 5 digit val = Grammar ruleSemantic rule num 1 -> num 2 digit num 1.val= num 2.val*10+ digit.val num -> digitnum.val = digit.val digit -> 0digit.val = 0 digit -> 1digit.val = 1 digit -> 2digit.val = 2 digit -> 3digit.val = 3 digit -> 4digit.val = 4 digit -> 5digit.val = 5 digit -> 6digit.val = 6 digit -> 7digit.val = 7 digit -> 8digit.val = 8 digit -> 9digit.val = 9

Semantic Analysis8 Attribute Grammar for Data Type Declaration Grammar Rule dec -> type varList type -> int type -> float varList 1 -> id, varList 2 varList -> id Semantic Rule varList.dtype = type.dtype type.dtype = int type.dtype = real id.dtype = varList 1.dtype varList 2.dtype= varList 1.dtype id.dtype = varList.dtype type dec varList float idvarList, id dtype= real

Semantic Analysis9 Another Example of Attribute Grammar Grammar Rule Bnum -> num baseC baseC ->o baseC -> d num 1 -> num 2 digit num -> digit digit -> 0|1|…|7 digit -> 8 | 9 val=19 base=8 val=3 base=8 val=19 base=8 val=2 base=8 val=2 Bnum num o baseC digit Semantic Rule num.base=baseC.base Bnum.val=num.val baseC.base=8 baseC.base=10 num 2.base=num 1.base digit.base=num 1.base num 1.val= if digit.val=error then error else num 2.val*num 1.base + digit.val num.val=digit.val digit.base=num.base digit.val = numval(D), where D is 0, 1,…,7 digit.val = if digit.base=8 then error else numval(D), where D is 8,9

Semantic Analysis10 Evaluation Order  From semantic rule X.a=f(X 1.a 1, X 2.a 2,…, X n.a n ) Value of a in node X depends on the values of a 1 in X 1, a 2 in X 2,…, and a n in X n. The order of evaluation can be shown in a dependency graph, which is a directed acyclic graph (DAG). X.a X 2.a 2 X 1.a 1 X n.a n … X.a X 2.a 2 X 1.a 1 X n.a n … X.a X 2.a 2 X 1.a 1 X n.a n … X.a X 2.a 2 X 1.a 1 X n.a n …

Semantic Analysis11 Dependency Graph: Example 1 num val = 45*10+5 num val = 4*10+5 digit val = 5 num val = 4 digit val = 5 digit val = Grammar ruleSemantic rule num 1 -> num 2 digit num 1.val= num 2.val*10+ digit.val num -> digitnum.val = digit.val digit -> 0digit.val = 0 digit -> 1digit.val = 1 digit -> 2digit.val = 2 digit -> 3digit.val = 3 digit -> 4digit.val = 4 digit -> 5digit.val = 5 digit -> 6digit.val = 6 digit -> 7digit.val = 7 digit -> 8digit.val = 8 digit -> 9digit.val = 9

Semantic Analysis12 Dependency Graph: Example 2 Grammar Rule dec -> type varList type -> int type -> float varList 1 -> id, varList 2 varList -> id Semantic Rule varList.dtype = type.dtype type.dtype = int type.dtype = real id.dtype = varList 1.dtype varList 2.dtype= varList 1.dtype id.dtype = varList.dtype type dec varList float idvarList, id dtype= real

Semantic Analysis13 Dependency Graph:Example 3 Grammar RuleSemantic Rule Bnum -> num baseCnum.base=baseC.base Bnum.val=num.val baseC ->obaseC.base=8 baseC -> dbaseC.base=10 num 1 -> num 2 digitnum 2.base=num 1.base digit.base=num 1.base num 1.val= if digit.val=error then error else num 2.val*num 1.base + digit.val num -> digitnum.val=digit.val digit.base=num.base digit -> 0|1|…|7digit.val = numval(D), where D is 0, 1,…,7 digit -> 8 | 9digit.val = if digit.base=8 then error else numval(D), where D is 8,9 Bnum num o baseC digit

Semantic Analysis14 Rule-based Attribute Evaluation  Order of attribute evaluation can be fixed at compiler construction Attribute grammar is to be analyzed in order to find the orger of evaluation  Used often in practice  Not general method  Two types of attributes Synthesized attributes Inherited attributes

Semantic Analysis15 Synthesized Attributes  An attribute a is a synthesized attribute if for a grammar rule A ->X 1 X 2 … X n, an attribute equation with a on the LHS is of the form A. a = f( X 1. a 1, X 2. a 2, … X n. a n ), or all dependencies point from child to parent in the parse tree  If all attributes in an attribute grammar are synthesized attributes, the grammar is called an S- attributed grammar. Grammar ruleSemantic rule num 1 -> num 2 digit num 1.val= num 2.val*10+ digit.val num -> digitnum.val = digit.val digit -> 0|1|…|9digit.val = val(D) num digit numdigit 5 4 5

Semantic Analysis16 Order of Evaluation for Synthesized Attributes  Use postorder (or bottom- up) evaluation Procedure PostEval (T:node) {for each child C of T {PostEval(C); } compute all synthesized attributes of T } num digit numdigit

Semantic Analysis17 Inherited Attributes  An attribute is an inherited attribute if it is not a synthesized attribute.  An attribute grammar is an L-attributed grammar if for each inherited attribute a j at X i in each grammar rule X -> X 1 X 2 … X n depends on the value of attributes of symbols X, X 1, X 2,…, X i-1. Grammar RuleSemantic Rule dec -> type varListvarList.dtype = type.dtype type -> int type.dtype = int type -> float type.dtype = real varList 1 -> id, varList 2 id.dtype = varList 1.dtype varList -> id id.dtype = varList.dtype type dec varList float idvarList, id

Semantic Analysis18 Order of Evaluation for Inherited Attributes  Use preorder and inorder evaluation together Procedure Eval (T:node) {case nodeType(T) of dec: {Eval(typeChild(T)); varList.dtype=type.dtype; Eval(varChild(T));} type: {if child(T) is int then T.dtype=int; if child(T) is float then T.dtype=real; } varList: {leftChild(T).dtype=T.dtype; if (rightmostChild(T) is not null) then {rightmostChild(T).dtype=T.dtype; Eval(rightChild(T));} } type dec varList float idvarList, id float

Semantic Analysis19 Another Example of Inherited Attributes Proc Eval(T:node) { case Nodetype(T) of Bnum: { Eval(rightChild(T)); (leftChild(T)).base= (rightChild(T)).base; Eval(leftChild(T)); T.val=leftChild(T).val;} baseC: { if child(T)=o then T.base=8; if child(T)=d thenT.base=10;} num: { leftChild(T).base=T.base; Eval(leftChild(T)); if (rightChild(T)!=null) then { rightChild(T).base=T.base; Eval(rightChild(T); T.val=f(T); } else T.val=leftChild(T).val; cal val} digit: { … } } Grammar RuleSemantic Rule Bnum -> num baseCnum.base=baseC.base; Bnum.val=num.val baseC ->obaseC.base=8 baseC -> dbaseC.base=10 num 1 -> num 2 digitnum 2.case=num 1.base; digit.base=num 1.base; num 1.val= if digit.val=error then error else num 2.val*num 1.base + digit.val num -> digitnum.val=digit.val; digit.base=num.base digit -> 0|1|…|7digit.val = numval(D) digit -> 8 | 9 digit.val = if digit.base=8 then error else numval(D) Bnum num o baseC digit base=8 val=5 base=8 val=5 base=8 val=4 val=44

Semantic Analysis20 Evaluation Order for Synthesized + Inherited Attributes Procedure CombinedEval(T:node) {for each child C of T {compute all inherited attributes of C; CombinedEval(C); } compute all synthesized attributes of T; }

Semantic Analysis21 Attribute Computation During Parsing