CMSC 430 Project 4The fourth project involves modifying the semantic analyzer for the attached compiler by addingchecks for semantic errors. The static semantic rules of this language are the...

1 answer below »
CMSC 430 Project 4The fourth project involves modifying the semantic analyzer for the attached compiler by addingchecks for semantic errors. The static semantic rules of this language are the following:Variables and parameter names have local scope. The scope rules require that all names bedeclared and prohibit duplicate names within the same scope. The type correspondence rules areas follows: Boolean expressions cannot be used with arithmetic or relational operators. Arithmetic expressions cannot be used with logical operators. Reductions can only contain numeric types. Only integer operands can be used with the remainder operator. The two statements in an if statement must match in type. No coercion is performed. All the statements in a case statement must match in type. No coercion is performed. The type of the if expression must be Boolean. The type of the case expression must be Integer A narrowing variable initialization or function return occurs when a real value is beingforced into integer. Widening is permitted. Boolean types cannot be mixed with numeric types in variable initializations or functionreturns.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 alreadyperformed by the code that you have been provided, although you are must make minormodifications to account for the addition of real types and the need to perform type coercion andto handle the additional arithmetic and logical operators. Using Boolean Expressions with Arithmetic Operator Using Boolean Expressions with Relational Operator Using Arithmetic Expressions with Logical Operator Reductions containing nonnumeric types Remainder Operator Requires Integer Operands If-Then Type Mismatch Case Types Mismatch If Condition Not Boolean Case Expression Not Integer Narrowing Variable Initialization Variable Initialization Mismatch Undeclared Variable Duplicate Variable Narrowing Function ReturnThis 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 Boolean 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 and the error message must be modified to indicate that numeric rather than only integer types are permitted.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 compilation listing output containing semantic errors is shown below:1 -- Test of Multiple Semantic Errors 2 3 function test a: integer returns integer; 4 b: integer is 5 if a + 5 then 6 2; 7 else 8 5; 9 endif; Semantic Error, If Expression Must Be Boolean 10 c: real is 9.8 - 2 + 8; 11 d: boolean is 7 = f; Semantic Error, Undeclared f 12 begin 13 case b is 14 when 1 => 4.5 + c; 15 when 2 => b; Semantic Error, Case Types Mismatch 16 others => c; 17 endcase; 18 end; Lexical Errors 0 Syntax Errors 0 Semantic Errors 3You 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 projectb. 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 casec. A discussion of lessons learned from the project and any improvements that could be madeGrading Rubric Criteria Meets Does Not MeetFunctionality70 points0 pointsGenerates semantic error when a remainder operator has non-integer operands (10)Does not generate semantic error when a remainder operator has non-integer operands (0)Generates semantic error when if and then types don't match (10)Does not generate semantic error when if and then types don't match (0)Generates semantic error when case types don't match (10)Does not generate semantic error when case types don't match (0)Generates semantic error when if condition is not Boolean (10)Does not generates semantic error when if condition is not Boolean (0)Generates semantic error when case expression is not integer (10)Does not generate semantic error when case expression is not integer (0)Generates semantic error on narrowing initialization (10)Does not generate semantic error on narrowing initialization (0)Generates semantic error for duplicate variables (10)Does not generate semantic error for duplicate variables (0)Test Cases15 points0 pointsIncludes test cases that test all type checking errors (10)Does not Include test cases that test all type checking errors (0)Includes test cases that test all symbol table errors (3)Does not include test cases that test all symbol table errors (0)Includes test case with multiple errors (2)Does not include test case with multiple errors (0)Documentation15 points0 pointsDiscussion of approach included (5)Discussion of approach not included (0)Lessons learned included (5)Lessons learned not included (0)Comment blocks with student name, project, date and code description included in each file (5)Comment blocks with student name, project, date and code description not included in each file (0)
**Cannot be late it's the final project due May, 10 9AM** He and the University will NOT accept late work, thank you for project 3 Pritam!
Answered Same DayMay 04, 2021

Answer To: CMSC 430 Project 4The fourth project involves modifying the semantic analyzer for the attached...

Pritam answered on May 09 2021
129 Votes
listing.cc
listing.cc
// Compiler Theory and Design
// Dr. Duane J. Jarc
// This file contains the bodies of the functions that produces the compilation
// listing
#include 
#include 
using names
pace std;
#include "listing.h"
static int lineNumber;
static string error = "";
static int totalErrors = 0;
static void displayErrors();
void firstLine()
{
    lineNumber = 1;
    printf("\n%4d  ",lineNumber);
}
void nextLine()
{
    displayErrors();
    lineNumber++;
    printf("%4d  ",lineNumber);
}
int lastLine()
{
    printf("\r");
    displayErrors();
    printf("     \n");
    return totalErrors;
}

void appendError(ErrorCategories errorCategory, string message)
{
    string messages[] = { "Lexical Error, Invalid Character ", "",
        "Semantic Error, ", "Semantic Error, Duplicate Identifier: ",
        "Semantic Error, Undeclared " };
    error = messages[errorCategory] + message;
    totalErrors++;
}
void displayErrors()
{
    if (error != "")
        printf("%s\n", error.c_str());
    error = "";
}
listing.h
// Compiler Theory and Design
// Duane J. Jarc
// This file contains the function prototypes for the functions that produce the
// compilation listing
enum ErrorCategories {LEXICAL, SYNTAX, GENERAL_SEMANTIC, DUPLICATE_IDENTIFIER,
    UNDECLARED};
void firstLine();
void nextLine();
int lastLine();
void appendError(ErrorCategories errorCategory, string message);
makefile
compile: scanner.o parser.o listing.o types.o
    g++ -o compile scanner.o parser.o listing.o types.o
    
scanner.o: scanner.c types.h listing.h tokens.h
    g++ -c scanner.c
scanner.c: scanner.l    
    flex scanner.l
    mv lex.yy.c scanner.c
parser.o: parser.c types.h listing.h symbols.h
    g++ -c parser.c
parser.c tokens.h: parser.y
    bison -d -v parser.y
    mv parser.tab.c parser.c
    mv parser.tab.h tokens.h
listing.o: listing.cc listing.h
    g++ -c listing.cc
types.o: types.cc types.h
    g++ -c types.cc
parser.y
/* Compiler Theory and Design
Duane J. Jarc */
%{
#include
#include
#include
using namespace std;
#include "types.h"
#include "listing.h"
#include "symbols.h"
int...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here