Semantic Analysis: Instructor: Venkata Ramana Badarla
Semantic Analysis: Instructor: Venkata Ramana Badarla
• Semantic consistency that cannot be handled at the parsing stage is handled here
• Parsers cannot handle context-sensitive features of programming languages
• These are static semantics of programming languages and can be checked by the
semantic analyzer
• Variables are declared before use
• Types match on both sides of assignments
• Parameter types and number match in declaration and use
• Compilers can only generate code to check dynamic semantics of programming
languages at runtime
• whether an overflow will occur during an arithmetic operation
• whether array limits will be crossed during execution
• whether recursion will cross stack limits
• whether heap memory will be insufficient
In main
• Types of p and return type of
dot prod match
int dot_prod (int x[], int y[]) • Number and type of the parameters of
{ dot prod are the same in both its
int d, i; declaration and use
d = 0;
for (i = 0; i < 10; i++) • p is declared before use, same for a
d += x[i] * y[i]; and b
return d; In dot_prod
}
• d and i are declared before use
main ()
{ • Type of d matches the return type of
int p; int a[10], b[10]; dot_prod
p = dot_prod (a, b);
• Type of d matches the result type of *
}
• Elements of arrays x and y are
compatible with *
In function ’main’
error in 3:too few arguments to fn ‘dot_product’
error in 4:too many arguments to fn ’dot_product’
error in 5:incompatible types in assignment
warning in 5:format ‘%d’ expects type ‘int’,
but argument 2 has type ‘int *’
Syntax tree: a - 4 + c
Syntax tree: a - 4 + c
Instructor: Venkata Ramana Badarla Semantic Analysis October 11, 2023 15 / 68
LAG/L-SDD for Construction of a Syntax Tree
1 Given a CFG
S → ABC ,
A → aA | a,
B → bB | b,
C → cC | c
which generate L(G) = {am b n c p |m, n, p ≥ 1}. Define an AG (attribute grammar)
based on this CFG to generate L = {an b n c n |n ≥ 1}
Assume synthesized attributes status with NT S and count with the NTs
A, B, and C .
• SDT is a complementary notation to SDD and all of the applications of SDDs can
be implemented using SDTs.
• Unlike in SDDs, the program fragments (also known as semantic actions) can
appear at any position within a production body.
• By convention, we place curly braces around actions; if braces are needed as
grammar symbols, then we quote them.
• Example,
A → XY {action : a}
A → X {action : a}Y
• Implementation:
• An action a should be executed before proceeding with the execution of
Nonterminals that follow it.
• Transform the grammar adding a marker M symbol A → X MY and using an
empty RHS production M → {action : a}
SDD SDT
Procedure dfvisit(n:node)
Begin
Foreach child m of n from left to right do
Evaluate inherited attributes of m;
dfvisit(m);
End
Evaluate synthesized attributes of n
End
S → while (C) S1
Inherited attributes
• S.next, points to the code that
label L1:
must be executed after S finishes C.code
• C.true, points to the code to be if(C is false)
executed if C is true goto C.false (i.e. S.next)
else
• C.false, points to the code to be
goto C.true (i.e L2)
executed if C is false label L2:
Synthesized attributes S.code
• C.code, denotes the intermediate goto L1
code for the condition
• S.code, denotes the intermediate
code for statement(s)
SDT
DAG: a + a * (b - c) + (b - c) * d
Quadruples: a = b * - c + b * -c
Triples:
Triples:
Triples:
Indirect triples:
Indirect triples:
• Arithmetic expressions
• Arrays
• Boolean Expressions
• Selection Statements
• Iterative Statements
• Procedures
• Etc.
• Labels generated should be attached with address of the target three address code
instruction
• Backword jumps (can be done in single pass) and forward jumps (needs two passes)
• Backpatching – technique to attach the labels to addresses in a single pass
• makelist (i) – creates a list containining i which is an index into the array of
instructions, and returns pointer to the list.
• merge (p1, p2) – concatenates two lists pointed by p1 and p2, and returns a
pointer to the concatenated list.
• backpactch(p, i) – inserts target label i for each of the instruction on the list
pointed by p.
1 n = f(a[i])
2 t1 = i*4
3 t2 = a[t1]
4 param t2
5 t3 = call f, 1
6 n = t3
Type conversion
Operations on Arrays
1 a[i] = x
2 y = a[j]
3 a[i+1] = a[j*2] + 3
Location of ith element in a[i]
Translation
base + i * width
1 t1 = j*2
2 t2 = a[t1] Multidimensional Arrays
3 t3 = t2 + 3 - Find width of each dimension
4 t4 = i + 1 and add recursively
5 a[t4] = t3 - Find total numbers of elements
x width of element
Array indexing b[i][j](array indices
start from 0)
1 base + i * w1 + j * w2 (OR)
2 base + (i* N + j) * elem_width