Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For these exercises, we will expand the grammar and operational semantics for expressions given in the slides to include subtraction and integer division as follows:

image text in transcribedimage text in transcribed

For these exercises, we will expand the grammar and operational semantics for expressions given in the slides to include subtraction and integer division as follows:

-> [2] - [3]

{ .place

.code [2].code || [3].code || .place = [2].place [3].place }

--> [2] / [3]

{ .place

.code [2].code || [3].code || .place = [2].place / [3].place }

  1. Sketch the decorated parse tree for the following assignment statement:

     alpha = 42*(alpha  beta) + 57 

    Since the grammar for expressions is ambiguous, you may assume that the parser somehow enforces the normal order of operations (so that multiplication and division take precedence over addition and subtraction, and all operations are left-associative).

  2. Suppose we enhance the grammar for statements to include if-then-else statements in addition to the grammars and operational semantics for assignment statements and while loops given in the slides. In particular, we now have the following:

    --> | | --> if (id[1] relop id[2]) then [1] else [2]

    The grammar and operational semantics for assignments and while loops are as given in the slides (with the enhancement for expressions to include subtraction and division). Write operational semantics for if-then-else statements. (Hint: in addition to the attribute .code, you may need attributes for labels for the then-part, the else-part and the end of the if-then-else statement. See the operational semantics for loops for a hint on how to do this.)

  3. Now, sketch the decorated parse tree to produce the three-address code for the following statement.

    if (a then d = b a else d = a b

  4. (Challenge) Sketch the decorated parse tree to produce the three-address code for the following loop: while (a
Operational Semantics Homework For these exercises, we will expand the grammar and operational semantics for expressions given in the slides to include subtraction and integer division as follows: .place . code newTemp) [ 3 ] . code [2].place- [2] . code l I place [3].place } = { .place . code newTemp) [2] .code i i [3]code [2].pl ace II .place / [3].place } = 1. Sketch the decorated parse tree for the following assignment statement: alpha 42 (alpha beta)57 Since the grammar for expressions is ambiguous, you may assume that the parser somehow enforces the normal order of operations (so that multiplication and division take precedence over addition and subtraction, and all operations are left-associative). Suppose we enhance the grammar for statements to include if-then-else statements in addition to the grammars and operational semantics for assignment statements and while loops given in the slides. In particular, we now have the following: 2. if (id[1] relop id[2]) then [1] else [2] The grammar and operational semantics for assignments and while loops are as given in the slides (with the enhancement for expressions to include subtraction and division). Write operational semantics for if-then-else statements. (Hint: in addition to the attribute .code, you may need attributes for labels for the then-part, the else-part and the end of the if-then-else statement. See the operational semantics for loops for a hint on how to do this.) 3. Now, sketch the decorated parse tree to produce the three-address code for the following statement if (a

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Students also viewed these Databases questions