Compiler Theory
Please follow instructions thoroughly.
2 years ago
60
Project4TestData.zip
Project4Requirements.pdf
- Project4Approach.pdf
- CMSC430P3.zip
- Project4SkeletonCode.zip
Project4TestData.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;
Project4Requirements.pdf
CMSC 430 Project 4
The fourth project involves modifying the semantic analyzer for the attached compiler by adding
checks for semantic errors. The static semantic rules of this language are the following:
Variable and parameter names have local scope. The scope rules require that all names be
declared and prohibit duplicate names within the same scope. The type correspondence rules are
as follows:
Both expressions in the when statement must be the same type
The type of the switch expression must be Integer.
All the case statements in a switch statement must match in type. No coercion is
performed.
Arithmetic operators can only be used with numeric types.
All list elements must be of the same type.
In a list variable declaration, the type of the list must match the type of the elements.
List subscripts must be integer expressions.
Character literals can be compared to one another but they cannot be compared to
numeric expressions.
Only integer operands can be used with the remainder operator.
All the statements in an if statement must match in type. No coercion is performed.
In a fold statement, the list must be a numeric type.
A narrowing variable initialization or function return error occurs when a real value is
being forced into integer. Widening is permitted. In all other cases, the type must match.
Type coercion from an integer to a real type is performed within arithmetic expressions.
You must make the following semantic checks. Those highlighted in yellow are already
performed by the code that you have been provided, although you are must make minor
modifications to account for the addition of real types and the need to perform type coercion and
to handle the additional arithmetic, relational and logical operators.
Type Mismatch on Variable Initialization
When Types Mismatch
Switch Expression Not Integer
Case Types Mismatch
Arithmetic Operator Requires Numeric Types
Undeclared Scalar
Undeclared List
List Element Types Do Not Match
List Type Does Not Match Element Types
List Subscript Must Be Integer
Character Literals Cannot be Compared to Numeric Expressions
Remainder Operator Requires Integer Operands
If-Elsif-Else Type Mismatch
Fold Requires A Numeric List
Illegal Narrowing Variable Initialization
Illegal Narrowing Function Return
Type Mismatch on Function Return
Duplicate Scalar
Duplicate List
This project requires modification to the bison input file, so that it defines the additional
semantic checks necessary to produce these errors and addition of functions to the library of type
checking functions already provided in types.cc. You must also make some modifications to
the functions provided. You need to add a check to the checkAssignment function for
mismatched types in the case that nonnumeric and numeric types are mixed. You need to also
add code to the checkArithmetic function to coerce integers to reals when the types are mixed.
The provided code includes a template class Symbols that defines the symbol table. It already
includes a check for undeclared identifiers. You need to add a check for duplicate identifiers.
Like the lexical and syntax errors, the compiler should display the semantic errors in the
compilation listing, after the line in which they occur. An example of a compilation listing output
containing semantic errors is shown below:
1 // Multiple Semantic Errors
2
3 function main returns integer;
4 value: integer is 4.5;
Semantic Error, Illegal Narrowing Variable Initialization
5 numbers: list of real is (1, 2, 3);
Semantic Error, List Type Does Not Match Element Types
6 one: integer is '1';
Semantic Error, Type Mismatch on Variable Initialization
7 begin
8 if value > 0 then
9 fold left + ('a', 'b') endfold;
Semantic Error, Fold Requires A Numeric List
10 elsif name = 'N' then
Semantic Error, Undeclared Scalar name
11 fold right * (1, 2.5) endfold;
Semantic Error, List Element Types Do Not Match
12 else
13 when value < 10, 1 : 1.5;
Semantic Error, When Types Mismatch
14 endif;
15 end;
Lexical Errors 0
Syntax Errors 0
Semantic Errors 7
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