Question: 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

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!