Semantics analysis

3,057 views 27 slides Aug 12, 2021
Slide 1
Slide 1 of 27
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

About This Presentation

Semantics is the third phase of compiler construction.


Slide Content

Semantics 1

Semantic Analysis 3 Semantics of a language provide meaning to its constructs like tokens and syntax structure. Semantic analysis judge whether the syntax structure constructed in the source program provide any meaning or not. For example - Int a=“book”; is syntactically correct but generates a semantic error. Solution – CFG + Semantic Rules = Syntax directed definition.

Semantic Analysis output of syntax analysis which is a parse tree is the input of semantic analysis which is also a parse tree with some additional attributes called annotated parse tree. Some semantic rules are associated with the production rules in semantic analysis, like: Production                      Semantic Rule E->E1+T                               E.code =E1.code|| T.code ||'+' 4

Semantic Analysis In which certain checks are performed to ensure that the components of a program fit together meaningfully. The semantic analysis phase checks the source program for semantic errors and gathers type information for the subsequent code- generation phase.

Ways to associate semantic rules There are two ways to represent the semantic rules associated with grammar symbols  Syntax-Directed Definitions (SDD)  Syntax-Directed Translation Schemes (SDT)

Syntax-Directed Definitions A syntax-directed definition (SDD) is a context-free grammar together with attributes and rules. Attributes are associated with grammar symbols and rules are associated with productions.   PRODUCTION                SEMANTIC RULE  E → E1 + T                      E.code = E1.code || T.code || ‘+’

Attribute Grammar Attribute grammar is a context-free grammar where some additional information (attributes) are appended to one or more of its non-terminals in order to provide context-sensitive information. Attribute grammar is a medium to provide semantics to the context-free grammar and it can help specify the syntax and semantics of a programming language. Example E → E + T { E.value = E.value + T.value }

synthesized attributes Synthesized attribute get values from the attribute values of their child nodes. Example S → ABC If S is taking values from its child nodes (A,B,C), then it is said to be a synthesized attribute, as the values of ABC are synthesized to S.

Inherited attributes I nherited attributes can take values from parent and/or siblings Example S → ABC A can get values from S, B and C. B can take values from S, A, and C. C can take values from S, A, and B.

SDD for expression grammar with synthesized attributes Annotated Parse Tree for 3*5+4n

SDD for expression grammar with inherited attributes Annotated Parse Tree for 3*5

S-attributed SDT If an SDT uses only synthesized attributes it is called S-attributed SDT. These attributes are evaluated using S-attributed SDTs that have their semantic actions written after the production (right hand side). Example: S → ABC S can take values from A, B, and C only.

L-attributed SDT This form of SDT uses both synthesized and inherited attributes with restriction of not taking values from right siblings. In L-attributed SDTs, a non-terminal can get values from its parent, child, and sibling nodes. As in the following production Example: S → ABC S can take values from A, B, and C. A can take values from S only. B can take values from S and A . C can get values from S, A, and B . No non-terminal can get values from the sibling to its right .

SYNTAX -Directed TRANSLATION (SDT) Background: Parser uses a CFG (Context-free- Grammer ) to validate the input string and produce output for next phase of the compiler. Output could be either a parse tree or abstract syntax tree Now to interleave semantic analysis with syntax analysis phase of the compiler, we use Syntax Directed Translation.

SYNTAX -Directed TRANSLATION (SDT) Definition: Syntax Directed Translation are augmented rules to the grammar that facilitate semantic analysis. Grammar + semantic rules = SDT

Example : E -> E+T | T T -> T*F | F F -> Id Grammar RULES E -> E+T { E.val = E.val + T.val } E -> T { E.val = T.val } T -> T*F { T.val = T.val * F.val } T -> F { T.val = F.val } F -> ID { F.val = ID.lexval }

Application of SDT Executing arithmetic expression Conversion from infix to postfix Conversion from infix to prefix Conversion from binary to decimal Counting number of reductions Creating syntax tree Generating the intermediate code Type checking Storing type info into symbol table

Example: Grammar : E -> E+T | T. T -> T*F | F F -> digit input string: 3*5+4

Annotated parse tree

Syntax directed translation schemes The  syntax directed translation scheme  is used to evaluate the order of semantic rules. In  translation scheme , the semantic rules are embedded within the right side of the productions. The position at which an action is to be executed is shown by enclosed between braces.

Errors 5 Program submitted to a compiler often have errors of various kinds So, good compiler should be able to detect as many errors as possible in various ways and also recover from them. Even in the presence of errors ,the compiler should scan the program and try to compile all of it.

CLASSIFICATION OF COMPILE TIME ERRORS lexical errors Syntactic errors Semantic errors

Type Mismatch Int a = 10 ; String b =“ Hello World ”; Double sum= a+b ; Undeclared or multiple declare variable Int a , b ; int b ; a = 10 ; b = 10 ; Sum = a+b ;

Actual and formal Parameter mismatch Fun (int a) { STATEMENTS ; } String value = “ Hello World ” ; Fun (value) ; Reserved identifier mismatch int void = 10 ;

Manage type casting 7 When a binary arithmetic operator is applied to an integer and real. In this case, the compiler may need to be converting the integer to a real. As shown in figure given below Float position , initial , Rate; Initial = 10.5 ; Rate = 10.5 ; Position = initial + Rate * 60 ; int a=10 ; float b=15.5 ; float c= a+int (b) ;

References www.tutorialspoint.com www.geeksforgeeks.org

Any Question?