• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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",tokString);
             }
             if (errflag)
             {
                   fprintf(tokfile,"%s","Error in source file line number:");
                   fprintf(tokfile,"%d\n",linenum);
             }
    }

} //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);
            }
}
0
morales7_0
Asked:
morales7_0
1 Solution
 
AxterCommented:
What print function.

You don't have any printf functions in you entire code.

Please be more specific about your question.

If you want to call a print function in your main you can do the following :
      while(!feof(tokfile))
      {
            printf("hello world\n");
            //Or
            cout << "hello world again" << endl;
            getNxChar();
            ungetNxChar();
            getTok();
      }

But I'm not sure what you want to print.
0
 
morales7_0Author Commented:
I'm sorry, I should have been more specific.  What I want to do is call the printToken() function with the rest of the other functions in the while loop just like they are being called above.  Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now