Question: The third project involves modifying the attached interpreter so that it interprets programs for the complete language. You may convert all values to double values,

The third project involves modifying the attached interpreter so that it interprets programs for the complete language. You may convert all values to double values, although you can maintain their individual types if you wish. When the program is run on the command line, the parameters to the function should be supplied as command line arguments. For example, for the following function header of a program in the file text.txt:

function main a: integer, b: integer returns integer;

One would execute the program as follows:

$ ./compile < test.txt 2 4

In this case, the parameter a would be initialized to 2 and the parameter b to 4. An example of a program execution is shown below:

$ ./compile < test.txt 2 4

1 function main a: integer, b: integer returns integer;

2 c: integer is

3 if a > b then

4 a rem b;

5 else

6 a ** 2;

7 endif;

8 begin

9 case a is

10 when 1 => c;

11 when 2 => (a + b / 2 - 4) * 3;

12 others => 4;

13 endcase;

14 end;

Compiled Successfully

Result = 0

After the compilation listing is output, the value of the expression which comprises the body of the function should be displayed as shown above. The existing code evaluates some of the arithmetic, relational and logical operators together with the reduction statement and integer literals only. You are to add the necessary code to include all of the following:

  • Real and Boolean literals
  • All additional arithmetic operators
  • All additional relational and logical operators
  • Both if and case statements
  • Functions with multiple variables
  • Functions with parameters

This project requires modification to the bison input file, so that it defines the additional the necessary computations for the above added features. You will need to add functions to the library of evaluation functions already provided in values.cc. You must also make some modifications to the functions already provided.

listing.cc

// 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 = "";

}

listin.h

// 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);

parser.y

%{

#include

#include

#include

#include

using namespace std;

#include "values.h"

#include "listing.h"

#include "symbols.h"

int yylex();

void yyerror(const char* message);

Symbols symbols;

int result;

%}

%error-verbose

%union

{

CharPtr iden;

Operators oper;

int value;

}

%token IDENTIFIER

%token INT_LITERAL

%token ADDOP MULOP RELOP

%token ANDOP

%token BEGIN_ BOOLEAN END ENDREDUCE FUNCTION INTEGER IS REDUCE RETURNS

%type body statement_ statement reductions expression relation term

factor primary

%type operator

%%

function:

function_header optional_variable body {result = $3;} ;

function_header:

FUNCTION IDENTIFIER RETURNS type ';' ;

optional_variable:

variable |

;

variable:

IDENTIFIER ':' type IS statement_ {symbols.insert($1, $5);} ;

type:

INTEGER |

BOOLEAN ;

body:

BEGIN_ statement_ END ';' {$$ = $2;} ;

statement_:

statement ';' |

error ';' {$$ = 0;} ;

statement:

expression |

REDUCE operator reductions ENDREDUCE {$$ = $3;} ;

operator:

ADDOP |

MULOP ;

reductions:

reductions statement_ {$$ = evaluateReduction($0, $1, $2);} |

{$$ = $0 == ADD ? 0 : 1;} ;

expression:

expression ANDOP relation {$$ = $1 && $3;} |

relation ;

relation:

relation RELOP term {$$ = evaluateRelational($1, $2, $3);} |

term ;

term:

term ADDOP factor {$$ = evaluateArithmetic($1, $2, $3);} |

factor ;

factor:

factor MULOP primary {$$ = evaluateArithmetic($1, $2, $3);} |

primary ;

primary:

'(' expression ')' {$$ = $2;} |

INT_LITERAL |

IDENTIFIER {if (!symbols.find($1, $$)) appendError(UNDECLARED, $1);} ;

%%

void yyerror(const char* message)

{

appendError(SYNTAX, message);

}

int main(int argc, char *argv[])

{

firstLine();

yyparse();

if (lastLine() == 0)

cout << "Result = " << result << endl;

return 0;

}

scanner.I

/* This file contains flex input file */

%{

#include

using namespace std;

#include "values.h"

#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; yylval.oper = LESS; return(RELOP); }

"+" { ECHO; yylval.oper = ADD; return(ADDOP); }

"*" { ECHO; yylval.oper = MULTIPLY; 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; yylval.iden = (CharPtr)malloc(yyleng + 1);

strcpy(yylval.iden, yytext); return(IDENTIFIER);}

{int} { ECHO; yylval.value = atoi(yytext); return(INT_LITERAL); }

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

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

%%

symbols.h

template <typename T>

class Symbols

{

public:

void insert(char* lexeme, T entry);

bool find(char* lexeme, T& entry);

private:

map symbols;

};

template <typename T>

void Symbols::insert(char* lexeme, T entry)

{

string name(lexeme);

symbols[name] = entry;

}

template <typename T>

bool Symbols::find(char* lexeme, T& entry)

{

string name(lexeme);

typedef typename map::iterator Iterator;

Iterator iterator = symbols.find(name);

bool found = iterator != symbols.end();

if (found)

entry = iterator->second;

return found;

}

values.cc

// This file contains the bodies of the evaluation functions

#include

#include

#include

using namespace std;

#include "values.h"

#include "listing.h"

int evaluateReduction(Operators operator_, int head, int tail)

{

if (operator_ == ADD)

return head + tail;

return head * tail;

}

int evaluateRelational(int left, Operators operator_, int right)

{

int result;

switch (operator_)

{

case LESS:

result = left < right;

break;

}

return result;

}

int evaluateArithmetic(int left, Operators operator_, int right)

{

int result;

switch (operator_)

{

case ADD:

result = left + right;

break;

case MULTIPLY:

result = left * right;

break;

}

return result;

}

values.h

// This file contains function definitions for the evaluation functions

typedef char* CharPtr;

enum Operators {LESS, ADD, MULTIPLY};

int evaluateReduction(Operators operator_, int head, int tail);

int evaluateRelational(int left, Operators operator_, int right);

int evaluateArithmetic(int left, Operators operator_, int right);

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

mv 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!