Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions