Question
below is some c code that is supposed to scan and parse sample code. if the user chooses option 1, the code is scanned and
below is some c code that is supposed to scan and parse sample code. if the user chooses option 1, the code is scanned and the tokens are printed to a new file. if the user chooses 2, the program is supposed to check for errors and print any on screen. i have some errors that i cant figure out and im also not really sure how to print the tokens to an output file. there is some sample code in the practice language also. any help would be appreciated!
in file:
main
{
id := 15;
read(name);
write(newname);
if(15 == 16)
{
while(abraham != 0)
{
id := 82;
}
}
else
{
read(name2);
}
}
c code:
#include
#include
#include
#include
/*constants for true and false*/
#define FALSE 0
#define TRUE 1
/*enumerated types for token types*/
typedef enum
{
MAIN, LPAREN, RPAREN, READ, WRITE, IF, ELSE, WHILE, ID, INTLITERAL,
PLUSOP, MINUSOP, MULTIOP, DIVOP, LBRACKET, RBRACKET, ASSIGNOP, SMCL,
COMMA, GTHAN, LTHAN, GTOET, LTOET, EQUALOP, NEQUAL, SCANEOF
} token;
/*functions declarations related to scanner*/
token scanner();
void clear_buffer();
void buffer_char(char c);
token check_reserved();
void lexical_error();
/*functions declarations related to parser*/
void parser();
void program();
void statement_list();
void statement();
void id_list();
void boolean_exp();
void expression();
void term();
void add_op();
void relational_op();
void match(token tok);
void syntax_error();
/*global variables*/
FILE *fin; /*source file*/
token next_token; /*next token in source file*/
char token_buffer; /*token buffer*/
int token_ptr; /*buffer pointer*/
int line_num = 1; /* line number in source file*/
int error = FALSE; /*flag to indicate error*/
/********************************************************************************/
/*returns next token from source file*/
token scanner()
{
char c; /*current character in source file*/
clear_buffer(); /*empty token buffer*/
while(TRUE) /*loop reads and returns next token*/
{
c = getc(fin); /*read a character from the source file*/
if(c == EOF)
return SCANEOF; /*end of file*/
else if(isspace(c)) /*skips white spaces and counts line number*/
{
if(c == ' ')
line_num = line_num + 1;
}
else if(isalpha(c)) /*identifier or reserved word*/
{
buffer_char(c); /*buffers the first character*/
c = getc(fin);
while(isalnum(c)) /*reads until the word is complete*/
{
buffer_char(c);
c = getc(fin);
}
ungetc(c, fin); /*puts back the last character read*/
return check_reserved(); /*checks to see if the word is reserved or an id*/
}
else if(isdigit(c))
{
buffer_char(c); /*buffers the first character*/
c = getc(fin);
while(isdigit(c)) /*reads until number is done*/
{
buffer_char(c);
c = getc(fin);
}
ungetc(c, fin); /*puts last character back*/
return INTLITERAL; /*returns integer literal*/
}
else if(c == '(') /*left parentheses*/
return LPAREN;
else if(c == ')') /*right parentheses*/
return RPAREN;
else if(c == ',') /*comma*/
return COMMA;
else if(c == ';') /*semicolon*/
return SMCL;
else if(c == '+') /*plus*/
return PLUSOP;
else if(c == '-') /*minus operator*/
return MINUSOP;
else if(c == '*') /*multiplication operator*/
return MULTIOP;
else if(c == '/') /*division operator or comment*/
{
c = getc(fin);
if(c == '/')
{
do
c = getc(fin);
while (c != ' ');
line_num = line_num + 1;
}
else
{
ungetc(c, fin);
return DIVOP;
}
}
else if(c == ':') /*assign op or error*/
{
c = getc(fin);
if(c == '=')
return ASSIGNOP;
else{
ungetc(c, fin);
lexical_error();
}
}
else if(c == '{') /* left bracket*/
return LBRACKET;
else if(c == '}') /*right bracket*/
return RBRACKET;
else if(c == '>') /*greater than or gtoet op*/
{
c = getc(fin);
if(c == '=')
return GTOET;
else
{
ungetc(c, fin);
return GTHAN;
}
}
else if(c == '<') /* less than or ltoet op*/
{
c = getc(fin);
if(c == '=')
return LTOET;
else
{
ungetc(c, fin);
return LTHAN;
}
}
else if(c == '!') /* not equal op or error*/
{
c = getc(fin);
if(c == '=')
return NEQUAL;
else
{
ungetc(c, fin);
lexical_error();
}
}
else if(c == '=') /*equal op or error*/
{
c = getc(fin);
if(c == '=')
return EQUALOP;
else
{
ungetc(c, fin);
lexical_error();
}
}
else /*invalid characters*/
lexical_error();
}
}
/**************************************************************************/
void clear_buffer() /*clears the buffer*/
{
token_ptr = 0; /* resets token pointer*/
token_buffer[token_ptr] = '\0'; /* adds null character*/
}
void buffer_char(char c)
{
token_buffer[token_ptr] = c; /*append current character*/
token_ptr = token_ptr + 1; /*move token pointer*/
token_buffer[token_ptr] = '\0'; /*move null character*/
}
token check_reserved()
{
/*the 6 reserved words*/
if(strcmp(token_buffer, "main") == 0)
return MAIN;
else if(strcmp(token_buffer, "if") == 0)
return IF;
else if(strcmp(token_buffer, "else") == 0)
return ELSE;
else if(strcmp(token_buffer, "while") == 0)
return WHILE;
else if(strcmp(token_buffer, "read") == 0)
return READ;
else if(strcmp(token_buffer, "write") == 0)
return WRITE;
else
return ID; /* or an identifier*/
}
void lexical_error()
{
printf("lexical error in line %d/n", line_num);
error = TRUE;
}
/******************************************************************/
/*parser section that parses source file*/
void parser()
{
next_token = scanner();
program();
match(SCANEOF);
}
/*
void program()
{
match(MAIN);
match(LBRACKET);
statement_list();
match(RBRACKET);
}
/*
void statement_list()
{
statement();
while(TRUE)
{
if(next_token == ID || next_token == READ || next_token == WRITE ||
next_token == IF || next_token == WHILE)
{
statement();
}
else
break;
}
}
/*
void statement()
{
if(next_token == ID)
{
match(ID);
match(ASSIGNOP);
expression();
match(SMCL);
}
else if(next_token == READ)
{
match(READ);
match(LPAREN);
id_list();
match(RPAREN);
match(SMCL);
}
else if(next_token == WRITE)
{
match(WRITE);
match(LPAREN);
id_list();
match(RPAREN);
match(SMCL);
}
else if(next_token == IF)
{
match(IF);
match(LPAREN);
boolean_exp();
match(RPAREN);
match(LBRACKET);
statement_list();
match(RBRACKET);
if(next_token == ELSE)
{
match(ELSE);
match(LBRACKET);
statement_list();
match(RBRACKET);
}
}
else if(next_token == WHILE)
{
match(WHILE);
match(LPAREN);
boolean_exp();
match(RPAREN);
match(LBRACKET);
statement_list();
match(RBRACKET);
}
else
syntax_error();
}
/*
/*parses the identifier list*/
void id_list()
{
match(ID);
while(next_token == COMMA)
{
match(COMMA);
match(ID);
}
}
/*
void expression()
{
term();
while(next_token == PLUSOP || next_token == MINUSOP ||
next_token == MULTIOP || next_token == DIVOP)
{
add_op();
term();
}
}
/*
void term()
{
if(next_token == ID)
match(ID);
else if(next_token == INTLITERAL)
match(INTLITERAL);
else if(next_token == LPAREN)
{
match(LPAREN);
expression();
match(RPAREN);
}
else
syntax_error();
}
/*
/*parses arithmetic operations*/
void add_op()
{
if(next_token == PLUSOP || next_token == MINUSOP ||
next_token == MULTIOP || next_token == DIVOP)
{
match(next_token);
}
else
syntax_error();
}
/* parses boolean expression*/
void boolean_exp()
{
if(next_token == ID || next_token == INTLITERAL)
match(next_token);
else
syntax_error();
relational_op();
if(next_token == ID || next_token == INTLITERAL)
match(next_token);
else
syntax_error();
}
/*parses boolean operations*/
void relational_op()
{
if(next_token == GTHAN || next_token == LTHAN ||
next_token == GTOET || next_token == LTOET ||
next_token == EQUALOP || next_token == NEQUAL)
{
match(next_token);
}
else
syntax_error();
}
/*checks to see if the token we got is the correct token*/
void match(token tok)
{
if(tok == next_token)
;
else
syntax_error();
next_token = scanner();
}
/*reports any syntax errors with line numbers*/
void syntax_error()
{
printf("syntax error in line %d ", line_num);
error = TRUE;
}
int main()
{
FILE *fout;
char source[20] = " ";
char output[20] = " ";
printf("Enter 1 to scan the source code and write the tokens to an output file");
printf("or 2 to parse the source code");
int option = 0;
scanf("%d", &option);
if(option == 1)
{
printf("Enter source file name: ");
scanf("%s", &source);
fin = source;
printf("Enter output file name: ");
scanf("%s", &output);
fout = output;
}
}
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