Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#ifndef PARSER _ H #define PARSER _ H void program ( ) ; void statement ( ) ; void forStatement ( ) ; void ifStatement
#ifndef PARSERH
#define PARSERH
void program;
void statement;
void forStatement;
void ifStatement;
void whileStatement;
void doWhileStatement;
void expression;
void condition;
void matchint expectedToken;
#endif PARSERH
parser.h #include
#include
#include "token.h
#include "parser.h
void program
getChar;
lex;
while nextToken EOFTOKEN
statement;
void statement
switch nextToken
case FORCODE: forStatement; break;
case IFCODE: ifStatement; break;
case WHILECODE: whileStatement; break;
case DOCODE: doWhileStatement; break;
default: printfSyntax error: invalid statement
; exit;
void forStatement
matchFORCODE;
matchLEFTPAREN;
expression;
matchSEMICOLON;
condition;
matchSEMICOLON;
expression;
matchRIGHTPAREN;
matchLEFTBRACE;
while nextToken RIGHTBRACE
statement;
matchRIGHTBRACE;
void ifStatement
matchIFCODE;
matchLEFTPAREN;
condition;
matchRIGHTPAREN;
matchLEFTBRACE;
while nextToken RIGHTBRACE
statement;
matchRIGHTBRACE;
if nextToken ELSECODE
matchELSECODE;
matchLEFTBRACE;
while nextToken RIGHTBRACE
statement;
matchRIGHTBRACE;
void whileStatement
matchWHILECODE;
matchLEFTPAREN;
condition;
matchRIGHTPAREN;
matchLEFTBRACE;
while nextToken RIGHTBRACE
statement;
matchRIGHTBRACE;
void doWhileStatement
matchDOCODE;
matchLEFTBRACE;
while nextToken RIGHTBRACE
statement;
matchRIGHTBRACE;
matchWHILECODE;
matchLEFTPAREN;
condition;
matchRIGHTPAREN;
matchSEMICOLON;
void expression
lex;
void condition
lex;
void matchint expectedToken
if nextToken expectedToken
lex;
else
printfSyntax error: expected token d
expectedToken;
exit;
int main
if infp fopenfrontinr NULL
printfERROR: cannot open front.in
;
return ;
else
program;
printfParsing completed successfully.
;
fcloseinfp;
return ;
Dummy implementations for getChar and lex
int nextToken;
FILE infp;
void getChar
Dummy implementation
void lex
Dummy implementation
nextToken EOFTOKEN; Set to EOFTOKEN for now
parser.c
#ifndef TOKENH
#define TOKENH
#include
Token definitions
enum
EOFTOKEN,
Add other token definitions here as needed
FORCODE, IFCODE, WHILECODE, DOCODE, ELSECODE,
LEFTPAREN, RIGHTPAREN, LEFTBRACE, RIGHTBRACE,
SEMICOLON
;
Global variables
extern int nextToken;
extern FILE infp;
Function prototypes
void getChar;
void lex;
#endif TOKENH
token.h these three codes run correctly but I want tot display the output of the front.in file if everything correct and there no syntax error but in the first must display the parser completed successfully
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