Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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. 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.
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. Five relational operators should be added. They are =,>,>, and . All of the
lexemes should be represented by the single token RELOP. One additional lexeme should be added for the ADDOP token. It is binary operator - that is the subtraction operator.
One additional lexeme should be added for the MULOP token. It is / that is the division operator. A new token REMOP should be added for the remainder operator. Its lexeme should be %.
A new token EXPOP should be added for the exponentiation operator. Its lexeme should ^.A new token NEGOP should be added for the unary minus operator. Its lexeme should be ~.
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. 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. 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.
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 inEE, followed by an optional plus or minus sign followed by one or more digits. The definition for the character literals should be modified so that five additional escape characters are also allowed: '\b','\t','
','\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:
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. 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. 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:
??? Function with Arithmetic Expression
function main returns integer;
begin
7+2**(5+4);
end;
Compiled successfully
Here is the required output for a program that contains more than one lexical error on the same
line:
// Function with Two Lexical Errors
function main returns integer;
begin
7$2?(2+4);
exical Error, Invalid Character $
Lexical Error, Invalid Character?
6 end;
Lexical Errors 2
Syntax Errors 0
Semantic Errors 0
token.h
enum Tokens {ADDOP =256, MULOP, ANDOP, RELOP, ARROW, BEGIN_, CASE, CHARACTER, END, ENDSWITCH, FUNCTION, INTEGER, IS, LIST, OF, OTHERS, RETURNS, SWITCH, WHEN, IDENTIFIER, INT_LITERAL, CHAR_LITERAL};
listing.cc
#include
#include
using namespace std;
#include "listing.h"
static int lineNumber;
static string error ="";
static int totalErrors =0;
static void displayErrors();
void firstLine()
{
lineNumber =1;
printf("
%4d ",lineNumber);
}
void nextLine()
{
displayErrors();
lineNumber++;
printf("%4d ",lineNumber);
}
int lastLine()
{
printf("\r");
displayErrors();
printf("
");
return totalErrors;
}
void appendError(ErrorCategories errorCategory, string message)
{
string messages[]={ "Lexical Error, Invalid Character ","",
"Semantic Error, ", "Semantic Error, Duplicate ",
"Semantic Error, Undeclared "};
error = messages[errorCategory]+ message;
totalErrors++;
}
void displayErrors()
{
if (error !="")
printf("%s
", error.c_str());
error ="";
}
listing.h
enum ErrorCategories {LEXICAL, SYNTAX, GENERAL_SEMANTIC, DUPLICATE_IDENTIFIER,
UNDECLARED};
void
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

9th Edition

0135188148, 978-0135188149, 9781642087611

More Books

Students also viewed these Databases questions