I′m working on a project that involves modifying an existing interpreter to fully support a programming language. This requires updates to the Flex (.l) and Bison (.y) files for lexical analysis and parsing, as well as modifications to values.cc for evaluation functions. The project involves adding support for hexadecimal integers, character literals (including escape sequences), additional arithmetic, relational, and logical operators, as well as if and fold statements. It also needs to handle functions with multiple variables and parameters, which are passed as command-line arguments. The final implementation must compile and run using Flex, Bison, and g++ in a terminal, not as a standalone C++ file, and a Makefile should be included for building the project. It’s crucial to follow the provided instructions carefully and ensure all features are tested using the provided test cases, with screenshots of the results.
__MACOSX/._Project 3 Test Data
Project 3 Test Data/test7.txt
// Tests All Arithmetic Operators
function main returns integer;
begin
9 + 2 – (5 + ~1) / 2 % 3 * 3 ^ 1 ^ 2;
end;
__MACOSX/Project 3 Test Data/._test7.txt
Project 3 Test Data/test6.txt
// Function with Character Literal Escape Characters
function main returns character;
lines: integer is 60;
begin
when lines < 60, '\n' : '\f';
end;
__MACOSX/Project 3 Test Data/._test6.txt
Project 3 Test Data/test4.txt
// Function with a List Variable
function main returns integer;
primes: list of integer is (2, 3, 5, 7);
begin
primes(1) + primes(2);
end;
__MACOSX/Project 3 Test Data/._test4.txt
Project 3 Test Data/test5.txt
// Function with Arithmetic Expression using Real Literals
// and Hexadecimal Integer Literals
function main returns real;
begin
.83e+2 + 2.5E-1 + (4.3E2 + #aF2) * .01;
end;
__MACOSX/Project 3 Test Data/._test5.txt
Project 3 Test Data/test1.txt
// Function with Arithmetic Expression
function main returns integer;
begin
7 + 2 * (5 + 4);
end;
__MACOSX/Project 3 Test Data/._test1.txt
Project 3 Test Data/test2.txt
// Function with When Statement
function main returns integer;
begin
when 3 < 2 & 6 < 7, 7 * 2 : 7 + 2;
end;
__MACOSX/Project 3 Test Data/._test2.txt
Project 3 Test Data/test3.txt
// Function with a Switch Statement
function main returns character;
a: integer is 2 * 2 + 1;
begin
switch a is
case 1 => ‘a’;
case 2 => ‘b’;
others => ‘c’;
endswitch;
end;
__MACOSX/Project 3 Test Data/._test3.txt
Project 3 Test Data/test14.txt
// Test Left Fold
function main returns integer;
begin
fold left – (3, 2, 1) endfold;
end;
__MACOSX/Project 3 Test Data/._test14.txt
Project 3 Test Data/test15.txt
// Test that Includes All Statements
function main a: integer, b: real, c: character returns real;
d: integer is when a > 0, #A: #A0;
e: list of integer is (3, 2, 1);
f: integer is
switch a is
case 1 => fold left – e endfold;
case 2 => fold right – e endfold;
others => 0;
endswitch;
g: integer is
if c = ‘A’ & b > 0 then
a * 2;
elsif c = ‘B’ | b < 0 then
(a ^ 2) * 10;
else
0;
endif;
begin
f + (d - 1) % g;
end;
__MACOSX/Project 3 Test Data/._test15.txt
Project 3 Test Data/test16.txt
// Nested if statements
function main x: integer, y: integer returns character;
begin
if x > 0 then
if y > 0 then
‘1’;
elsif y < 0 then
'4';
else
'Y';
endif;
elsif x < 0 then
if y > 0 then
‘3’;
elsif y < 0 then
'2';
else
'Y';
endif;
else
if y <> 0 then
‘X’;
else
‘O’;
endif;
endif;
end;
__MACOSX/Project 3 Test Data/._test16.txt
Project 3 Test Data/test12.txt
// Two parameter declarations
function main a: integer, b: real returns real;
begin
if a < #A then
b + 1;
else
b - 1;
endif;
end;
__MACOSX/Project 3 Test Data/._test12.txt
Project 3 Test Data/test13.txt
// Test Right Fold
function main returns integer;
values: list of integer is (3, 2, 1);
begin
fold right - values endfold;
end;
__MACOSX/Project 3 Test Data/._test13.txt
Project 3 Test Data/test11.txt
// Single Parameter Declaration
function main a: real returns real;
begin
a + 1.5;
end;
__MACOSX/Project 3 Test Data/._test11.txt
Project 3 Test Data/test10.txt
-- Multiple Variable Initializations
function main returns character;
b: integer is 5 + 1 - 4;
c: real is 2 + 3.7;
d: character is 'A';
begin
if b + 1 - c > 0 then
d;
else
‘\n’;
endif;
end;
__MACOSX/Project 3 Test Data/._test10.txt
Project 3 Test Data/test8.txt
// Test Logical and Relational Operators
function main returns integer;
begin
when !(6 <> 7) & 5 + 4 >= 9 | 6 = 5, 6 + 9 * 3 : 8 – 2 ^ 3;
end;
__MACOSX/Project 3 Test Data/._test8.txt
Project 3 Test Data/test9.txt
// Function with an If Statement
function main returns integer;
a: integer is 28;
begin
if a < 10 then
1;
elsif a < 20 then
2;
elsif a < 30 then
3;
else
4;
endif;
end;
__MACOSX/Project 3 Test Data/._test9.txt
__MACOSX/._Project 3 Skeleton Code-2
Project 3 Skeleton Code-2/scanner.l
/* CMSC 430 Compiler Theory and Design
Project 3 Skeleton
UMGC CITE
Summer 2023 */
/* This file contains flex input file */
%{
#include
#include
#include
using namespace std;
#include “values.h”
#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]
dec {digit}+
char ‘.’
punc [\(\),:;]
%%
{ws} { ECHO; }
{comment} { ECHO; nextLine(); }
{line} { ECHO; nextLine(); }
“+” { ECHO; yylval.oper = ADD; return(ADDOP); }
“*” { ECHO; yylval.oper = MULTIPLY; return(MULOP); }
“&” { ECHO; yylval.oper = AND; return(ANDOP); }
“<" { ECHO; yylval.oper = LESS; return(RELOP); }
"=>” { ECHO; return(ARROW); }
begin { ECHO; return(BEGIN_); }
case { ECHO; return(CASE); }
character { ECHO; return(CHARACTER); }
end { ECHO; return(END); }
endswitch { ECHO; return(ENDSWITCH); }
function { ECHO; return(FUNCTION); }
integer { ECHO; return(INTEGER); }
is { ECHO; return(IS); }
list { ECHO; return(LIST); }
of { ECHO; return(OF); }
others { ECHO; return(OTHERS); }
returns { ECHO; return(RETURNS); }
switch { ECHO; return(SWITCH); }
when { ECHO; return(WHEN); }
{id} { ECHO; yylval.iden = (CharPtr)malloc(yyleng + 1);
strcpy(yylval.iden, yytext); return(IDENTIFIER);}
{dec} { ECHO; yylval.value = atoi(yytext); return(INT_LITERAL); }
{char} { ECHO; yylval.value = yytext[1]; return(CHAR_LITERAL); }
{punc} { ECHO; return(yytext[0]); }
. { ECHO; appendError(LEXICAL, yytext); }
%%
__MACOSX/Project 3 Skeleton Code-2/._scanner.l
Project 3 Skeleton Code-2/symbols.h
// CMSC 430 Compiler Theory and Design
// Project 3 Complete
// UMGC CITE
// Summer 2023
// This file contains the template symbol table
template
class Symbols
{
public:
void insert(char* lexeme, T entry);
bool find(char* lexeme, T& entry);
private:
map
};
template
void Symbols
{
string name(lexeme);
symbols[name] = entry;
}
template
bool Symbols
{
string name(lexeme);
typedef typename map
Iterator iterator = symbols.find(name);
bool found = iterator != symbols.end();
if (found)
entry = iterator->second;
return found;
}
__MACOSX/Project 3 Skeleton Code-2/._symbols.h
Project 3 Skeleton Code-2/makefile
compile: scanner.o parser.o listing.o values.o
g++ -o compile scanner.o parser.o listing.o values.o
scanner.o: scanner.c values.h 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 values.h listing.h symbols.h
g++ -c parser.c
parser.c tokens.h: parser.y
bison -d -v parser.y
mv parser.tab.c parser.c
cp parser.tab.h tokens.h
listing.o: listing.cc listing.h
g++ -c listing.cc
values.o: values.cc values.h
g++ -c values.cc
__MACOSX/Project 3 Skeleton Code-2/._makefile
Project 3 Skeleton Code-2/values.h
// CMSC 430 Compiler Theory and Design
// Project 3 Skeleton
// UMGC CITE
// Summer 2023
// This file contains type definitions and the function
// definitions for the evaluation functions
typedef char* CharPtr;
enum Operators {ADD, MULTIPLY, LESS, AND};
double evaluateArithmetic(double left, Operators operator_, double right);
double evaluateRelational(double left, Operators operator_, double right);
__MACOSX/Project 3 Skeleton Code-2/._values.h
Project 3 Skeleton Code-2/listing.cc
Project 3 Skeleton Code-2/listing.cc
// CMSC 430 Compiler Theory and Design
// Project 3 Skeleton
// UMGC CITE
// Summer 2023
// This file contains the bodies of the functions that produces the
// compilation listing
#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 ”
,
“Semantic Error, Undeclared ”
};
error
=
messages
[
errorCategory
]
+
message
;
totalErrors
++
;
}
void
displayErrors
()
{
if
(
error
!=
“”
)
printf
(
“%s\n”
,
error
.
c_str
());
error
=
“”
;
}
__MACOSX/Project 3 Skeleton Code-2/._listing.cc
Project 3 Skeleton Code-2/parser.y
/* CMSC 430 Compiler Theory and Design
Project 3 Skeleton
UMGC CITE
Summer 2023
Project 3 Parser with semantic actions for the interpreter */
%{
#include
#include
#include
#include
#include