Intermediate Code Generation Using Syntax Directed Translation Schemes
Semantic Analysis
We will be seeing one of the important topic of compiler design i.e Semantic Analysis ,As we know that compiler is divided into different phases and each phases are related with each other ,so semantic analysis comes under picture once the syntax is been checked using Syntax Analyzer.
In “Semantic Analyzer” semantics of the source code is verified after the syntax of the code has been verified .Semantic analysis validates the meaning of the code by checking if the sequence of tokens:
•is meaningful and correct
•is associated with the correct type
- is consistent and correct in the way in which control structures and data types are used.
The semantics of the grammar is validated using semantics rule. “Semantic rules” are the collection of the procedure called at appropriate times by the parser as the grammar requires that can be attached to grammar to perform type checking. Semantic rules can be applied to the grammar by attaching attributes to the CFG.
- CFG+ sematic rules =Attribute grammar
Attribute grammar:
E->E+T {E.value=E.value+T.value}**value here is the attribute applied to the grammar.
Based on the way the attributes obtain their values they are divided into two categories:
•Synthesized — obtain the values from child nodes
- Inherited — obtain the values from parents or siblings
Synthesized attribute
The attributes that obtain values from the attribute values of their child nodes
Production :
L->E
E->E1+T
E-> T
T->T*F
T-> F
F->digit
Semantics Rule:
print(E.val)
E.val=E1.val+T.val
E.val = T.val
T.val = T1.val * F.val
T.val = F.val
F.val = digit.lexval
Inherited Attribute
•Inherited attributes take values from parents and/or siblings.
Production
D->LT
T->real
T->int
T->L1,id
L->id
Semantic Rule
L.in=T.type
T.type=real
T.type=int
L1.in=L1.in;addtype(id.entry,L.in)
addtype(id.entry,L.in)
Syntax Directed Translation Scheme into Three Address Code
