The second project involves modifying the syntactic analyzer for the attached compiler by adding to the existing grammar. The full grammar of the language is shown below. The highlighted portions of...

1 answer below »
The second project involves modifying the syntactic analyzer for the attached compiler by adding to the existing grammar. The full grammar of the language is shown below. The highlighted portions of the grammar show what you must either modify or add to the existing grammar.function: function_header {variable} body function_header: FUNCTION IDENTIFIER [parameters] RETURNS type ; variable: IDENTIFIER : type IS statement parameters: parameter {, parameter} parameter: IDENTIFIER : type type: INTEGER | REAL | BOOLEAN body: BEGIN statement END ; statement: expression ; | REDUCE operator {statement} ENDREDUCE ; | IF expression THEN statement ELSE statement ENDIF ; | CASE expression IS {case} OTHERS ARROW statement ; ENDCASE ; operator: ADDOP | MULOP case: WHEN INT_LITERAL ARROW statement expression: ( expression ) | expression binary_operator expression | NOT expression | INT_LITERAL | REAL_LITERAL | BOOL_LITERAL | IDENTIFIER binary_operator: ADDOP | MULOP | REMOP | EXPOP | RELOP | ANDOP | OROPIn the above grammar, the red symbols are nonterminals, the blue symbols are terminals and the black punctuation are EBNF metasymbols. The braces denote repetition 0 or more times and the brackets denote optional.You must rewrite the grammar to eliminate the EBNF brace and bracket metasymbols and to incorporate the significance of parentheses, operator precedence and associativity for all operators. Among arithmetic operators the exponentiation operator has highest precedence following by the multiplying operators and then the adding operators. All relational operators have the same precedence. Among the binary logical operators, and has higher precedence than or. Of the categories of operators, the unary logical operator has highest precedence, the arithmetic operators have next highest precedence, followed by the relational operators and finally the binary logical operators. All operators except the exponentiation operator are left associative. The directives to specify precedence and associativity, such as %prec and %left, may not be usedYour parser should be able to correctly parse any syntactically correct program without any problem.You must modify the syntactic analyzer to detect and recover from additional syntax errors using the semicolon as the synchronization token. To accomplish detecting additional errors an error production must be added to the function header and another to the variable declaration.Your bison input file should not produce any shift/reduce or reduce/reduce errors. Eliminating them can be difficult so the best strategy is not introduce any. That is best achieved by making small incremental additions to the grammar and ensuring that no addition introduces any such errors.An example of compilation listing output containing syntax errors is shown below:1 -- Multiple errors23 function main a integer returns real;Syntax Error, Unexpected INTEGER, expecting ':'4 b: integer is * 2;Syntax Error, Unexpected MULOP5 c: real is 6.0;6 begin7 if a > c then8 b 3.0;Syntax Error, Unexpected REAL_LITERAL, expecting ';'9 else10 b = 4.;11 endif;12 ;Syntax Error, Unexpected ';', expecting ENDLexical Errors 0Syntax Errors 4 Semantic Errors 0You are to submit two files. 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. 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 pointsParses all syntactically correct programs (25)Does not parse all syntactically correct programs (0)Productions correctly implement precedence and associativity (10)Productions do not correctly implement precedence and associativity (0)Grammar contains no shift/reduce or reduce/reduce errors (5)Grammar contains shift/reduce or reduce/reduce errors (0)Detects and recovers from all programs with single syntax errors (20)Does not detect and recover from errors in the function header (0)Detects and recovers from a program with multiple syntax errors (10)Does not detect and recover from multiple errors (0)Test Cases15 points0 pointsIncludes test cases that test all grammar productions (6)Does not include test cases that test all grammar productions (0)Includes test cases that test errors in all productions (6)Does not include test cases that test errors in all productions (0)Includes test case with multiple errors (3)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)
Answered Same DayApr 01, 2021

Answer To: The second project involves modifying the syntactic analyzer for the attached compiler by adding to...

Pritam answered on Apr 07 2021
150 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 "listing.h"
static int lineNumber;
static int totalErrors = 0;
static int lexicalErrors =
 0;
static int syntaxErrors = 0;
static int semanticErrors = 0;
static queue errors;
static void displayErrors();
void firstLine()
{
    lineNumber = 1;
    printf("\n%4d  ",lineNumber);
}
void nextLine()
{
    displayErrors();
    lineNumber++;
    printf("\n%4d  ",lineNumber);
}
int lastLine()
{
    printf("\r");
    displayErrors();
    if(totalErrors == 0)
        printf("\n\nCompiled Successfully.\n");
    else
        printf("\n\nLexical Errors %d\nSyntax Errors %d\nSemantic Errors %d\n", lexicalErrors, syntaxErrors, semanticErrors);
    return totalErrors;
}

void appendError(ErrorCategories errorCategory, string message)
{
    string messages[] = { "Lexical Error, Invalid Character ", "",
        "Semantic Error, ", "Semantic Error, Duplicate Identifier: ",
        "Semantic Error, Undeclared " };
    errors.push(messages[errorCategory] + message);
    if(errorCategory == LEXICAL)
        lexicalErrors++;
    else if(errorCategory == SYNTAX)
        syntaxErrors++;
    else
        semanticErrors++;
    totalErrors++;
}
void displayErrors()
{
    while (!errors.empty())
    {
            printf("\n\t%s", errors.front().c_str());
            errors.pop();
    }
}
listing.h
// CMSC 430
// Dr. Duane J. Jarc
// This file contains the function prototypes for the functions that produce the // compilation listing
#include // std::cin, std::cout
#include // std::queue
#include
#include
using namespace std;
enum ErrorCategories {LEXICAL, SYNTAX, GENERAL_SEMANTIC, DUPLICATE_IDENTIFIER, UNDECLARED};
void firstLine();
void nextLine();
int lastLine();
void appendError(ErrorCategories errorCategory, std::string message);
makefile
compile: scanner.o parser.o listing.o
    g++ -o compile scanner.o parser.o listing.o
    
scanner.o: scanner.c 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 listing.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
parser.y
/* Compiler Theory and Design
Dr. Duane J. Jarc */
%{
#include
using namespace std;
#include "listing.h"
int yylex();
void yyerror(const char* message);
%}
%error-verbose
%token...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here