Hi everyone. My assignment is to take a line of text as input from a file and analyze it to give the following output .
I ...identifier
have....identifier
9....number
apples...identifier.
Anything like 2day will show that the string is wrong..I tried to write the program in simple c++ but unfortunately it shows around 24 errors. Also is there any simpler way to write this program.Please help..my assingment is due in 24 hrs.
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#define STATES 5
#define INPUTS 5
#define ACTIONS 2
enum actions { NOACTION ,OUTPUT , APPEND };
enum boolean {true,false};
enum token_type {IF,FOR,WHILE};
class state_table
{
private :
int a[STATES][INPUTS][ACTIONS];
int cur_state,index;
punc p;
FILE* fin;
token_type ttype;
boolean op;
boolean more;
char* token;
int lineno;
symbol_table* st;
public :
state_table(char* filename)
{
initiate();
fin = fopen(filename,"r");
if(!fin)
{
cout<<"File not found : ";
exit(1);
}
more = true;
}
void initiate();
symbol_table* getsymboltable() { return st; }
boolean ifmore() { return more; }
void print_table();
void take_input(char);
void perform_action(int,char);
void resolvekeywords();
int get_lineno();
char* getnexttoken(token_type&);
};
int state_table::get_lineno()
{
return lineno;
}
// initialization of the state table to know the action and next state on
// each input character
void state_table::initiate()
{
int i,j;
cur_state = 0;
lineno = 1;
token = new char;
op = false;
index = 0;
p.int_co("({[]});\'\",.");
st= new symbol_table();
for(i=0;i<STATES;i++)
{
for(j=0;j<INPUTS;j++)
{
if(j!=4||i==0)
a[i][j][0] = j;
else
a[i][j][0] = 0;
if(i==0&&j==0)
a[i][j][1] = NOACTION;
else if(i==0&&j==4)
a[i][j][1] = APPEND;
else if(i==4||j==4)
a[i][j][1] = OUTPUT;
else if(i==0||i==j)
a[i][j][1] = APPEND;
else
a[i][j][1] = OUTPUT;
}
}
a[2][1][0] = 2;
a[2][1][1] = APPEND;
}
void state_table::print_table()
{
int i,j;
for(i=0;i<STATES;i++)
{
for(j=0;j<INPUTS;j++)
cout<<a[i][j][0]<<" "<<a[i][j][1]<<"| ";
cout<<endl;
}
}
void state_table::take_input(char ch)
{
int input,action ;
if(ch==' '||ch==NULL)
input = 0;
else if(ch>='0'&&ch<='9')
input = 1;
else if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||ch=='_')
input = 2;
else if(p.indexof(ch)!=-1)
input = 4;
else
input = 3;
action = a[cur_state][input][1];
perform_action(action,ch);
cur_state = a[cur_state][input][0];
}
// perform the specified action
// it can be either output the token
// or append when the token is not fully formed.
void state_table::perform_action(int action,char ip)
{
switch(action)
{
case NOACTION :
ttype = NV;
break;
case OUTPUT :
token[index] = NULL;
switch(cur_state)
{
case 2:
resolvekeywords();
break;
case 1:
ttype = NUM;
break;
}
op = true;
if(ip!=' ')
{
ungetc(ip,fin);
index = 0;
}
else
{
index = 0;
}
break;
case APPEND :
token[index] = ip;
index++;
break;
}
}
/// resolving the keywords and converting them to tokens
void state_table::resolvekeywords()
{
if(strcmp(token,"if")==0)
ttype = IF;
else if(strcmp(token,"while")==0)
ttype = WHILE;
else if(strcmp(token,"for")==0)
ttype = FOR;
else
{
ttype = ID;
if(!st->isintable(token))
st->add(token);
}
}
char* state_table::getnexttoken(token_type& t)
{
char ch;
while(op!=true)
{
ch = fgetc(fin);
if(ch==EOF)
{
take_input(' ');
more = false;
break;
}
if(ch!='\n')
take_input(ch);
else
{
take_input(' ');
lineno++;
}
}
op= false;
t = ttype;
return token;
}
<< moderator edit: added code tags: [code][/code] tags >>