0% found this document useful (0 votes)
63 views22 pages

ACD Unit - III Notes

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)
63 views22 pages

ACD Unit - III Notes

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/ 22

SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

UNIT-III Compiler Design – 20AIT02


SEMANTICS, RUN TIME STORAGE MANAGEMENT

1
UNIT-III
SEMANTICS, RUN TIME STORAGE MANAGEMENT
Syntax directed translation, S-attributed and L-attributed grammars, and Chomsky hierarchy of
Languages and recognizers, Type checking, type conversions, equivalence of type expressions,
Overloading of functions and operations. Storage organization, storage allocation strategies, scope
Access to non-local names, parameter passing, and language facilities for dynamics storage
Allocation. Syntax Directed Translation

Semantic Analysis computes additional information related to the meaning of the program once
the syntactic structure is known. Semantic analysis involves adding information to the symbol
table and performing type checking. It needs both representation and implementation
mechanism.
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.
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:
 Generate Code;
 Insert information into the Symbol Table;
 Perform Semantic Check;
 Issue error messages;
There are two notations for attaching semantic rules:
1. Syntax Directed Definitions. High-level specification hiding many
implementation details (also called Attribute Grammars).
2.Translation Schemes. More implementation oriented: Indicate the order in which semantic
rules are to be evaluated
Syntax Directed Definitions are a generalization of context-free grammars in which:
1. Grammar symbols have an associated set of Attributes;
2. Productions are associated with Semantic Rules for computing the values of attributes.
Annotated Parse-Trees where each node of the tree is a record with a field for each attribute (e.g.,
X.a indicates the attribute a of the grammar symbol X).
The value of an attribute of a grammar symbol at a given parse-tree node is defined by a
semantic rule associated with the production used at that node.

2
There are two kinds of attributes:
1. Synthesized Attributes: They are computed from the values of the attributes of
the children nodes.
2. Inherited Attributes: They are computed from the values of the attributes of
both the siblings and the parent nodes

Example: Let us consider the Grammar for arithmetic expressions (DESK CALCULATOR)
The Syntax Directed Definition associates to each non terminal a synthesized attribute called val.

Definition: An S-Attributed Definition is a Syntax Directed Definition that uses only synthesized
attributes.
Evaluation Order: Semantic rules in a S-Attributed Definition can be evaluated by a bottom-up,
or Post Order, traversal of the parse-tree.
The annotated parse-tree for the input 3*5+4n is:

Fig 3.1 Annotated Parse Tree

3
Implementation of SDT for Desk Calculator:
Use an extra fields in the parser stack entries corresponding to the grammar symbols. These
fields hold the values of the corresponding translations.

The grammar and corresponding Semantic action is shown below:

The Annotated parse tree for the expression 23*4+5 is depicted in Fig 3.2.

Fig 3.1 Annotated Parse Tree for 23*4+5$

4
The method of implementing the desk calculator is given using the program fragment represents
the stack implementation to evaluate a given expression.

The sequences of moves to implement the evaluation of an expression are shown in Fig 3.3.

Fig 3.3 Sequence of moves

5
S – attributed and L – attributed SDTs in Syntax directed translation:

Types of attributes :
Attributes may be of two types – Synthesized or Inherited.
1. Synthesized attributes –
A Synthesized attribute is an attribute of the non-terminal on the left-hand side of a
production. Synthesized attributes represent information that is being passed up the parse
tree. The attribute can take value only from its children (Variables in the RHS of the
production).
For eg. let’s say A -> BC is a production of a grammar, and A’s attribute is
dependent on B’s attributes or C’s attributes then it will be synthesized attribute.
2. Inherited attributes –
An attribute of a nonterminal on the right-hand side of a production is called an
inherited attribute. The attribute can take value either from its parent or from its siblings
(variables in the LHS or RHS of the production).

Chomsky Hierarchy Of Languages :

According to Chomsky hierarchy, grammar is divided into 4 types as follows:


1. Type 0 is known as unrestricted grammar.
2. Type 1 is known as context-sensitive grammar.
3. Type 2 is known as a context-free grammar.
4. Type 3 Regular Grammar.

Type 0: Unrestricted Grammar:

Type-0 grammars include all formal grammar. Type 0 grammar languages are recognized by
turing machine. These languages are also known as the Recursively Enumerable languages.

6
Type 1: Context-Sensitive Grammar
Type-1 grammars generate context-sensitive languages. The language generated by the
grammar is recognized by the Linear Bound Automata
In Type 1
 First of all Type 1 grammar should be Type 0.

Type 2: Context-Free Grammar: Type-2 grammars generate context-free languages. The


language generated by the grammar is recognized by a Pushdown automata. In Type 2:
 First of all, it should be Type 1.
 The left-hand side of production can have only one variable and there is no restriction

on
|\alpha | = 1.
For example:
S --> AB
A --> a
B --> b

Type 3: Regular Grammar: Type-3 grammars generate regular languages. These


languages are exactly all languages that can be accepted by a finite-state automaton. Type 3
is the most restricted form of grammar.
Type 3 should be in the given form only :
V --> VT / T (left-regular grammar)
(or)
V --> TV /T (right-regular grammar)
For example:
S --> a
The above form is called strictly regular grammar.
There is another form of regular grammar called extended regular grammar. In this form:
V --> VT* / T*. (extended left-regular grammar)
(or)
V --> T*V /T* (extended right-regular grammar)
For example :
S --> ab.

7
TYPE CHECKING:

8
9
10
11
Function Overloading:

If any class have multiple functions with same names but different parameters then they are said
to be overloaded.

Function overloading allows you to use the same name for different functions, to perform, either
same or different functions in the same class.

Ways to overload a function:


1. By changing number of Arguments.
2. By having different types of argument.
By Changing Number of Arguments:
int sum(int a,int b)
{
return a + b;
}

int sum(int a, int b, int c)


{
return a + b + c;
}

By having different types of argument:


int sum(int a,int b)
{
return a+b;
}

float sum(double x, int y)


{
return x+y;
}

Operator Overloading 12
Operator overloading is a compile-time polymorphism.
As the name indicated the operator is overloaded to provide the special meaning to the user-
defined data type.
For example: Overloading the Unary operator ++
#include <iostream>
using namespace std;
class Een
{
private:
int n;
public:
Een(): n(4){}
void operator ++() {
n = n+2;
}
void Print() {
cout<<n;
}
};
int main()
{
Een E;
++E; // calling of a function “void operator ++()”
E.Print();
return 0;
}

13
14
15
16
17
18
19
20
PARAMETER PASSING

There are two types of parameters:

1. Formal parameters
2. Actual parameters
Based on these parameters there are various parameter passing methods.
The most common methods are:

1. Call by value:
1. This is the simplest method of parameter passing.
2. The actual parameters are evaluated and their r-values are passed to called procedure.
3. The operations on formal parameters do not changes the values of actual parameter.
Example: Languages like C, C++ use actual parameter passing method.

2. Call by reference:
1. This method is also called as call by address or call by location.
2. The L-value, the address of actual parameter is passed to the called routines activation record.
3. The value of actual parameters can be changed.
4. The actual parameter should have an L-value.
Example: Reference parameter in C++,PASCAL’S var parameters.

3. Copy restore:
1. This method is a hybrid between call by value and call by reference.
2. This is also known as copy-in-copy-out or values result.
3. The calling procedure calculates the value of actual parameters and it is then copied to activation record for
the called procedure.
4. During execution of called procedure, the actual parameters value is not affected.
5. If the actual parameters have L-value then at return the value of formal parameter is copied to actual
parameter.
Example: In Ada this parameter passing method is used.

4. Call by name:
1. This is less popular method of parameter passing.
2. Procedure is treated like macro.
3. The procedure body is substituted for call in caller with actual parameters substituted for formals.
4. The actual parameters can be surrounded by parenthesis to preserve their integrity.
5. The locals name of called procedure and names of calling procedure are distinct.
*(For Example Program Refer Class Notes)

21
Language Facilities for dynamic storage allocation:

● Explicit and Implicit allocation of memory to variables


○ Most languages support dynamic allocation of memory.
○ Pascal supports new(p) and dispose(p) for pointer types.
○ C provides malloc() and free() in the standard library.
○ C++ provides the new and free operators.
○ These are all examples of EXPLICIT allocation.
○ Other languages like Python and Lisp have IMPLICIT allocation.
● Garbage – Finding variables that are not referred by the program any more
○ In languages with explicit deallocation, the programmer must be careful to free every dynamically
allocated variable, or GARBAGE will accumulate.
○ Garbage is dynamically allocated memory that is no longer accessible because no pointers are
pointing to it.
○ In some languages with implicit deallocation, GARBAGE COLLECTION is occasionally necessary.
○ Other languages with implicit deallocation carefully track references to allocated memory and
automatically free memory when nobody refers to it any longer
Dynamic storage allocation techniques:
● Explicit allocation of fixed sized blocks of memory with contiguous block of memory available for
usage by the program
● Explicit allocation of variable sized blocks of memory where the storage can be fragmented
● Implicit deallocation of blocks of memory that are not used any more by using reference counts and
Marking techniques
● we assume the heap is an initially empty block of memory.
● As memory is allocated and deallocated, fragmentation occurs.
● For allocation, we must find a HOLE large enough to hold the requested memory.
● For deallocation, we must merge adjacent holes to prevent further fragmentation.

22

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy