[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

How to write the main() for this program?

I have this scanner program that returns only the tokens.  The program needs to read another program that is stored in a text file, pick out the tokens, and write the tokens to another text file.  Can anyone help me on how to write the main() to use this program.  Here's the code for the program that analyses the code and returns the tokens.  Thanks!!!!!!

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream.h>

typedef enum {START,INASSIGN,INCOMMENT,INNUM,INID,DONE}
      State;

#define BMAX 256
#define MAXTL 40

static char lineBuf[BMAX];

static int linenum = 0;
static int bufsz = 0;
static int EoFflag = false;
static int linepos = 0;

 

FILE *source, *tokfile;

static int getNxChar(void)
{
       if(!(linepos < bufsz))
       {
             linenum++;
             
             if(fgets(lineBuf, BMAX-1, source))
             {
                   bufsz = strlen(lineBuf);
                   linepos = 0;
                   return lineBuf[linepos++];
             }
             else
             {
                   EoFflag = true;
                   return EOF;
             }
       }
       else
       {
             return lineBuf[linepos++];
       }
 }

 static void ungetNxChar(void)
 {
       if(!EoFflag)
             linepos--;
 }

 void getTok(void)
 {
       char tokString[MAXTL+1];
       int tokStrInd = 0;
       State state = START;

       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;
                               break;
                         case'=':
                               break;
                         case'<':
                               break;
                         case'>':
                               break;
                         case'(':
                               break;
                         case')':
                               break;
                         case';':
                               break;
                         case'+':
                               break;
                         case'-':
                               break;
                         case'*':
                               break;
                         case'/':
                               break;
                         default:
                               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);
             }
    }

}
 
0
morales7_0
Asked:
morales7_0
1 Solution
 
grg99Commented:
Argh, you can do it this way, with a few state variables.

But you might want to consider the clarity that comes from using loops right in the code, i.e.

if( ch == '{' ) do GetCh(); while( ch != '}' );

... and so on.  

( Just a suggestion )
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