Question
I Need Help with this: The mentioned Files are: ---------------- scanner.l ------------------ /* Compiler Theory and Design Dr. Duane J. Jarc */ /* This file
I Need Help with this:
The mentioned Files are:
----------------
scanner.l
------------------
/* Compiler Theory and Design Dr. Duane J. Jarc */
/* 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(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; }
------------------- tokens.h -------------------
// Compiler Theory and Design
// Dr. Duane J. Jarc
// This file contains the enumerated type definition for tokens enum
Tokens {RELOP = 256, ADDOP, MULOP, ANDOP, BEGIN_, BOOLEAN, END, ENDREDUCE, FUNCTION, INTEGER, IS, REDUCE, RETURNS, IDENTIFIER, INT_LITERAL};
----------------
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
----------------------------
listing.h
--------------------------
// Compiler Theory and Design
// Dr. 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);
-----------------
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 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(" ");
displayErrors();
printf(" ");
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 ", error.c_str());
error = "";
}
---------------------
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.1: rem. 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, endis, is, 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 2 3 function testi returns boolean; 4 begin 7 + 2 > 6 and 8 - 5* (7 - 4); 5 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 7 5 2 12 + 4); Lexical Error, Invalid Character $ Lexical Error, Invalid Character 6 end; 5 Lexical Errors 2 Syntax Errors 0 Semantic Errors O
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started