Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following grammar applies + and * operators to integer and real constants. T

ID: 3864512 • Letter: T

Question

 The following grammar applies + and * operators to integer and real  constants. The result of adding two integers is type integer, otherwise the  result is type real. A number (T) with a decimal point is real, otherwise is  integer.  E-> E+T | T T-> T*F | F F-> num.num | num  Write a syntax directed definition for this grammar to determine the type of  each subexpression; if an operation involves mixed integer and real values, convert integer values to real.   Write semantic actions to output 3-addres code. Assume the following operators:  INT2RL dest src         // integer to real ADDI dest src1 src2     // integer sum ADDR dest src1 src2     // real sum MULI dest src1 src2     // integer multiply MULR dest src1 src2     // real multiply  (If you believe you need any additional 3-address operators, you may add them to the above list). Use temporary variables for intermediate results). 

Explanation / Answer

Syntax Directed Definition :

Each variable here has one attribute val.

ADDI E E T

(meaning : E.val = E.val + T.val)

MULI T T F

(meaning : T.val = T.val * F.val)

ADDR E E T

(Meaning : E'.val = E'.val + T'.val)

MULR T T F'

(meaning : T'.val = T'.val * F'.val)

INT2RL F F ;

ADDR T' T' F

(meaning : T'.val = T'.val + F.val)

INT2RL F F;

T'val = F.val

Here productions are divided into two parts( E and E') :

1) First part will only perform operation on integers. This part involves Variables E, T, F.

Productions invovled with integer part :

2) Second part performs operations on real number as well as combination of real numbers and integers.

In this E', T', F' and F variables are involved. Prodcutions involved are :

Here, At productions T'->T'*F and T'->F will convert the integer numbers into real numbers as part of semantic rule.

If you have any doubts regarding solution, you can ask in comment section.

Production Semantic rule X -> E X.val = E.val X -> E' X.val = E'.val E -> E + T

ADDI E E T

(meaning : E.val = E.val + T.val)

E -> T E.val = T.val T -> T * F

MULI T T F

(meaning : T.val = T.val * F.val)

T -> F T.val = F.val F -> num F.val = num E' -> E' + T'

ADDR E E T

(Meaning : E'.val = E'.val + T'.val)

E' -> T' E'.val = T'.val T' -> T' * F'

MULR T T F'

(meaning : T'.val = T'.val * F'.val)

T' -> F' T'.val = F'.val T' -> T' * F

INT2RL F F ;

ADDR T' T' F

(meaning : T'.val = T'.val + F.val)

T' -> F

INT2RL F F;

T'val = F.val

F' -> num.num F'.val = num.num
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote