introduction to semantic analysis .pptx

sivamathi12 78 views 30 slides Aug 27, 2024
Slide 1
Slide 1 of 30
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30

About This Presentation

.


Slide Content

Unit III -ii Introduction to Semantic Analysis- Type Checking- Attributes and Specification of Semantic Rules - Syntax-directed-Definition --Syntax-directed Translation Scheme - Dependency Graph - Directed Acyclic Graph Construction

4.1 Introduction Semantic Analysis is the third phase of Compiler . Semantic Analysis makes sure that declarations and statements of program are semantically correct . It is a collection of procedures which is called by parser as and when required by grammar. Both syntax tree of previous phase and symbol table are used to check the consistency of the given code. Type checking is an important part of semantic analysis where compiler makes sure that each operator has matching operands.

Semantic Analyzer: It uses syntax tree and symbol table to check whether the given program is semantically consistent with language definition. It gathers type information and stores it in either syntax tree or symbol table. This type information is subsequently used by compiler during intermediate-code generation.

Semantic Errors: Type mismatch Undeclared variable Multiple declaration of variable in a scope. Accessing an out of scope variable. Actual and formal parameter mismatch.

In type checking data types are checked. Also incompatible operands of an operator is also checked. Label checking involves checking if the reference labels exist in program , names of variables , same names for two variables. Flow control checking , control structures are validated.

Static Semantics – It is named so because of the fact that these are checked at compile time . The static semantics and meaning of program during execution, are indirectly related. Dynamic Semantic Analysis – It defines the meaning of different units of program like expressions and statements. These are checked at runtime unlike static semantics.

Fig 4. Position of type checking

4.2 Attributes and Specification of Semantic Rules CFG -> specify syntax structure of language. semantic analysis -> add some information to grammar. So CFG is extended by set of attributes and set of semantic rules . semantic rules specify how attributed values of symbols of production are manipulated. Two notations are used to associate semantics with production: Syntax directed definition. Translation schemes.

Syntax Directed Translation: high level specification for translation. Hide implementation details from user User need not specify the order in which translation takes place.

Evaluation order of semantic rules: Comprises generating code Save information in symbol table Issue error message translate token stream Translation Schemes : Indicate the order in which translation takes place. Give little bit of information about implementation.

Dependency Graph: Semantic rules set up dependencies between attributes. It is represented by a graph called Dependency Graph. A dependency graph is used to represent the flow of information among the attributes in a parse tree

Annotated parse tree or decorated parse tree: Evaluating semantic rules means -> give values for attributes in parse tree for given input string. A parse tree with such values is called Annotated parse tree or decorated parse tree.

Attribute Grammar: It is an extension of CFG. Used to describe features of programming languages that was not described in BNF(Backus Normal Form). Backus-Naur Form (BNF) is a notation used to describe the syntax of programming languages. It was introduced by John Backus and Peter Naur in the 1960s. In Attribute Grammar some additional information called Attribute is appended to non-terminals. Each attribute has well defined domain of values such as integer, float,char et.. Attribute Grammar pass values among nodes of the tree.

Based on the way the attributes obtain values, it is divided into two categories: Synthesised attributes : values obtain from the child I t is defined by the semantic rule associated with the production at the node. The non-terminal concerned must be in the head of the production. Inherited attributes: take values from the parent It is defined by the semantic rule associated with the production at the parent of node. The non-terminal concerned must be in the body of the production.

Syntax Directed Definition (SDD) and Types of Syntax Directed Definitions - Computer Notes (ecomputernotes.com)

Syntax Directed Definition Syntax directed definition specifies the values of attributes by associating semantic rules with the grammar productions. It is a context free grammar with attributes and rules together which are associated with grammar symbols and productions respectively. The process of syntax directed translation is two-fold: Construction of syntax tree and Computing values of attributes at each node by visiting the nodes of syntax tree.

They are usually enclosed within curly braces ({ }). It can occur anywhere in a production but usually at the end of production. Eg : E—> E1 + T {print ‘+’}

Example 2:
Tags