morales7_0
asked on
How do I get my program to print.
I have this program that I adjusted to print out the tokens with in their categories. I just can't figure out how to call the print function in the while loop I have in main(). Can anyone look over my code and see what's wrong with it. Thanks!!!
/************************* ********** ********** ********** ********** ****/
/* Marco A Morales CS 380 Compilers 30 Nov 04 */
/* */
/* Parse program for the Tiny program */
/* */
/************************* ********** ********** ********** ********** ****/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream>
using std::ofstream;
using std::ifstream;
using std::cout;
using std::cin;
using std::endl;
#define BMAX 256 //maximum length for the input buffer
#define MAXTL 40 //maximum lentgth of the token
#define MAXRESERVED 8
//the six states that the scanner can be in
typedef enum {START,INASSIGN,INCOMMENT, INNUM,INID ,DONE}
State;
typedef enum
{ENDFILE, ERROR, // book-keeping tokens
IF, THEN, ELSE, END, REPEAT, UNTIL, READ, WRITE, // reserved words
ID, NUM, // multicharacter tokens
ASSIGN, EQ, LT, GT, PLUS, MINUS, TIMES, OVER, LPAREN, RPAREN, SEMI} // special symbols
TokenType;
static struct
{ char* str;
TokenType token;
} reservedWords[MAXRESERVED]
= {{"if", IF},{"else", ELSE},{"end", END},
{"repeat", REPEAT},{"until", UNTIL},
{"read", READ},{"write", WRITE}};
static char lineBuf[BMAX]; //array to hold the a line of the Tiny file
static int linenum = 0; //counter to track the number of lines
static int bufsz = 0; //current length of array
static int EoFflag = false; //end of file flag
static int linepos = 0; //marker for the array of "lineBuf"
//modiffied functions from instructor's code
int getNxChar(); //function to get next character from
void ungetNxChar(); //fucntion to go back if not the end of Tiny file
void getTok(); //function to determine token
//modiffied functions from instructor's code
int getNxChar(); //function to get next character from
void ungetNxChar(); //fucntion to go back if not the end of Tiny file
void getTok(); //function to determine token
void printToken ( TokenType, const char* );
//pointers for input and output files
FILE *tokfile, *symbfile;
//main program to use the functions from instructor's code
int main()
{
if((tokfile = fopen("tokens.txt","rt")) == NULL) //open Tiny file
{
cout<<"Error..."<<endl; //give error message if file cannot open
exit(0);
}
else
cout<<"No Error"<<endl; //give message that file was able to be open
if(( symbfile = fopen("symbols.txt","wt")) == NULL) //open tokens file
{
cout<<"Error..."<<endl; //give error message if file cannot open
exit(0);
}
else
cout<<"No Error"<<endl; //give message that file was able to be open
while(!feof(tokfile))
{
getNxChar();
ungetNxChar();
getTok();
}
return 0;
} //end of main
//function to get token from file
int getNxChar()
{
if(!(linepos < bufsz))
{
linenum++;
if(fgets(lineBuf, BMAX-1, tokfile))
{
bufsz = strlen(lineBuf);
linepos = 0;
return lineBuf[linepos++];
}
else
{
EoFflag = true;
return EOF;
}
}
else
{
return lineBuf[linepos++];
}
} //end of getNxChar function
//fucntion to go back if it is not the end of the Tiny file and decrement the position in array
void ungetNxChar()
{
if(!EoFflag)
linepos--;
} //end of ungetNxChar function
//function to match up tokens and write them to new file tokens.txt
void getTok()
{
char tokString[MAXTL+1];
int tokStrInd = 0;
State state = START;
TokenType currentToken;
bool keep, errflag;
errflag = false;
while (state != DONE)
{
int a = getNxChar();
keep = true;
switch(state)
{
case START:
if(isdigit(a))
state = INNUM;
else if(isalpha(a))
state = INID;
else if(a == ':')
state = INASSIGN;
else if((a == ' ')||(a == '\t')||(a == '\n'))
keep = false;
else if(a == '{')
{
keep = false;
state = INCOMMENT;
}
else
{
state = DONE;
switch(a)
{
case EOF:
keep = false;
currentToken = ENDFILE;
break;
case'=':
currentToken = EQ;
break;
case'<':
currentToken = LT;
break;
case'>':
currentToken = GT;
break;
case'(':
currentToken = LPAREN;
break;
case')':
currentToken = RPAREN;
break;
case';':
currentToken = SEMI;
break;
case'+':
currentToken = PLUS;
break;
case'-':
currentToken = MINUS;
break;
case'*':
currentToken = TIMES;
break;
case'/':
currentToken = OVER;
break;
default:
currentToken = ERROR;
keep = false;
errflag = true;
break;
}
}
break;
case INCOMMENT:
keep = false;
if(a == EOF)
state = DONE;
else if(a == '}')
state = START;
break;
case INASSIGN:
state = DONE;
if(!(a == '='))
{
ungetNxChar();
keep = false;
errflag = true;
}
break;
case INNUM:
if(!isdigit(a))
{
ungetNxChar();
keep = false;
state = DONE;
}
break;
case INID:
if(!isalpha(a))
{
ungetNxChar();
keep = false;
state = DONE;
}
break;
}
if((keep) && (tokStrInd <= MAXTL))
tokString[tokStrInd++] = (char)a;
if(state == DONE)
{
//tokString[tokStrInd] = '\0';
//fprintf(tokfile,"%s\n",t okString);
}
if (errflag)
{
fprintf(tokfile,"%s","Erro r in source file line number:");
fprintf(tokfile,"%d\n",lin enum);
}
}
} //end of getTok function
static TokenType tokenLookUpTable (char * t)
{
int i;
for (i=0; i<MAXRESERVED; i++)
if(!strcmp(t, reservedWords[i].str))
return reservedWords[i].token;
return ID;
}
void printToken ( TokenType token, const char * tokenString )
{
switch (token)
{
case IF:
case THEN:
case ELSE:
case END:
case REPEAT:
case UNTIL:
case READ:
case WRITE:
fprintf (symbfile, "reserved word: %s\n",tokenString);
break;
case ASSIGN: fprintf (symbfile, "=\n"); break;
case LT: fprintf (symbfile, "<\n"); break;
case GT: fprintf (symbfile, ">\n"); break;
case EQ: fprintf (symbfile, "==\n"); break;
case LPAREN: fprintf (symbfile, "(\n"); break;
case RPAREN: fprintf (symbfile, ")\n"); break;
case SEMI: fprintf (symbfile, ";\n"); break;
case PLUS: fprintf (symbfile, "+\n"); break;
case MINUS: fprintf (symbfile, "-\n"); break;
case TIMES: fprintf (symbfile, "*\n"); break;
case OVER: fprintf (symbfile, "/\n"); break;
case ENDFILE: fprintf (symbfile, "EOF\n"); break;
case NUM:
fprintf (symbfile, "NUM, val= %s\n", tokenString);
break;
case ID:
fprintf (symbfile, "ID, name= %s\n", tokenString);
break;
case ERROR:
fprintf (symbfile, "ERROR: %s\n", tokenString);
break;
default: fprintf (symbfile, "Unknown token: %d\n", token);
}
}
/*************************
/* Marco A Morales CS 380 Compilers 30 Nov 04 */
/* */
/* Parse program for the Tiny program */
/* */
/*************************
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream>
using std::ofstream;
using std::ifstream;
using std::cout;
using std::cin;
using std::endl;
#define BMAX 256 //maximum length for the input buffer
#define MAXTL 40 //maximum lentgth of the token
#define MAXRESERVED 8
//the six states that the scanner can be in
typedef enum {START,INASSIGN,INCOMMENT,
State;
typedef enum
{ENDFILE, ERROR, // book-keeping tokens
IF, THEN, ELSE, END, REPEAT, UNTIL, READ, WRITE, // reserved words
ID, NUM, // multicharacter tokens
ASSIGN, EQ, LT, GT, PLUS, MINUS, TIMES, OVER, LPAREN, RPAREN, SEMI} // special symbols
TokenType;
static struct
{ char* str;
TokenType token;
} reservedWords[MAXRESERVED]
= {{"if", IF},{"else", ELSE},{"end", END},
{"repeat", REPEAT},{"until", UNTIL},
{"read", READ},{"write", WRITE}};
static char lineBuf[BMAX]; //array to hold the a line of the Tiny file
static int linenum = 0; //counter to track the number of lines
static int bufsz = 0; //current length of array
static int EoFflag = false; //end of file flag
static int linepos = 0; //marker for the array of "lineBuf"
//modiffied functions from instructor's code
int getNxChar(); //function to get next character from
void ungetNxChar(); //fucntion to go back if not the end of Tiny file
void getTok(); //function to determine token
//modiffied functions from instructor's code
int getNxChar(); //function to get next character from
void ungetNxChar(); //fucntion to go back if not the end of Tiny file
void getTok(); //function to determine token
void printToken ( TokenType, const char* );
//pointers for input and output files
FILE *tokfile, *symbfile;
//main program to use the functions from instructor's code
int main()
{
if((tokfile = fopen("tokens.txt","rt")) == NULL) //open Tiny file
{
cout<<"Error..."<<endl; //give error message if file cannot open
exit(0);
}
else
cout<<"No Error"<<endl; //give message that file was able to be open
if(( symbfile = fopen("symbols.txt","wt"))
{
cout<<"Error..."<<endl; //give error message if file cannot open
exit(0);
}
else
cout<<"No Error"<<endl; //give message that file was able to be open
while(!feof(tokfile))
{
getNxChar();
ungetNxChar();
getTok();
}
return 0;
} //end of main
//function to get token from file
int getNxChar()
{
if(!(linepos < bufsz))
{
linenum++;
if(fgets(lineBuf, BMAX-1, tokfile))
{
bufsz = strlen(lineBuf);
linepos = 0;
return lineBuf[linepos++];
}
else
{
EoFflag = true;
return EOF;
}
}
else
{
return lineBuf[linepos++];
}
} //end of getNxChar function
//fucntion to go back if it is not the end of the Tiny file and decrement the position in array
void ungetNxChar()
{
if(!EoFflag)
linepos--;
} //end of ungetNxChar function
//function to match up tokens and write them to new file tokens.txt
void getTok()
{
char tokString[MAXTL+1];
int tokStrInd = 0;
State state = START;
TokenType currentToken;
bool keep, errflag;
errflag = false;
while (state != DONE)
{
int a = getNxChar();
keep = true;
switch(state)
{
case START:
if(isdigit(a))
state = INNUM;
else if(isalpha(a))
state = INID;
else if(a == ':')
state = INASSIGN;
else if((a == ' ')||(a == '\t')||(a == '\n'))
keep = false;
else if(a == '{')
{
keep = false;
state = INCOMMENT;
}
else
{
state = DONE;
switch(a)
{
case EOF:
keep = false;
currentToken = ENDFILE;
break;
case'=':
currentToken = EQ;
break;
case'<':
currentToken = LT;
break;
case'>':
currentToken = GT;
break;
case'(':
currentToken = LPAREN;
break;
case')':
currentToken = RPAREN;
break;
case';':
currentToken = SEMI;
break;
case'+':
currentToken = PLUS;
break;
case'-':
currentToken = MINUS;
break;
case'*':
currentToken = TIMES;
break;
case'/':
currentToken = OVER;
break;
default:
currentToken = ERROR;
keep = false;
errflag = true;
break;
}
}
break;
case INCOMMENT:
keep = false;
if(a == EOF)
state = DONE;
else if(a == '}')
state = START;
break;
case INASSIGN:
state = DONE;
if(!(a == '='))
{
ungetNxChar();
keep = false;
errflag = true;
}
break;
case INNUM:
if(!isdigit(a))
{
ungetNxChar();
keep = false;
state = DONE;
}
break;
case INID:
if(!isalpha(a))
{
ungetNxChar();
keep = false;
state = DONE;
}
break;
}
if((keep) && (tokStrInd <= MAXTL))
tokString[tokStrInd++] = (char)a;
if(state == DONE)
{
//tokString[tokStrInd] = '\0';
//fprintf(tokfile,"%s\n",t
}
if (errflag)
{
fprintf(tokfile,"%s","Erro
fprintf(tokfile,"%d\n",lin
}
}
} //end of getTok function
static TokenType tokenLookUpTable (char * t)
{
int i;
for (i=0; i<MAXRESERVED; i++)
if(!strcmp(t, reservedWords[i].str))
return reservedWords[i].token;
return ID;
}
void printToken ( TokenType token, const char * tokenString )
{
switch (token)
{
case IF:
case THEN:
case ELSE:
case END:
case REPEAT:
case UNTIL:
case READ:
case WRITE:
fprintf (symbfile, "reserved word: %s\n",tokenString);
break;
case ASSIGN: fprintf (symbfile, "=\n"); break;
case LT: fprintf (symbfile, "<\n"); break;
case GT: fprintf (symbfile, ">\n"); break;
case EQ: fprintf (symbfile, "==\n"); break;
case LPAREN: fprintf (symbfile, "(\n"); break;
case RPAREN: fprintf (symbfile, ")\n"); break;
case SEMI: fprintf (symbfile, ";\n"); break;
case PLUS: fprintf (symbfile, "+\n"); break;
case MINUS: fprintf (symbfile, "-\n"); break;
case TIMES: fprintf (symbfile, "*\n"); break;
case OVER: fprintf (symbfile, "/\n"); break;
case ENDFILE: fprintf (symbfile, "EOF\n"); break;
case NUM:
fprintf (symbfile, "NUM, val= %s\n", tokenString);
break;
case ID:
fprintf (symbfile, "ID, name= %s\n", tokenString);
break;
case ERROR:
fprintf (symbfile, "ERROR: %s\n", tokenString);
break;
default: fprintf (symbfile, "Unknown token: %d\n", token);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER