Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

A.

KRISHNA CHAITHANYA REDDY


RA2111003011246

UNIT 4 SUMMARY(PART 2):


Inherited A ributes

• Inherited A ributes are useful for expressing the dependence of a


on the context in which it appears.
• It is always possible to rewrite a syntax directed defini on to use only
synthesized a ributes, but it is o en more natural to use both synthesized and
inherited a ributes.
• Evalua on Order. Inherited a ributes cannot be evaluated by a simple
PreOrder traversal of the parse-tree:
– Unlike synthesized a ributes, the order in which the inherited a ributes
of the children are computed is important!!! Indeed:

∗ Inherited a ributes of the children can depend from both le and right
siblings!

Boolean Expression

• The transla on of if-else-statements and while-statements is ed to


the transla on of boolean expressions.
•In programming languages, boolean expressions are used to
• 1. Alter the flow of control.
• 2. Compute logical values.

1. Alter the flow of control.


▪ Boolean expressions are used as condi onal expressions in statements that
alter the flow of control.
▪ The value of such boolean expressions is implicit in a posi on reached in a
program.

For example, in if (E) S, the expression E must be true if statement S is reached.


•2. Compute logical values.
▪ A boolean expression can represent true or false as values.
▪ Boolean expressions can be evaluated in analogy to arithme c expressions
using three-address instruc ons with logical operators.
• Boolean expressions are composed of the boolean operators
• && - AND, I I - OR, and ! - NOT
• applied to elements that are boolean variables or rela onal expressions.
• Rela onal expressions are of the form El re1 E2, where El and E2 are arithme c
expressions
•Grammar to generate Boolean expression:
B → B1 or B2 | B → B1 and B2 | B → not B1 | B → (B1)|B → E1 relop E2 |
B → false| B → true

Backpatching

• A key problem when genera ng code for boolean expressions and


flow-of-control statements is that of matching a jump instruc on
with the target of the jump.
• For example, the transla on of the boolean expression B in if ( B ) S
contains a jump, for when B is false, to the instruc on following the
code for S.
• In a one-pass transla on, B must be translated before S is examined.
• What then is the target of the goto that jumps over the code for S?
• For implemen ng Backpatching
• Instruc ons are generated into an instruc on array
• Labels act as indices to this array.
• To manipulate list of labels , three func ons are used:
• makelist(i)

• merge(p1,p2) and
• backpatch(p,i)
• makelist(i) : creates a new list containing only i,
an index into the array of instruc ons and
returns pointer to the list it has made.
• merge(i,j) – concatenates the lists pointed to by
i and j ,and returns a pointer to the
concatenated list.
• backpatch(p,i) – inserts i as the target label for
each of the statements on the list pointed to by
p.

CASE Statement

The CASE statement chooses from a sequence of conditions, and executes a


corresponding statement. The CASE statement evaluates a single expression and
compares it against several potential values, or evaluates multiple Boolean
expressions and chooses the first one that is TRUE.

Syntax

searched_case_statement ::=
[ <<label_name>> ]
CASE { WHEN boolean_expression THEN {statement;} ... }...
[ ELSE {statement;}... ]
END CASE [ label_name ];

simple_case_statement ::=

[ <<label_name>> ]
CASE case_operand
{ WHEN when_operand THEN {statement;} ... }...
[ ELSE {statement;}... ]
END CASE [ label_name ];

Usage Notes

The WHEN clauses are executed in order.

Each WHEN clause is executed only once.

After a matching WHEN clause is found, subsequent WHEN clauses are not executed.

The statements in a WHEN clause can modify the database and call non-
deterministic functions.

Assignment Statements

Produc on rule Seman c ac ons

S → id :=E {p = look_up(id.name);
If p ≠ nil then
Emit (p = E.place)
Else
Error;
}

E → E1 + E2 {E.place = newtemp();
Emit (E.place = E1.place '+' E2.place)
}

E → E1 * E2 {E.place = newtemp();
Emit (E.place = E1.place '*' E2.place)
}

E → (E1) {E.place = E1.place}


E → id {p = look_up(id.name);
If p ≠ nil then
Emit (p = E.place)
Else
Error;
}

In the syntax directed translation, assignment statement is mainly deals with


expressions. The expression can be of type real, integer, array and records.

Consider the grammar

1. S → id := E
2. E → E1 + E2
3. E → E1 * E2
4. E → (E1)
5. E → id

The translation scheme of above grammar is given below:

You might also like