Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CMSC 4 3 0 Project 3 The third project involves modifying the attached interpreter so that it interprets programs for the complete language. When the

CMSC 430 Project 3
The third project involves modifying the attached interpreter so that it interprets programs for the
complete language.
When the program is run on the command line, the parameters to the function should be supplied
as command line arguments. For example, for the following function header of a program in the
file text.txt:
function main x: integer, y: integer returns character;
One would execute the program as follows:
$ ./compile < test.txt 10-10
In this case, the parameter x would be initialized to 10 and the parameter y to -10. An example
of a program execution is shown below:
$ ./compile < test.txt 10-10
1// Determines the quadrant of a point on the x-y plane
2
3 function main x: integer, y: integer returns character;
4 begin
5 if x >0 then
6 if y >0 then
7'1';
8 elsif y <0 then
9'4';
10 else
11'Y';
12 endif;
13 elsif x <0 then
14 if y >0 then
15'3';
16 elsif y <0 then
17'2';
18 else
19'Y';
20
endif;
21 else
22 if y <>0 then
23'X';
24 else
25'O';
26 endif;
27 endif;
28 end;
Compiled Successfully
Result =52
After the compilation listing is output, the value of the expression which comprises the body of
the function should be displayed as shown above.
The existing code evaluates some of the arithmetic, relational and logical operators together with
the case statement and decimal integer and real literals only. You are to add the necessary code
to include all of the following:
Hexadecimal integer and character literals that include escape characters
All additional arithmetic operators
All additional relational and logical operators
Both if and fold statements
Functions with multiple variables
Functions with parameters
The fold statement repeatedly applies the specified operation to the list of values, producing one
final value. A left fold associates the operator left to right and a right fold right to left. For
example, the following left fold:
fold left -(3,2,1) endfold;
would be evaluated as ((32)1)=0, but using a right fold:
fold right -(3,2,1) endfold;
It would be evaluated as (3(21))=2. For operations that are associative, the result would be
the same whether it is as folded to the left or right.
This project requires modification to the bison input file, so that it defines the additional the
necessary computations for the above added features. You will need to add functions to the
library of evaluation functions already provided in values.cc. You must also make some
modifications to the functions already provided.
Here is the recommended approach for project 3.
1) Study the skeleton project provided, build it and run it so that you understand how semantic
actions are used to perform the evaluation needed to interpret programs in our language.
2) You should incorporate the new features that are in the skeleton into your version of project 2
and be sure that it builds and runs just as the skeleton did. Then confirm that test cases
test1.txt - test4.txt that were provided as test cases for the skeleton code produce the
correct output. Note that changes are required to both parser.y and scanner.l. You are likely
to get type clash warnings from bison initially. These will be resolved once the entire project is
completed.
3) Make additions as defined by the specification incrementally. Start with adding the code to
evaluate real literals and the hexadecimal integer literals. These changes involve modifications to
scanner.l. There is a predefined C++ function atof that will convert a string containing a real
literal to a double. But you will need to write a function that converts a string containing a
hexadecimal integer to an int. Once you have made these modifications use test5.txt test
them. Shown below is the output that should result when using that test case as input:
$ ./compile < test5.txt
1// Function with Arithmetic Expression using Real Literals
2// and Hexadecimal Integer Literals
3
4 function main returns real;
5 begin
6.83e+2+2.5E-1+(4.3E2+ #aF2)*.01;
7 end;
Compiled Successfully
Result =115.57
4) Next, add the code to scanner.l to evaluate character literals that includes both ordinary
character literals and the escape characters. As with the hexadecimal integer literals, you will
need to write a function to perform this conversion. Once you have made this modification use
test6.txt test it. Shown below is the output that should result when using that test case as
input:
1// Function with Character Literal Escape Characters
2
3 function main returns character;
4 lines: integer is 60;
5 begin
6 when lines <60,'
' : '\f';

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

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

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions