CMSC 430

ebote
  • 2 years ago
  • 70
files (17)

Project1TestData.zip

test1.txt

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

test2.txt

// Function with a lexical error function main returns integer; begin 7 * 2 $ (2 + 4); end;

test3.txt

// Punctuation symbols ,;() => // Identifier name name123 // Literals 123 'a' // Logical operator & // Relational operator < // Arithmetic operators + * // Reserved words begin case character end endswitch function is integer list of returns switch when

test4.txt

// Function with All Reserved Words function main returns character; number: real is when 2 < 3, 0 : 1; values: list of integer is (4, 5, 6); begin if number < 6.3 then fold left + (1, 2, 3) endfold; elsif 6 < 7 then fold right + values endfold; else switch a is case 1 => number + 2; case 2 => number * 3; others => number; endswitch; endif; end;

test5.txt

// Program Containing the New Operators function main b: integer, c: integer returns integer; a: integer is 3; begin if (a < 2) | (a > 0) & (~b <> 0) then 7 - 2 / (9 % 4); else if b >= 2 | b <= 6 & !(c = 1) then 7 + 2 * (2 + 4); else a ^ 2; endif; endif; end;

test6.txt

// Program Containing the New Comment, Modified Identifier // and Real Literal and Hex and Character Literals -- This is the new style comment function main b: integer, c: integer returns integer; a: real is .3; d: real is 5.7; a__1: real is .4e2; ab_c_d: real is 4.3E+1; ab1_cd2: real is 4.5e-1; hex: integer is #2aF; char1: character is 'C'; char2: character is '\n'; begin hex + 2; end;

test7.txt

// Function with Two Lexical Errors function main returns integer; begin 7 $ 2 ? (2 + 4); end;

test8.txt

-- Punctuation symbols ,:;() => // Valid identifiers name_1 name_1__a2_ab3 // Invalid identifiers name___2 _name3 name4_ // Integer Literals 23 #3aD // Real Literals 123.45 .123 1.2E2 .1e+2 1.2E-2 // Character Literals 'A' '\n' // Logical operators & | ! // Relational operators = <> > >= < <= // Arithmetic operators + - * / % ^ ~ // Reserved words begin case character else elsif end endcase endfold endif endswitch fold function if integer is left list of others real returns right switch then when

Project3Requirements.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

Project3TestData.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;

Project1Requirements.pdf

CMSC 430 Project 1

The first project involves modifying the attached lexical analyzer and the compilation listing

generator code. You need to make the following modifications to the lexical analyzer,

scanner.l:

1. The following reserved words should be added:

else, elsif, endfold, endif, fold, if, left, real, right, then

Each reserved words should be a separate token. The token name should be the same as

the lexeme, but in all upper case.

2. Two additional logical operators should be added. The lexeme for the first should be |

and its token should be OROP. The second logical operator added should be ! and its token

should be NOTOP.

3. Five relational operators should be added. They are =, <>, >, >= and <=. All of the

lexemes should be represented by the single token RELOP.

4. One additional lexeme should be added for the ADDOP token. It is binary operator – that is

the subtraction operator.

5. One additional lexeme should be added for the MULOP token. It is/ that is the division

operator.

6. A new token REMOP should be added for the remainder operator. Its lexeme should be %.

7. A new token EXPOP should be added for the exponentiation operator. Its lexeme should

be ^.

8. A new token NEGOP should be added for the unary minus operator. Its lexeme should be

~.

9. A second type of comment should be added that begins with -- and ends with the end of

line. As with the existing comment, no token should be returned.

10. The definition for the identifiers should be modified so that underscores can be included,

however, no more than two consecutive underscores are permitted, but leading and

trailing underscores should not be permitted.

11. One additional type of integer literal should be added, which are hexadecimal integers.

The begin with the # character followed by one of more decimal digits, or the letter A-F,

in either upper or lower case.

12. A real literal token should be added. It should begin with a sequence of zero or more

digits following by a decimal point followed by one or more additional digits. It may

optionally end with an exponent. If present, the exponent should begin with an e or E,

followed by an optional plus or minus sign followed by one or more digits.

13. The definition for the character literals should be modified so that five additional escape

characters are also allowed: '\b', '\t', '\n', '\b' and '\f'.

You must also modify the header file tokens.h to include each the new tokens mentioned

above.

The compilation listing generator code should be modified as follows:

1. The lastLine function should be modified to compute the total number of errors. If any

errors occurred the number of lexical, syntactic and semantic errors should be displayed.

If no errors occurred, it should display Compiled Successfully. It should return the

total number of errors.

2. The appendError function should be modified to count the number of lexical, syntactic

and semantic errors. The error message passed to it should be added to a queue of

messages that occurred on that line.

3. The displayErrors function should be modified to display all the error messages that

have occurred on the previous line and then clear the queue of messages.

An example of the output of a program with no lexical errors is shown below:

1 // Function with Arithmetic Expression

2

3 function main returns integer;

4 begin

5 7 + 2 * (5 + 4);

6 end;

Compiled Successfully

Here is the required output for a program that contains more than one lexical error on the same

line:

1 // Function with Two Lexical Errors

2

3 function main returns integer;

4 begin

5 7 $ 2 ? (2 + 4);

Lexical Error, Invalid Character $

Lexical Error, Invalid Character ?

6 end;

Lexical Errors 2

Syntax Errors 0

Semantic Errors 0

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

4DBs.docx

Week 1 Discussion

Contains unread posts

Starts Mar 13

·

The projects for this course require a Linux environment that has the compiler tools flex and bison together with make and the GNU g++ compiler. Explain what Linux environment you plan to use and post a screen shot demonstrating that your environment has the required tools.

The language that you will be expected to write a compiler for is a language created for this course. A functional language was chosen because such languages can be easily interpreted without generating any intermediate code. It is important that you understand the language first, so examine the lexical rules that are provided in the project 1 specification in week 2 and the syntax rules that are provided with the project 2 specification in week 4. Write a short program in this language that contains one function, and has at least one input, one variable and one statement of your choice. Show the output of that program on two different inputs. Here is an example of the kind of program you should create:

// A Simple Program with a When and Switch Statement

function main a: integer returns integer;     b: integer is when a > 10, 1 : 2; begin     switch b is         case 1 => 10;         case 2 => 20;         others => 30;       endswitch;     end;

If this program is run with an input of 5, it returns 10. If it is run with an input of 15, it returns 20. If you have any questions on the semantics of this language, post your question as a response to this topic.

Week 3 Discussion

Contains unread posts

Starts Mar 27

·

Define a language that is not a regular language using a context-free grammar that is not ambiguous. The alphabet of the language must contain at least five characters. Describe in English the strings of the language. Choose any string that is in the language and demonstrate that it is in the language by showing the left-most derivation that derives it. Draw the parse tree that corresponds to the left-most derivation. Give an example of one string that is not in the language. 

Week 5 Discussion

Contains unread posts

Starts Apr 10

·

Provide a program in the target language of our compiler/interpreter that incorporates all elements of the language. It must include all of the following:

· Integer (Decimal and Hexadecimal), Real and Character literals

· Every arithmetic operator: + - * / % ^ ~

· Every relational operator: = /= > >= < <=

· Every logical operator: &

· A when statement| 

· An if statement

· A switch statement

· A fold statement

· Multiple variable declarations

· Multiple parameter declarations

In addition, your program should be written in a such a way that it tests the associativity and precedence of all operators. It should also include nested statements. Also provide several sets of input (values for the parameters) to the program, enough so that every line of code within the program is executed at least once. For each input supplied, provide the corresponding output.

Week 7 Discussion

Contains unread posts

Starts Apr 24

·

Choose a programming language with which you are familiar and identify one of the semantic rules of that language. Give an example of a program in that language that violates that rule. Post a screen shot showing the error message that the compiler for that language would generate for your program example.

Then review the requirements for project 4. Select one of the semantic errors that you are to detect in project 4 and provide a test case that will detect it.

Project4TestData.zip
This file is too large to display.View in new window
Project2TestData.zip
This file is too large to display.View in new window
Project4Requirements.pdf
This file is too large to display.View in new window
Project4SkeletonCode.zip
This file is too large to display.View in new window
Project1Approach.pdf
This file is too large to display.View in new window
Project3Approach.pdf
This file is too large to display.View in new window
Project1SkeletonCode.zip
This file is too large to display.View in new window
Project2SkeletonCode.zip
This file is too large to display.View in new window
Project4Approach.pdf
This file is too large to display.View in new window
Project2Approach.pdf
This file is too large to display.View in new window
Unconfirmed9552.crdownload
This file is too large to display.View in new window
Project2Requirements.pdf
This file is too large to display.View in new window