Question: Your task is to write an interpreter for a simple program whose programming language, reCALC, contains basic language constructs such as variables, assignment statements, simple

Your task is to write an interpreter for a simple program whose programming language, reCALC, contains basic language constructs such as variables, assignment statements, simple control flow, and output. The interpreter will be written using a compiler generator (YACC). The program should call the lexical analyzer (Lex) for the next token, parse the token stream, report grammatical errors, perform static and dynamic semantic checks.

You will need to construct a YACC specification file recalc.y which will be used in conjuction with the provided lexer.l to create your interpreter. The executable can be built (and tested with 0.test, for example) in the following way:

$ yacc -d recalc.y

$ flex -I lexer.l

$ gcc lex.yy.c y.tab.c -lfl

$ ./a.out < tests/0.test

The included proj2.tar contains the needed lexer.l file, a sample executable, and a directory of test files. Do not change the lexer.l file to suit your needs it will be used as is to test your recalc.y file.

Syntax Specification

The syntax of the reCALC language is described by a set of syntax diagrams in syntax.pdf. A syntax diagram is a directed graph with one entry and one exit. Each path through the graph defines an allowable sequence of symbols. For example, the structure of a valid reCALC program is defined by the first syntax diagram. The occurrence of the name of another diagram such as declaration and compound statement indicate that any sequence of symbols defined by the other diagram may occur at that point. The following is an example valid reCALC program, called ex.recalc.

program xyz is

begin

int val;

string farewell;

val = 10;

farewell = Farewell;

print The number . val . is held in val.;

print farewell . !;

end

which yields the following result when run through the reCALC interpreter:

$ ./a.out < ex.recalc

The number 10 is held in val.

Farewell!

Your first task in this assignment is to develop a context free grammar for the reCALC language from the syntax diagrams.

lexer.l contents:

%{ #include #include #include "y.tab.h"

extern int yyline, yycolumn; %}

%% {yyline++; yycolumn=1;} [0-9]+ {yylval.sv = atoi(yytext); yycolumn += yyleng; return(ICONSTnumber);} print {yycolumn += yyleng; return(PRINTnumber);} program {yycolumn += yyleng; return(PROGRAMnumber);} is {yycolumn += yyleng; return(ISnumber);} begin {yycolumn += yyleng; return(BEGINnumber);} end {yycolumn += yyleng; return(ENDnumber);} int {yycolumn += yyleng; return(VARINTnumber);} string {yycolumn += yyleng; return(VARSTRnumber);} [ \t] {yycolumn += yyleng;} div {yycolumn += yyleng; return(DIVnumber);} "." {yycolumn += yyleng; return(DOTnumber);} ";" { yycolumn += yyleng; return(SEMInumber);} "(" {yycolumn += yyleng; return(LPARENnumber);} "-" {yycolumn += yyleng; return(MINUSnumber);} "*" {yycolumn += yyleng; return(TIMESnumber);} "," {yycolumn += yyleng; return(COMMAnumber);} ")" {yycolumn += yyleng; return(RPARENnumber);} "+" {yycolumn += yyleng; return(PLUSnumber);} "=" {yycolumn += yyleng; return(EQnumber);} \"([^"]|\\\")*\" {yylval.s = strdup(yytext); yycolumn += yyleng; return(STRINGnumber);} [a-zA-Z]+[0-9]* {yylval.s = strdup(yytext); yycolumn += yyleng; return(IDnumber);} . {printf("Unknown character %s, line = %d, column = %d ", yytext, yyline, yycolumn); yycolumn += yyleng;} %%

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!