Question: #ifndef PARSER _ H #define PARSER _ H void program ( ) ; void statement ( ) ; void forStatement ( ) ; void ifStatement

#ifndef PARSER_H
#define PARSER_H
void program();
void statement();
void forStatement();
void ifStatement();
void whileStatement();
void doWhileStatement();
void expression();
void condition();
void match(int expectedToken);
#endif // PARSER_H
parser.h #include
#include
#include "token.h"
#include "parser.h"
void program(){
getChar();
lex();
while (nextToken != EOF_TOKEN){
statement();
}
}
void statement(){
switch (nextToken){
case FOR_CODE: forStatement(); break;
case IF_CODE: ifStatement(); break;
case WHILE_CODE: whileStatement(); break;
case DO_CODE: doWhileStatement(); break;
default: printf("Syntax error: invalid statement
"); exit(1);
}
}
void forStatement(){
match(FOR_CODE);
match(LEFT_PAREN);
expression();
match(SEMICOLON);
condition();
match(SEMICOLON);
expression();
match(RIGHT_PAREN);
match(LEFT_BRACE);
while (nextToken != RIGHT_BRACE){
statement();
}
match(RIGHT_BRACE);
}
void ifStatement(){
match(IF_CODE);
match(LEFT_PAREN);
condition();
match(RIGHT_PAREN);
match(LEFT_BRACE);
while (nextToken != RIGHT_BRACE){
statement();
}
match(RIGHT_BRACE);
if (nextToken == ELSE_CODE){
match(ELSE_CODE);
match(LEFT_BRACE);
while (nextToken != RIGHT_BRACE){
statement();
}
match(RIGHT_BRACE);
}
}
void whileStatement(){
match(WHILE_CODE);
match(LEFT_PAREN);
condition();
match(RIGHT_PAREN);
match(LEFT_BRACE);
while (nextToken != RIGHT_BRACE){
statement();
}
match(RIGHT_BRACE);
}
void doWhileStatement(){
match(DO_CODE);
match(LEFT_BRACE);
while (nextToken != RIGHT_BRACE){
statement();
}
match(RIGHT_BRACE);
match(WHILE_CODE);
match(LEFT_PAREN);
condition();
match(RIGHT_PAREN);
match(SEMICOLON);
}
void expression(){
lex();
}
void condition(){
lex();
}
void match(int expectedToken){
if (nextToken == expectedToken){
lex();
} else {
printf("Syntax error: expected token %d
", expectedToken);
exit(1);
}
}
int main(){
if ((in_fp = fopen("front.in","r"))== NULL){
printf("ERROR: cannot open front.in
");
return 1;
} else {
program();
printf("Parsing completed successfully.
");
fclose(in_fp);
}
return 0;
}
// Dummy implementations for getChar and lex
int nextToken;
FILE *in_fp;
void getChar(){
// Dummy implementation
}
void lex(){
// Dummy implementation
nextToken = EOF_TOKEN; // Set to EOF_TOKEN for now
}
parser.c
#ifndef TOKEN_H
#define TOKEN_H
#include
// Token definitions
enum {
EOF_TOKEN,
// Add other token definitions here as needed
FOR_CODE, IF_CODE, WHILE_CODE, DO_CODE, ELSE_CODE,
LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE,
SEMICOLON
};
// Global variables
extern int nextToken;
extern FILE *in_fp;
// Function prototypes
void getChar();
void lex();
#endif // TOKEN_H
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

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!