Question: -, *, / 1. [3 pts) You are given the task of bootstrapping a compiler for a new programming language L. Assume that you have

 -, *, / 1. [3 pts) You are given the task

-, *, / 1. [3 pts) You are given the task of bootstrapping a compiler for a new programming language L. Assume that you have already implemented a minimal core of the language, Lo, which has the following features: Integer literals, variables, and arithmetic ops, e.g. 1, 25, x, y, +, Assignment of a single-operation expression to a variable, e.g. x = -5, y = x + 1 Unconditional jump to a label, e.g. goto L Conditional jump with a single variable as condition, e.g. if x goto L (if x # 0, jump to L) Print a single variable or integer, e.g. print x, print 4 Here is a factorial program written in Lo: . n = 10 X = 1 L1: x = x * n n = n - 1 if n goto L1 print x . . . The next level of the language, Li, has the following new features: General arithmetic expressions (parentheses allowed), e.g. x (y - Z) * 2 General assignment and print statement (arbitrary expr allowed): x = a + b * c, print(x + y) General if statement (arbitrary expr allowed as cond): if (E) si else S2 General while statement: while (E) S (if E's value # 0, execute S and continue, otherwise break) Compound statement: {S1; 52; ...} These new features need to be implemented in Lo. As an exercise, show how the following L code fragments can be implemented in Lo by giving an equivalent code sequence in Lo for each of them. (Hint: You may introduce additional variables if needed.) (a) print(x + y - z * 2) (b) if (x - y) z = 2 else z = 5; print(z * 3) (C) while (k) {x = x * k; k = k - 1}; print(x) -, *, / 1. [3 pts) You are given the task of bootstrapping a compiler for a new programming language L. Assume that you have already implemented a minimal core of the language, Lo, which has the following features: Integer literals, variables, and arithmetic ops, e.g. 1, 25, x, y, +, Assignment of a single-operation expression to a variable, e.g. x = -5, y = x + 1 Unconditional jump to a label, e.g. goto L Conditional jump with a single variable as condition, e.g. if x goto L (if x # 0, jump to L) Print a single variable or integer, e.g. print x, print 4 Here is a factorial program written in Lo: . n = 10 X = 1 L1: x = x * n n = n - 1 if n goto L1 print x . . . The next level of the language, Li, has the following new features: General arithmetic expressions (parentheses allowed), e.g. x (y - Z) * 2 General assignment and print statement (arbitrary expr allowed): x = a + b * c, print(x + y) General if statement (arbitrary expr allowed as cond): if (E) si else S2 General while statement: while (E) S (if E's value # 0, execute S and continue, otherwise break) Compound statement: {S1; 52; ...} These new features need to be implemented in Lo. As an exercise, show how the following L code fragments can be implemented in Lo by giving an equivalent code sequence in Lo for each of them. (Hint: You may introduce additional variables if needed.) (a) print(x + y - z * 2) (b) if (x - y) z = 2 else z = 5; print(z * 3) (C) while (k) {x = x * k; k = k - 1}; print(x)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!