Question: Please help fix this code and add anything to it so that it is not limited: Please use the following grammar: prog - > begin
Please help fix this code and add anything to it so that it is not limited:
Please use the following grammar:
prog begin stmtlist end
stmtlist stmt ; stmtlist
stmt
stmt ;
stmt var expr
var A B C
expr var expr
var expr
var
Implement a recursive descent parser.
Note that:
we have two keywords: begin and end
we have only three valid identifier: A B and C
the grammar supports three operators: and
a semicolon ; is a stmt separator within stmtlist
The lexical analyzer in lexan.cpp has already been modified to recognize the tokens listed
above. You may want to compile and run testlexan.cpp with lexan.cpp to verify that this
is the case. Use the input file tptxt and create others that you find necessary. It does
not mean that any token that the lexical analyzer recognizes is a valid token for the prog
grammar above. It is the parser's job to flag invalid input due to bad tokens or invalid
statements.
Here are three examples of valid input files they appear in parser.zip and they are tp
txt tptxt and tptxt:
begin
A A
end
begin
A A;
B C
end
begin
B A C;
A B;
C A B C A;
end
The results appear in the file outtxt outtxt and outtxt
Remember, if we can generate a parse tree for an input file, your parser should accept the
input. Anything else should be flagged as an error.
code should not be limited to handle only these three examples
Code: #include
#include
#include
#include "token.h
#include "functions.h
using namespace std;
ifstream ifs;
input file stream used by lexan
SymTab symtab;
global symbol table
Token token;
global token
int lookahead
;
no look ahead token yet
int dbg
;
debut is ON
int main
ifs
get
ifs
;
open an input file stream w
the program
init
kws
;
initialize keywords in the symtab
match
lookahead
;
get the first input token
prog
;
return
;
prog
begin stmt
list end
stmt
list
stmt ; stmt
list
stmt
stmt;
stmt
var
expr
var
A
B
C
expr
var
expr
var
expr
var
void begin
match
KW
;
void end
match
KW
;
void stmts
stmt
;
if
lookahead
;
match
;
;
stmts
;
void stmt
var
;
match
;
expr
;
void var
if
lookahead
ID
match
ID
;
else
error
lookahead ID
;
void expr
var
;
if
lookahead
match
;
expr
;
else if
lookahead
match
;
expr
;
void prog
begin
;
stmts
;
end
;
utility methods
void emit
int t
switch
t
case
: case
: case
:
cout
char
t
;
break;
case
;
:
cout
;
;
break;
case
:
cout
;
break;
case ID:
case KW:
case UID:
cout
symtabtokstr
tokentokvalue
;
break;
default:
cout
token
t
tokvalue
tokentokvalue
;
break;
void error
int t
int expt, const string &str
cerr
unexpected token
;
if
lookahead
DONE
cerr
EOF;
else
cerr
tokentokstr
;
cerr
of type
lookahead;
switch
expt
case
:
default value; nothing to do
break;
case ID:
cout
while looking for an ID
;
break;
case KW:
cout
while looking for KW
str
;
break;
default:
cout
while looking for
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
