Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 . * parser . h * c #ifndef PARSER _ H #define PARSER _ H void program ( ) ; void statement ( )

1.*parser.h*
c
#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
2.*parser.c*
c
#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;
}
3.*token.h*
c
#ifndef TOKEN_H
#define TOKEN_H
#include
// Token definitions
enum {
EOF_TOKEN, how can I run these files in ubuntu in the same director

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

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions