Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

-, *, / 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

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Describe legally required benefits.

Answered: 1 week ago

Question

Discuss the key ambient conditions and their effects on customers.

Answered: 1 week ago

Question

Be familiar with the integrative servicescape model.

Answered: 1 week ago

Question

Understand the role of corporate design in communications.

Answered: 1 week ago