430 CMSC

ebote
  • 2 years ago
  • 80
files (12)

Project3Requirements1.pdf

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 ((3 – 2) – 1) = 0, but using a right fold:

fold right - (3, 2, 1) endfold;

It would be evaluated as (3 – (2 – 1)) = 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.

You are to submit two files.

1. The first is a .zip file that contains all the source code for the project. The .zip file

should contain the flex input file, which should be a .l file, the bison file, which should

be a .y file, all .cc and .h files and a makefile that builds the project.

2. The second is a Word document (PDF or RTF is also acceptable) that contains the

documentation for the project, which should include the following:

a. A discussion of how you approached the project

b. A test plan that includes test cases that you have created indicating what aspects

of the program each one is testing and a screen shot of your compiler run on that

test case

c. A discussion of lessons learned from the project and any improvements that could

be made

Project4TestData1.zip

semantic1.txt

// Variable Initialization Mismatch function main returns integer; value: integer is 'A'; begin 1; end;

semantic10.txt

// List Subscript is not Integer function main returns integer; aList: list of integer is (1, 2, 3); begin aList(1.5); end;

semantic11.txt

-- Mixing Numeric and Character Types with Relational Operator function main returns integer; begin if 'b' < 'c' then 1; elsif 'b' < 1 then 2; else 3; endif; end;

semantic12.txt

// Using Character Literal with Exponentiation Operator // and Negation Operator function main returns integer; c: character is ~'c'; begin 5 ^ 'P'; end;

semantic13.txt

// Mixing Real Literals with the Remainder Operator function main returns integer; begin 4 % 4.8; end;

semantic14.txt

-- If Elsif Else Mismatch function main returns integer; begin if 9 < 10 then 1; elsif 8 = 1 then 2; else 3.7; endif; end;

semantic15.txt

// Folding a nonnumeric List function main returns integer; begin fold left + ('a', 'b', 'c') endfold; end;

semantic16.txt

-- Narrowing Variable Initialization function main returns integer; b: integer is 5 * 2.5; begin b + 1; end;

semantic17.txt

-- Narrowing Function Return function main returns integer; b: integer is 6 * 2; begin if 8 < 0 then b + 3.0; else b * 4.6; endif; end;

semantic18.txt

-- Duplicate Scalar and List Variables function main returns integer; scalar: integer is 4 * 2; scalar: character is 'b'; a_list: list of integer is (4, 2); a_list: list of real is (2.3, 4.4); begin 1; end;

semantic19.txt

// Multiple Semantic Errors function main returns integer; value: integer is 4.5; numbers: list of real is (1, 2, 3); one: integer is '1'; begin if value > 0 then fold left + ('a', 'b') endfold; elsif name = 'N' then fold right * (1, 2.5) endfold; else when value < 10, 1 : 1.5; endif; end;

semantic2.txt

// When Types Mismatch function main returns integer; begin when 2 < 1, 1 : 'a'; end;

semantic3.txt

// Non Integer Switch Expression function main returns integer; b: character is 'A'; begin switch b is case 1 => 2; case 2 => 4; others => 6; endswitch; end;

semantic4.txt

// Case Types Mismatch function main returns integer; b: character is 'b'; begin switch 1 is case 1 => 2; case 2 => b; others => 6; endswitch; end;

semantic5.txt

// Using Character Variable with Arithmetic Operator function main returns integer; b: character is 'b'; begin b + 10; end;

semantic6.txt

// Undeclared Scalar Variable function main returns integer; begin 2 * b + 3; end;

semantic7.txt

// Undeclared List Variable function main returns integer; begin primes(1) + 1; end;

semantic8.txt

// List with Elements of Different Types function main returns integer; aList: list of integer is (1, 2, 3.5); begin aList(1); end;

semantic9.txt

// List Type Does Not Match Element Types function main returns character; aList: list of character is (1, 2, 3); begin aList(1); end;

valid1.txt

-- Program with a Real Variable function main returns real; a: real is 4.5; begin a; end;

valid2.txt

-- Program with a Hexadecimal Literals function main returns integer; a: integer is #A; begin a + #a; end;

valid3.txt

-- Program with a Real Variable function main returns real; a: real is 4 + 4.5; begin a; end;

Project2TestData1.zip

syntax1.txt

// Missing Operator in Expression function main returns integer; begin 8 + 2 9 * 3; end;

syntax2.txt

// Error in Function Header, Missing Colon function main a integer returns integer; b: integer is 3 * 2; begin if a <= 0 then b + 3; else b * 4; endif; end;

syntax3.txt

// Error in Variable Declaration function main a: integer returns integer; b integer is if a > 5 then a * 3; else 2 + a; endif; c: real is 3.5; begin if a <= 0 then b + 3; else b * 4; endif; end;

syntax4.txt

// Multiple Errors, Error in Case Clause and Missing Others Clause function main a: integer returns integer; begin switch a is case 1 => a 2; case 2 => 5; endswitch; end;

syntax5.txt

-- Multiple Errors function main a integer returns real; b: integer is * 2; c: real is 6.0; begin if a > c then b + / .4; else switch b is case => 2; case 2 => c; endswitch; endif; end;

test1.txt

// Function with Arithmetic Expression function main returns integer; begin 7 + 2 * (5 + 4); end;

test10.txt

// Two Parameter Declarations function main a: integer, b: real returns real; c: real is .7; begin a + b * c; end;

test11.txt

// Arithmetic Operators function main returns integer; begin 9 + ~2 - (5 - 1) / 2 % 3 * 3 ^ 1 ^ 2; end;

test12.txt

// Relational and Logical Operators function main returns integer; begin when 5 > 8 & 3 = 3 | 9 < 1 & !(3 <> 7) | 6 <= 7 & 3 >= 9, 1 : 0; end;

test13.txt

// Comprehensive Test with Nested If function main a: integer, b: character, c: real returns real; d: integer is #8e; e: real is 3.75; f: character is 'A'; g: list of integer is (1, 3, 5); begin if ~a > 5 & a < 1 & !(c = 5.8 | c <> .7E4) then if c >= 7.5E-2 & c <= 5.2 then when a >= d, a + 2 - 7.9E+2 / 9 * 4 : 8.9; elsif g(1) = a ^ 2 % 3 then a % 2 - 5 / c; else fold left + (1, 2, 3) endfold; endif; else fold right - g endfold; endif; end;

test14.txt

// Comprehensive Test with Nested Switch function main a: integer, b: real returns real; c: integer is 8; d: real is .7E2; begin switch a is case 1 => a * 2 / d ^ 2; case 2 => a + 5.3E+2 - b; case 3 => switch d is case 1 => a % 2; others => 9.1E-1; endswitch; case 4 => a / 2 - c; others => a + 4.7 * b; endswitch; end;

test2.txt

// Function with When Statement function main returns integer; begin when 3 < 2 & 6 < 7, 7 * 2 : 7 + 2; end;

test3.txt

// Function with a Switch Statement function main returns character; a: integer is 2 * 2 + 1; begin switch a is case 1 => 'a'; case 2 => 'b'; others => 'c'; endswitch; end;

test4.txt

// Function with a List Variable function main returns integer; primes: list of integer is (2, 3, 5, 7); begin primes(1) + primes(2); end;

test5.txt

// Function with a Real and Character Variables and Literals function main returns character; a: real is 7.8e-1; begin when a < .45, '\n' : 'A'; end;

test6.txt

// Function with an If Statemnt function main a: integer returns integer; begin if a < 10 then 1; elsif a < 20 then 2; elsif a < 30 then 3; else 4; endif; end;

test7.txt

// Left and Right Fold Statement function main returns integer; a: list of integer is (1, 2, 3); begin switch a(1) is case 1 => fold right - (2,3, 4) endfold; case 2 => fold left + a endfold; others => 0; endswitch; end;

test8.txt

// Multiple Integer Variable Initializations function main returns integer; b: integer is 5 + 1 - 4; c: integer is 2 + 3; begin b + 1 - c; end;

test9.txt

// Single Parameter Declaration function main a: integer returns integer; begin a + 1; end;

Project3TestData1.zip

test1.txt

// Function with Arithmetic Expression function main returns integer; begin 7 + 2 * (5 + 4); end;

test10.txt

-- Multiple Variable Initializations function main returns character; b: integer is 5 + 1 - 4; c: real is 2 + 3.7; d: character is 'A'; begin if b + 1 - c > 0 then d; else '\n'; endif; end;

test11.txt

// Single Parameter Declaration function main a: real returns real; begin a + 1.5; end;

test12.txt

// Two parameter declarations function main a: integer, b: real returns real; begin if a < #A then b + 1; else b - 1; endif; end;

test13.txt

// Test Right Fold function main returns integer; values: list of integer is (3, 2, 1); begin fold right - values endfold; end;

test14.txt

// Test Left Fold function main returns integer; begin fold left - (3, 2, 1) endfold; end;

test15.txt

// Test that Includes All Statements function main a: integer, b: real, c: character returns real; d: integer is when a > 0, #A: #A0; e: list of integer is (3, 2, 1); f: integer is switch a is case 1 => fold left - e endfold; case 2 => fold right - e endfold; others => 0; endswitch; g: integer is if c = 'A' & b > 0 then a * 2; elsif c = 'B' | b < 0 then (a ^ 2) * 10; else 0; endif; begin f + (d - 1) % g; end;

test16.txt

// Nested if statements function main x: integer, y: integer returns character; begin if x > 0 then if y > 0 then '1'; elsif y < 0 then '4'; else 'Y'; endif; elsif x < 0 then if y > 0 then '3'; elsif y < 0 then '2'; else 'Y'; endif; else if y <> 0 then 'X'; else 'O'; endif; endif; end;

test2.txt

// Function with When Statement function main returns integer; begin when 3 < 2 & 6 < 7, 7 * 2 : 7 + 2; end;

test3.txt

// Function with a Switch Statement function main returns character; a: integer is 2 * 2 + 1; begin switch a is case 1 => 'a'; case 2 => 'b'; others => 'c'; endswitch; end;

test4.txt

// Function with a List Variable function main returns integer; primes: list of integer is (2, 3, 5, 7); begin primes(1) + primes(2); end;

test5.txt

// Function with Arithmetic Expression using Real Literals // and Hexadecimal Integer Literals function main returns real; begin .83e+2 + 2.5E-1 + (4.3E2 + #aF2) * .01; end;

test6.txt

// Function with Character Literal Escape Characters function main returns character; lines: integer is 60; begin when lines < 60, '\n' : '\f'; end;

test7.txt

// Tests All Arithmetic Operators function main returns integer; begin 9 + 2 - (5 + ~1) / 2 % 3 * 3 ^ 1 ^ 2; end;

test8.txt

// Test Logical and Relational Operators function main returns integer; begin when !(6 <> 7) & 5 + 4 >= 9 | 6 = 5, 6 + 9 * 3 : 8 - 2 ^ 3; end;

test9.txt

// Function with an If Statement function main returns integer; a: integer is 28; begin if a < 10 then 1; elsif a < 20 then 2; elsif a < 30 then 3; else 4; endif; end;

Project4Requirements1.pdf
This file is too large to display.View in new window
Project2Requirements1.pdf
This file is too large to display.View in new window
Project4SkeletonCode1.zip
This file is too large to display.View in new window
Project2SkeletonCode1.zip
This file is too large to display.View in new window
Project4Approach1.pdf
This file is too large to display.View in new window
Project2Approach1.pdf
This file is too large to display.View in new window
Project3SkeletonCode.zip
This file is too large to display.View in new window
Project3Approach2.pdf
This file is too large to display.View in new window