0% found this document useful (0 votes)
14 views

Semanticanalysis

The document discusses several key concepts in compiler design: 1) Abstract syntax trees (ASTs) represent the syntactic structure of source code without non-terminals, used for semantic analysis and code generation. 2) Three-address code is an intermediate representation where instructions have at most three operands, making optimizations easier. 3) Attribute grammars associate attributes with grammar productions to evaluate attributes on an AST for tasks like type checking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Semanticanalysis

The document discusses several key concepts in compiler design: 1) Abstract syntax trees (ASTs) represent the syntactic structure of source code without non-terminals, used for semantic analysis and code generation. 2) Three-address code is an intermediate representation where instructions have at most three operands, making optimizations easier. 3) Attribute grammars associate attributes with grammar productions to evaluate attributes on an AST for tasks like type checking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

www.sakshieducation.

com

Intermediate forms of source Programs – abstract syntax tree


 In computer science, an abstract syntax tree (AST), or just syntax tree, is
a tree representation of theabstract syntactic structure of source code written in
a programming language.
 Each node of the tree denotes a construct occurring in the source code. The syntax is
"abstract" in not representing every detail appearing in the real syntax. For instance,
grouping parentheses are implicit in the tree structure, and a syntactic construct like an if-
condition-then expression may be denoted by means of a single node with three branches.

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

number 9 as the right child.


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

is – Non-terminals are necessary for parsing, not for meaning.


 An Abstract Syntax Tree is a simplifed version of a parse tree – basically a parse tree
ak

without non-terminals.
.s

Polish Notation and Three Address Code:


w

Polish Notation (PN):


w

 Polish notation (PN), also known as normal Polish


notation (NPN), Łukasiewicz notation, Warsaw notation, Polish prefix notation or
w

simplyprefix notation, is a form of notation for logic, arithmetic, and algebra.


 Its distinguishing feature is that it places operators to the left of their operands. If
the arity of the operators is fixed, the result is a syntax lacking parentheses or other
brackets that can still be parsed without ambiguity.
 The Polishlogician Jan Łukasiewicz invented this notation in 1924 in order to
simplify sentential logic.

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

Since three-address code is used as an intermediate language within compilers, the


operands will most likely not be concrete memory addresses or processor registers, but
sh

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

address code is typically generated by the compiler. A refinement of three-address code


is static single assignment form (SSA).
w

 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

L1: if t1 >= 10 goto L2 ; conditional jump


sh

t2 := t1 * t1 ; square of i
t3 := t1 * 4 ; word-align address
ak

t4 := b + t3 ; address to store i*i


*t4 := t2 ; store through pointer
.s

t1 := t1 + 1 ; increase i
w

goto L1 ; repeat loop


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

the below image.


ie
sh
ak
.s
w

Syntax Directed Translation:


 Syntax-directed translation refers to a method of compiler implementation where the
w

source language translation is completely driven by the parser.


w

 A common method of Syntax-directed translation is translating a string into a sequence


of actions by attaching one such action to each rule of a grammar. Thus, parsing a string
of the grammar produces a sequence of rule applications. SDT provides a simple way to
attach semantics to any such syntax.
 The Principle of Syntax Directed Translation states that the meaning of an input sentence
is related to its syntactic structure, i.e., to its Parse-Tree.

Chapter 4 Page 5
www.sakshieducation.com
www.sakshieducation.com

 By Syntax Directed Translations we indicate those formalisms for specifying translations


for programming language constructs guided by context-free grammars.
 We associate Attributes to the grammar symbols representing the language constructs.
Values for attributes are computed by Semantic Rules associated with grammar
productions.
 Evaluation of Semantic Rules may:
o Generate Code
o Insert information into the Symbol Table

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

Previous GATE Questions:


1. Question

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

You might also like