ACD Unit - III Notes
ACD Unit - III Notes
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:
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 Annotated parse tree for the expression 23*4+5 is depicted in Fig 3.2.
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.
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).
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.
on
|\alpha | = 1.
For example:
S --> AB
A --> a
B --> b
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.
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
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:
22