compiler

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

 

hello I need help 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.     A new token ARROW should be added for the two character punctuation symbol =>.

2.     The following reserved words should be added:

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

case, else, endcase, endif, if, others, real, then, when

Each reserved words should be a separate token. The token name should be the same as the lexeme, but in all upper case.

3.     Two additional logical operators should be added. The lexeme for the first should be or and its token should be OROP. The second logical operator added should be not and its token should be NOTOP.

4.     Five relational operators should be added. They are =, /=, >, >= and <=. All of the lexemes should be represented by the single token RELOP.

5.     One additional lexeme should be added for the ADDOP token. It is binary -.

6.     One additional lexeme should be added for the MULOP token. It is/.

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

rem.

8.     A new token EXPOP should be added for the exponentiation 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, consecutive underscores, leading and trailing underscores should not be permitted.

11. A real literal token should be added. It should begin with a sequence of one or more digits following by a decimal point followed by zero 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. The token should be named REAL_LITERAL.

12. A Boolean literal token should be added. It should have two lexemes, which are true and

false. The token should be named BOOL_LITERAL.

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     (* Program with no errors *) 2

3        function test1 returns boolean;

4        begin

5               7 + 2 > 6 and 8 = 5 * (7 – 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 test2 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

 

SKELETON CODE:

========================================================================

listing.h

enum ErrorCategories {LEXICAL, SYNTAX, GENERAL_SEMANTIC, DUPLICATE_IDENTIFIER,

UNDECLARED};

void firstLine();

void nextLine();

int lastLine();

void appendError(ErrorCategories errorCategory, string message);

========================================================================

listing.cc

#include <cstdio>

#include <string>

using namespace 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 = “”;

}

==========================================================================

tokens.h

enum Tokens {RELOP = 256, ADDOP, MULOP, ANDOP, BEGIN_, BOOLEAN, END, ENDREDUCE,

FUNCTION, INTEGER, IS, REDUCE, RETURNS, IDENTIFIER, INT_LITERAL};

==========================================================================

scanner.l

/* This file contains flex input file */

%{

#include <cstdio>

#include <string>

using namespace std;

#include “listing.h”

#include “tokens.h”

%}

%option noyywrap

ws  [ \t\r]+

comment  \-\-.*\n

line  [\n]

id  [A-Za-z][A-Za-z0-9]*

digit  [0-9]

int  {digit}+

punc  [\(\),:;]

%%

{ws}  { ECHO; }

{comment} { ECHO; nextLine();}

{line}  { ECHO; nextLine();}

“<”  { ECHO; return(RELOP); }

“+”  { ECHO; return(ADDOP); }

“*”  { ECHO; return(MULOP); }

begin  { ECHO; return(BEGIN_); }

boolean  { ECHO; return(BOOLEAN); }

end  { ECHO; return(END); }

endreduce { ECHO; return(ENDREDUCE); }

function { ECHO; return(FUNCTION); }

integer  { ECHO; return(INTEGER); }

is  { ECHO; return(IS); }

reduce  { ECHO; return REDUCE; }

returns  { ECHO; return(RETURNS); }

and  { ECHO; return(ANDOP); }

{id}  { ECHO; return(IDENTIFIER);}

{int}  { ECHO; return(INT_LITERAL); }

{punc}  { ECHO; return(yytext[0]); }

.  { ECHO; appendError(LEXICAL, yytext); }

%%

int main()

{

firstLine();

FILE *file = fopen(“lexemes.txt”, “wa”);

int token = yylex();

while (token)

{

  fprintf(file, “%d %s\n”, token, yytext);

  token = yylex();

}

lastLine();

fclose(file);

return 0;

}

==============================================================================

makefile

compile: scanner.o listing.o

g++ -o compile scanner.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

listing.o: listing.cc listing.h

g++ -c listing.cc

Calculate the price
Make an order in advance and get the best price
Pages (550 words)
$0.00
*Price with a welcome 15% discount applied.
Pro tip: If you want to save more money and pay the lowest price, you need to set a more extended deadline.
We know how difficult it is to be a student these days. That's why our prices are one of the most affordable on the market, and there are no hidden fees.

Instead, we offer bonuses, discounts, and free services to make your experience outstanding.
How it works
Receive a 100% original paper that will pass Turnitin from a top essay writing service
step 1
Upload your instructions
Fill out the order form and provide paper details. You can even attach screenshots or add additional instructions later. If something is not clear or missing, the writer will contact you for clarification.
Pro service tips
How to get the most out of your experience with Homework Mules
One writer throughout the entire course
If you like the writer, you can hire them again. Just copy & paste their ID on the order form ("Preferred Writer's ID" field). This way, your vocabulary will be uniform, and the writer will be aware of your needs.
The same paper from different writers
You can order essay or any other work from two different writers to choose the best one or give another version to a friend. This can be done through the add-on "Same paper from another writer."
Copy of sources used by the writer
Our college essay writers work with ScienceDirect and other databases. They can send you articles or materials used in PDF or through screenshots. Just tick the "Copy of sources" field on the order form.
Testimonials
See why 20k+ students have chosen us as their sole writing assistance provider
Check out the latest reviews and opinions submitted by real customers worldwide and make an informed decision.
Education
Thank you so much, Reaserch writer. you are so helpfull. I appreciate all the hard works. See you.
Customer 452701, February 12th, 2023
Technology
Thank you for your work
Customer 452551, October 22nd, 2021
Business Studies
Great paper thanks!
Customer 452543, January 23rd, 2023
Accounting
Thank you for your help. I made a few minor adjustments to the paper but overall it was good.
Customer 452591, November 11th, 2021
Political science
I like the way it is organized, summarizes the main point, and compare the two articles. Thank you!
Customer 452701, February 12th, 2023
Finance
Thank you very much!! I should definitely pass my class now. I appreciate you!!
Customer 452591, June 18th, 2022
Psychology
Thank you. I will forward critique once I receive it.
Customer 452467, July 25th, 2020
Psychology
I requested a revision and it was returned in less than 24 hours. Great job!
Customer 452467, November 15th, 2020
Political science
Thank you!
Customer 452701, February 12th, 2023
11,595
Customer reviews in total
96%
Current satisfaction rate
3 pages
Average paper length
37%
Customers referred by a friend
OUR GIFT TO YOU
15% OFF your first order
Use a coupon FIRST15 and enjoy expert help with any task at the most affordable price.
Claim my 15% OFF Order in Chat
Show more
<
Live Chat 1 7633094299EmailWhatsApp

Order your essay today and save 15% with the discount code WELCOME