Académique Documents
Professionnel Documents
Culture Documents
Semantic analysis
Special Forms
Semantic analysis
S-Attributed Grammars
S-attributed grammars
S-attributed grammar: An
attribute grammar that uses
synthesized attributes exclusively
Semantic analysis
S-Attributed Grammars
id
t:int
id
t:int
Semantic analysis
S-Attributed Grammars
Reducing by:
Expr t:int
id
t:int
id
t:int
Semantic analysis
S-Attributed Grammars
Reducing by:
Expr t:int
id
t:int
Expr t:int
id
t:int
Semantic analysis
S-Attributed Grammars
Expr
Expr t:int
id
t:int
t:int
Expr t:int
id
t:int
Semantic analysis
S-Attributed Grammars
Decl
Mode IDList ;
Mode
int
Mode
real
IDList
IDList,id
IDList
id
Semantic analysis
S-Attributed Grammars
Decl
Mode IDList ;
Mode
int
Mode
real
IDList
IDList1,id
IDList
id
{IDList.type = Mode.dtype}
{Mode.dtype = int}
{Mode.dtype = real}
{addSymbTable(id.token,IDList.type)
IDList1.type = IDList.type }
{addSymbTable(id.token,IDList.type)}
10
Semantic analysis
S-Attributed Grammars
{IDList.type = Mode.dtype}
{Mode.dtype = int}
{Mode.dtype = real}
{addSymbTable(id.token,IDList.type)
IDList1.type = IDList.type }
{addSymbTable(id.token,IDList.type)}
IDList
id
Decl
dt:int
Mode
IDList t:int
IDList t:int
int
id
id
11
Semantic analysis
S-Attributed Grammars
{IDList.type = Mode.dtype}
{Mode.dtype = int}
{Mode.dtype = real}
{addSymbTable(id.token,IDList.type)
IDList1.type = IDList.type }
{addSymbTable(id.token,IDList.type)}
IDList
id
Decl
dt:int
Mode
IDList t:int
IDList t:int
int
id
id
12
Semantic analysis
S-Attributed Grammars
{ Mode.dtype = int }
{ Mode.dtype = real }
IDList
IDList1,id
{ IDList.tokens = append(IDList1.tokens, id.token) }
IDList
id
{IDList.tokens = list(id.token)}
13
Semantic analysis
S-Attributed Grammars
Mode
IDList tokens
IDList tokens
int
id token
id token ;
14
Semantic analysis
S-Attributed Grammars
15
Semantic analysis
L-Attributed Grammars
L-attributed grammars
P :- C1 C2 Cn
Any
depends on:
1. attributes of the other child nodes to its left (e.g. it can use
attributes from nodes C1 Ck-1)
2. inherited attributes of P itself.
Semantic analysis
L-Attributed Grammars
synthesized attributes
18
Semantic analysis
L-Attributed Grammars
C1
C2
C3
19
Semantic analysis
L-Attributed Grammars
20
10
Semantic analysis
L-Attributed Grammars
synthesized attributes
ii)
b)
21
Semantic analysis
L-Attributed Grammars
22
11
Semantic analysis
Embedded Actions
For example:
E :- T {R.i := T.val} R {E.val := R.s}
24
12
Semantic analysis
Embedded Actions
E
E :-
{R.i := T.val}
{E.val := R.s}
T
R
T val=4
i=4
num val=4
25
Semantic analysis
Embedded Actions
13
Semantic analysis
Embedded Actions
Old:
E :-
T
R
{R.i := T.val}
{E.val := R.s}
New:
E :- T X R
X :-
{E.val := R.s}
{R.i := T.val}
27
Semantic analysis
Embedded Actions
X :-
{R.i := T.val}
28
14
5.7 Summary
Semantic analysis
Summary
30
15
Semantic analysis
Summary
31
Semantic analysis
Summary
32
16
5.8 Examples
Semantic analysis
Examples
List Depth
List ( IList )
{ IList.depth=IList.depth + 1 ;
print Depth:, List.depth }
IList IList1 ( IList2 ) { if (IList1.depth > IList2.depth):
IList.depth = IList1.depth
else:
IList.depth = IList2.depth +1 }
ILIst
{ IList.depth=0 }
Draw the parse tree and calculate attributes for:
((()) ((() ())))
34
17
Semantic analysis
L-Attributed Grammars
L-attributed Grammars & Top-Down Analysis
Assume a single-pass compiler, where semantic actions are performed as
the parse tree is constructed,
In a top-down parser, there is no problem, as the growing of the tree
exactly matches the recursive-descent ordering of the evaluation rules.
E.g.,
When we expand the start symbol E with production D :- T L, then we
can apply the rules which pass attributes from D to T and L.
We next expand T using T :- int
As int is a terminal, we then return to the T node, and can now pass
any synthesised attributes from int up to T.
As T is now finished, we pass synthesised attributes up to D
Moving on to Ts sister, L, it can be given any synthesised attributes of T
We then construct the subtree for L.
Etc.
35
18