Run Time System and Intermediate Code Generation Eng 57
Run Time System and Intermediate Code Generation Eng 57
co
1 | Page
www.gradeup.co
Run Time System and Intermediate Code Generation
Content :
Go through the source code once and creates equivalent binary program. The assembler
substitute all of the symbolic instruction with machine code in one pass.
• Advantage: Every source statement needs to be processed once.
• Disadvantages: We cannot use any forward reference in our program. Forward reference
means a reference to an instruction which has not yet been encountered by the
assembler. In order to handle forward reference, the program needs to be scanned
twice. In other words, a two pass assembler is needed.
2 | Page
www.gradeup.co
• Creates a symbol table
.Solves forward references
• Converts the code to the machine code
Role of semantic analyzer: The job of semantic analyzer is to ensure that a program
confirms to certain extra-syntactic rules and is, therefore, correct. This analysis may be
carried out by examining the text and is, therefore, static in nature. It analysis properties
that cannot be captured by context free grammars.
Syntax Directed Definitions: It is an augmented context free grammar in which each
grammar symbol, terminal or non-terminal, has an associated set of attributes. An attribute
can be represent anything depending on the type of analysis we are doing. All tokens
have at least one attribute which is the associated lexeme.
It is a generalization of a context free grammar where each production A α is
associated with a set of semantic rules of the form
X : f(a1, a2,…a3)
Where f is a function and x is an attribute. There are two possibilities.
1. X is a synthesized attribute of A and a1,a2…,a k are attributes belonging to the grammar
symbols of α.
2. X is an inherited attribute of one of the grammar symbols in α and a1,a2…,a k are
attrubtes belonging to A or α.
3 | Page
www.gradeup.co
Example: let us annotate the parse tree for the S – attributed definition for the input 4-
6/3+5
4 | Page
www.gradeup.co
The values of the new synthesized attributes are computed from the attributes appearing
on the stack for the grammar symbols on the right hand side of the reducing production
whenever a reduction is made. If there is a semantic rule.
L- attributed definitions S –attributed schemes do not permit inherited attributes and are
too restrictive. Therefore, we know introduce another class of syntax directed definitions
known as L-attributed definitions. The attributes of this class can always be evaluated
in depth-first order.
1. The attributes of the symbols X1, 1≤ i ≤ j – 1, i.e. the attributes of the symbols on the
left of X1 in the production.
2. The inherited attributes of A
According to this definition every S-attributed definition is also a L-attributed definition.
5 | Page
www.gradeup.co
Translation Schemes: it is a refinement of a syntax directed definition in which semantic
actions enclosed between braces{} are inserted within the right sides of production to
specify the exact point in time when the actions are to be executed.
It generates an output for each sentence x fenerated by the underlying grammar by
executing the actions in the order they appear during a depth-first traversal of a parse
tree for x. For example in the translation scheme A X 1 ….X1 {action} XI+1….xn ,
action will be performed after the sub tree for X1 is traversed but before the beginning
of XI+1
Example: Let us consider the grammar to generate simple expressions in infix form
and a translator which converts it into postfix form.
We can remove left recursion from this grammar. The transformed translation scheme is
as follows:
6 | Page
www.gradeup.co
We can show the parse tree for the input 5+4+6 with each semantic action attached as
the appropriate child of the node corresponding to the left side of their production. When
perform in depth-first order, the action print the output 5 4 + 6 +
Type Analyses and type checking: semantic analyzer checks that the source program
is semantically correct. Static type checking which is an important part of semantic
analysis ensures that certain type of checks like uniqueness check, flow of control checks
etc. must be performed and if there is any error, it must be reported. This type checker
is required to detect and report error if an operator is applied to an incompatible operand.
7 | Page
www.gradeup.co
Type system: It is an component of a language which keeps track of the types of
variables and in general, of the types of all expressions in a program.
There are two kind of execution errors-trapped errors and un-trapped errors
1. Trapped errors are the ones that cause the computation to stop immediately. For example
division by zero and accessing an illegal address.
2. Un-trapped errors are the ones that go un-noticed for a while and later cause arbitrary
behavior. For example, accessing data past the end of an array in absence of run time
bounds checks.
8 | Page
www.gradeup.co
9 | Page
www.gradeup.co
10 | P a g e