Semanticanalysis
Semanticanalysis
com
m
This distinguishes abstract syntax trees from concrete syntax trees, traditionally
co
designated parse trees, which are often built by a parser during the source code translation
and compiling process. Once built, additional information is added to the AST by means
n.
of subsequent processing, e.g., contextual analysis. Please refer the below fig.
tio
ca
du
ie
sh
ak
ASTs represent the syntactic structure of the some code. The trees of programming
.s
constructs such as expressions, flow control statements, etc - grouped into operators
(interior nodes) and operands (leaves). For example, the syntax tree for the expression i +
w
9 would have the operator + as root, the variable i as the operator's left child, and the
w
The difference here is that nonterminals and terminals don't play a role, as ASTs don't
deal with grammars and string generation, but programming constructs, and thus they
represent relationships between such constructs, and not the ways they are generated by a
grammar.
Note that the operators themselves are programming constructs in a given language, and
don't have to be actual computational operators (like + is): for loops would also be treated
in this way. For example, you could have a syntax tree such as for [ expr, expr, expr,
Chapter 4 Page 1
www.sakshieducation.com
www.sakshieducation.com
stmnt ] (represented inline), where for is an operator, and the elements inside the square
brackets are its children (representing C's for syntax) - also composed out of operators
etc.
ASTs are usually generated by compilers in the syntax analysis (parsing) phase as well,
and are used later for semantic analysis, intermediate representation, code generation, etc.
Here's a graphical representation of an AST:
m
co
n.
tio
ca
du
Parse trees tell us exactly how a string was parsed. Parse trees contain more information
ie
than we need ie., we only need the basic shape of the tree, not where every non-terminal
sh
without non-terminals.
.s
Chapter 4 Page 2
www.sakshieducation.com
www.sakshieducation.com
The term Polish notation is sometimes taken (as the opposite of infix notation) to also
include Polish postfix notation, or reverse Polish notation (RPN), in which the operator is
placed after the operands.
When Polish notation is used as a syntax for mathematical expressions by programming
language interpreters, it is readily parsed into abstract syntax trees and can, in fact, define
a one-to-one representation for the same. Because of this, Lisp (see below) and related
programming languages define their entire syntax in terms of prefix notation (and others
use postfix notation).
m
co
Three Address Code (TAC):
In computer science, three-address code (often abbreviated to TAC or 3AC) is
n.
an intermediate code used by optimizing compilers to aid in the implementation of code-
tio
improving transformations.
Each TAC instruction has at most three operands and is typically a combination of
ca
assignment and a binary operator. For example, t1 := t2 + t3 . The name derives from the
use of three operands in these statements even though instructions with fewer operands
du
may occur.
ie
rather symbolic addresses that will be translated into actual addresses during register
allocation.
ak
It is also not uncommon that operand names are numbered sequentially since three-
.s
In three-address code, this would be broken down into several separate instructions.
w
These instructions translate more easily to assembly language. It is also easier to detect
w
common sub-expressions for shortening the code. In the following example, one
calculation is composed of several smaller ones:
Example:
# Calculate one solution to the [[quadratic equation]].
x = (-b + sqrt(b^2 - 4*a*c)) / (2*a)
t1 := b * b
t2 := 4 * a
Chapter 4 Page 3
www.sakshieducation.com
www.sakshieducation.com
t3 := t2 * c
t4 := t1 - t3
t5 := sqrt(t4)
t6 := 0 - b
t7 := t5 + t6
t8 := 2 * a
t9 := t7 / t8
x := t9
m
Three-address code may have conditional and unconditional jumps and methods of
co
accessing memory. It may also have methods of calling functions, or it may reduce these
to jumps. In this way, three-address code may be useful in control-flow analysis. In the
n.
following C-like example, a loop stores the squares of the numbers between 0 and 9:
tio
...
for (i = 0; i < 10; ++i) { ca
b[i] = i*i;
}
du
...
t1 := 0 ; initialize i
ie
t2 := t1 * t1 ; square of i
t3 := t1 * 4 ; word-align address
ak
t1 := t1 + 1 ; increase i
w
L2:
w
Attribute Grammer:
An attribute grammar is a formal way to define attributes for the productions of
a formal grammar, associating these attributes to values. The evaluation occurs in the
nodes of the abstract syntax tree, when the language is processed by
some parser or compiler.
Chapter 4 Page 4
www.sakshieducation.com
www.sakshieducation.com
The attributes are divided into two groups: synthesized attributes and inherited attributes.
o The synthesized attributes are the result of the attribute evaluation rules, and may
also use the values of the inherited attributes.
o The inherited attributes are passed down from parent nodes.
o In some approaches, synthesized attributes are used to pass semantic information
up the parse tree, while inherited attributes help pass semantic information down
and across it.
o For instance, when constructing a language translation tool, such as a compiler, it
m
may be used to assign semantic values to syntax constructions.
co
o Also, it is possible to validate semantic checks associated with a grammar,
representing the rules of a language not explicitly imparted by the syntax
n.
definition.
tio
Attribute grammars can also be used to translate the syntax tree directly into code for
some specific machine, or into some intermediate language.
ca
One strength of attribute grammars is that they can transport information from anywhere
in the abstract syntax tree to anywhere else, in a controlled and formal way. Please check
du
Chapter 4 Page 5
www.sakshieducation.com
www.sakshieducation.com
m
o Perform Semantic Check
co
o Issue error messages etc.
There are two notations for attaching semantic rules:
n.
1. Syntax Directed Definitions. High-level specification hiding many implementation
tio
details (also called Attribute Grammars).
2. Translation Schemes. More implementation oriented: Indicate the order in which
ca
semantic rules are to be evaluated.
The construction of parse tree is as show in the figure.
du
ie
sh
ak
.s
w
w
w
Chapter 4 Page 6
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
tio
ca
du
ie
sh
ak
.s
w
w
w
Chapter 4 Page 7
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
tio
o (a) ca
o (b)
du
o (c)
o (d)
ie
2. Question
sh
ak
.s
w
w
w
o (a)
o (b)
o (c)
o (d)
3. Question
Chapter 4 Page 8
www.sakshieducation.com
www.sakshieducation.com
m
co
o (a)
n.
o (b)
tio
o (c) ca
o (d)
4. Question
du
ie
sh
ak
.s
w
o (a)
w
o (b)
w
o (c)
o (d)
5. Question
Chapter 4 Page 9
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
o (a)
tio
o (b)
o (c)
ca
o (d)
du
6. Question
ie
sh
ak
.s
w
w
w
Chapter 4 Page 10
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
tio
ca
du
ie
o (a)
sh
o (b)
o (c)
ak
o (d)
.s
7. Question
w
w
w
Chapter 4 Page 11
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
tio
o (a)
o (b)
ca
o (c)
du
o (d)
8. Question
ie
sh
ak
.s
w
w
w
o (a)
o (b)
o (c)
o (d)
Chapter 4 Page 12
www.sakshieducation.com
www.sakshieducation.com
9. Question
m
o (a)
co
o (b)
o (c)
n.
o (d)
tio
10. Question ca
du
ie
sh
ak
.s
w
w
w
Chapter 4 Page 13
www.sakshieducation.com
www.sakshieducation.com
o (a)
o (b)
o (c)
o (d)
m
co
n.
tio
ca
du
ie
sh
ak
.s
w
w
w
Chapter 4 Page 14
www.sakshieducation.com