Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with the following; The first project involves modifying the attached lexical analyzer and the compilation listing generator code. You need to make

I need help with the following;

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.l:

1. A new token ARROW should be added for the two character punctuation symbol =>.

2. The following reserved words should be added: 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.

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.

/* This file contains flex input file */

%{ #include #include

using namespace std;

#include "listing.h" #include "tokens.h"

%}

%option noyywrap

/* This file contains flex input file */ %{ #include  #include  using namespace std; #include "listing.h" #include "tokens.h" %} %option noyywrap ws [ \t ]+ comment \-\-.* line [ ] 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 ", token, yytext); token = yylex(); } lastLine(); fclose(file); return 0; }

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

understand the importance of cost to pricing decisions

Answered: 1 week ago

Question

What does the tape hum lucidum do ?

Answered: 1 week ago

Question

The persistence of vision for the human eye is....,....?

Answered: 1 week ago