Chapter 6(new) - Chapter 6: Semantic Analysis 1 (Static)...

Info iconThis preview shows pages 1–21. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Chapter 6: Semantic Analysis
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 (Static) Semantic Analyzer ==> Semantic Structure - What is the program supposed to do? - Semantics analysis can be done during syntax analysis phase or the final code generator. - typical static semantic features include declarations and type checking . - information (attributes) gathered can be either added to the tree as annotations or entered into the symbol table .
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Output of the semantic analyzer – annotated AST with subscripts from a range
Background image of page 4
5 Two Categories of Semantic Analysis 1. The analysis of a program to meet the definition of the programming language. 1. The analysis of a program to enhance the efficiency of execution of the translated program.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Semantic Analysis Process includes formally: - description of the analyses to perform - implementation of the analysis (translation of the description) that may use appropriate algorithms.
Background image of page 6
7 Description of Semantic Analysis 1. Identify attributes (properties) of language (syntactic) entities. 1. Write attribute equations (or semantic rules ) that express how the computation of such attributes is related to the grammar rules of the language. Such a set of attributes and equations is called an attribute grammar .
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Syntax-directed semantics - The semantic content of a program is closely related to its syntax. - All modern languages have this property.
Background image of page 8
9 Attributes - An attribute is any property of a programming language construct. - Typical examples of attributes are: the data type of a variable, the value of an expression, the location of a variable in memory, the object code of a procedure, the number of significant digits in a number. - Attribute corresponds to the name of a field of a structure.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Attribute Grammars In syntax-directed semantics, attributes are associated with grammar symbols of the language. That is, if X is a grammar symbol and a is an attribute associated to X, then we write X. a for the value of a associated to X. For each grammar rule X 0 -> X 1 X 2 …X n the values of the attributes X i . a j of each grammar symbol X i are related to the values of the attributes of other grammar symbols in the rule.
Background image of page 10
11 That is, each relationship is specified by an attribute equation or semantic rule of the form: X i . a j = f ij (X 0 . a 1 ,.., X 0 . a k ,.., X 1 . a 1 ,.., X 1 . a k , . ., X n . a 1 ,.., X n . a k ) An attribute grammar for the attributes a 1 ,…, a k is the collection of all such attribute equations (semantic rules), for all the grammar rules of the language.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 number.val must be computed prior to factor.val
Background image of page 12
13 Attribute grammars may involve several interdependent attributes.
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
e.g. 345o 128d 128o (x)
Background image of page 14
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
16 Attribute grammars may be defined for different purposes.
Background image of page 16
17
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
* - 42 3 34 (34 – 3) * 42 term 1 . tree = mkOpNode(*,term 2 .tree,factor.tree) factor.tree = mkNumNode(number.lexval)
Background image of page 18
19 Algorithms for attribute computation Dependency graph and evaluation order
Background image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Attribute grammar for simple C-like variable declarations Grammar Rules Semantic Rules
Background image of page 20
Image of page 21
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 111

Chapter 6(new) - Chapter 6: Semantic Analysis 1 (Static)...

This preview shows document pages 1 - 21. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online