m// c.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char buf[30];
char tokenType[20];
int indexOfBuf=-1;
int indexOfSymbolTable=-1;
int row=1;
int col=0;
int colPre=0;
int rowOfPrint=0;
int numOfError=0;
//************\\cmpWordWithKeyW//*********************************
void cmpWordWithKeyW(char buf[])
{
if(!strcmp(buf,"if"))
strcpy(tokenType,"ifkey");
else if(!strcmp(buf,"for"))
strcpy(tokenType,"forkey");
else if(!strcmp(buf,"else"))
strcpy(tokenType,"elsekey");
else if(!strcmp(buf,"while"))
strcpy(tokenType,"whilekey");
else if(!strcmp(buf,"do"))
strcpy(tokenType,"dokey");
else if(!strcmp(buf,"switch"))
strcpy(tokenType,"seitchkey");
else if(!strcmp(buf,"case"))
strcpy(tokenType,"casekey");
else if(!strcmp(buf,"break"))
strcpy(tokenType,"breakkey");
else if(!strcmp(buf,"main"))
strcpy(tokenType,"mainkey");
else if(!strcmp(buf,"int"))
strcpy(tokenType,"intkey");
else if(!strcmp(buf,"char"))
strcpy(tokenType,"charkey");
else if(!strcmp(buf,"float"))
strcpy(tokenType,"floatkey");
else if(!strcmp(buf,"return"))
strcpy(tokenType,"returntkey");
else if(!strcmp(buf,"void"))
strcpy(tokenType,"voidkey");
else if(!strcmp(buf,"class"))
strcpy(tokenType,"classkey");
else if(!strcmp(buf,"float"))
strcpy(tokenType,"floatkey");
else if(!strcmp(buf,"include"))
strcpy(tokenType,"includekey");
}
//**************\\symbolTable//******************************
class symbolTableItem
{
public:
char tokenName [30];
char tokenType [20];
int rowOfToken;
int colOfToken;
}symbolTable [100];
//*************\\initBuffer//****************************
void initBuffer()
{
for(int i=0;i<30;i++)
buf[i]='\0';
indexOfBuf=-1;
}
//*************\\insertToSymbolTable//****************************
void insertToSymbolTable()
{
indexOfSymbolTable++;
strcpy(symbolTable[indexOfSymbolTable].tokenName,buf);
strcpy(symbolTable[indexOfSymbolTable].tokenType,tokenType);
symbolTable[indexOfSymbolTable].colOfToken=col-indexOfBuf;
symbolTable[indexOfSymbolTable].rowOfToken=row;
initBuffer();
}
//*************\\print//************************************
void print()
{
cout<<" row col tokenName tokenType\n";
cout<<"__________________________________\n\n";
cout<<" ";
for(int i=0;i<indexOfSymbolTable;i++)
{
cout<<" "<<symbolTable[i].rowOfToken<<" , ";
cout<<symbolTable[i].colOfToken<<" ";
cout<<symbolTable[i].tokenName<<" ";
// gotoxy(1,rowOfPrint);
cout<<symbolTable[i].tokenType<<"\n ";
}
}
//*************\\updateGetRowCol//****************************
void updateGetRowCol(char ch)
{
if(col!=0)
colPre=col;
if (ch=='\n' || ch=='\r')
{
col=0;
row++;
}
else
col++;
}
//*************\\updatePutRowCol//****************************
void updatePutRowCol()
{
if(col==0)
row--;
col=colPre;
colPre--;
}
//*************\\insertToBuffer//****************************
void insertToBuffer(char ch)
{
indexOfBuf++;
if(indexOfBuf==30)
{
cout<<"overflow of buffer\n";
initBuffer();
}
else
buf[indexOfBuf]=ch;
}
//*************\\putFromBuffer//****************************
void putFromBuffer()
{
buf[indexOfBuf]='\0';
indexOfBuf--;
}
//**************\\stateDiagram//****************************
void stateDiagram(ifstream &fp)
{
char ch;
initBuffer();
int state=0;
do
{
switch(state)
{
case 0:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(isspace(ch))
state=70;
else if(isalpha(ch))
state=1;
else if(isdigit(ch))
state=3;
else if(ch== '<')
state=14;
else if(ch== '+')
state=12;
else if(ch== '-')
state=13;
else if(ch== '=')
state=17;
else if(ch== '/')
state=20;
else if(ch== '"')
state=25;
else if(ch== '{')
state=28;
else if(ch== '}')
state=29;
else if(ch== '(')
state=30;
else if(ch== ')')
state=31;
else if(ch== '[')
state=32;
else if(ch== ']')
state=33;
else if(ch== '&')
state=34;
else if(ch== '|')
state=36;
else if(ch== '!')
state=38;
else if(ch== ';')
state=42;
else if(ch== '\n')
state=70;
else if(ch== '\r')
state=70;
/*
else if(ch== '\t')
state=70;
*/
break;
case 1:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(isalpha(ch))
state=1;
else if(isdigit(ch))
state=1;
else
state=2;
break;
case 2:
fp.seekg(-1,ios::cur);
updatePutRowCol();
putFromBuffer();
strcpy(tokenType,"id");
cmpWordWithKeyW(buf);
insertToSymbolTable();
initBuffer();
state=0;
break;
case 3:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(isdigit(ch))
state=3;
else if(ch=='.')
state=4;
else
state=10;
break;
case 4:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(isdigit(ch))
state=5;
else
{
rowOfPrint++;
cout<<++numOfError<<". error in declaration of real number("<<row<<","<<colPre-indexOfBuf+1<<")\n";
fp.seekg(-1,ios::cur);;
updatePutRowCol();
initBuffer();
state=0;
}
break;
case 5:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(isdigit(ch))
state=5;
else if(ch=='E'||ch=='e')
state=6;
else
state=11;
break;
case 6:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(isdigit(ch))
state=8;
else if(ch=='+' || ch=='-')
state=7;
else
{
rowOfPrint++;
cout<<++numOfError<<". error in declaration of sientific number("<<row<<","<<colPre-indexOfBuf+1<<")\n";
fp.seekg(-1,ios::cur);;
updatePutRowCol();
initBuffer();
state=0;
}
break;
case 7:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(isdigit(ch))
state=8;
else
{
rowOfPrint++;
cout<<++numOfError<<". error in declaration of sientific number("<<row<<","<<colPre-indexOfBuf+1<<")\n";
fp.seekg(-1,ios::cur);;
updatePutRowCol();
initBuffer();
state=0;
}
break;
case 8:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(isdigit(ch))
state=8;
else
state=9;
break;
case 9:
fp.seekg(-1,ios::cur);;
updatePutRowCol();
putFromBuffer();
strcpy(tokenType,"sientific number");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 10:
fp.seekg(-1,ios::cur);;
updatePutRowCol();
putFromBuffer();
strcpy(tokenType,"integer number");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 11:
fp.seekg(-1,ios::cur);;
updatePutRowCol();
putFromBuffer();
strcpy(tokenType,"real number");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 12:
strcpy(tokenType,"+operator");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 13:
strcpy(tokenType,"-operator");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 14:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(ch=='=')
state=15;
else
state=16;
break;
case 15:
strcpy(tokenType,"lessThanOrEqual");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 16:
fp.seekg(-1,ios::cur);;
updatePutRowCol();
putFromBuffer();
strcpy(tokenType,"lessThan");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 17:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(ch=='=')
state=18;
else
state=19;
break;
case 18:
strcpy(tokenType,"=operator");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 19:
fp.seekg(-1,ios::cur);;
updatePutRowCol();
putFromBuffer();
strcpy(tokenType,"equal");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 20:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(ch=='*')
state=21;
else
state=22;
break;
case 21:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(ch=='*')
state=23;
else if(ch==fp.eof())
state=24;
else
state=21;
break;
case 22:
fp.seekg(-1,ios::cur);;
updatePutRowCol();
putFromBuffer();
strcpy(tokenType,"division");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 23:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(ch=='/')
state=24;
else
state=21;
break;
case 24:
break;
case 25:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(ch=='"')
state=20;
else if(ch=='"\"')
state=27;
else if(ch==fp.eof())
{
rowOfPrint++;
cout<<++numOfError<<". error incomplition litteral("<<row<<","<<colPre-indexOfBuf+1<<")\n";
fp.seekg(-1,ios::cur);;
updatePutRowCol();
initBuffer();
state=0;
}
else
state=25;
break;
case 26:
break;
case 27:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(ch=='"\"')
state=25;
else if(ch=='"')
state=25;
else if(ch=='n')
state=25;
else if(ch=='t')
state=25;
else
{
rowOfPrint++;
cout<<++numOfError<<". error bad litteral("<<row<<","<<colPre-indexOfBuf+1<<")\n";
fp.seekg(-1,ios::cur);;
updatePutRowCol();
initBuffer();
state=0;
}
break;
case 28:
strcpy(tokenType,"leftBrace");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 29:
strcpy(tokenType,"rightBrace");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 30:
strcpy(tokenType,"leftPrantez");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 31:
strcpy(tokenType,"rightPrantez");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 32:
strcpy(tokenType,"left[");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 33:
strcpy(tokenType,"right]");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 34:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(ch=='&')
state=35;
else
{
rowOfPrint++;
cout<<++numOfError<<". error &operator("<<row<<","<<colPre-indexOfBuf+1<<")\n";
fp.seekg(-1,ios::cur);;
updatePutRowCol();
initBuffer();
state=0;
}
break;
case 35:
strcpy(tokenType,"logical ANDoperator");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 36:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(ch=='|')
state=37;
else
{
rowOfPrint++;
cout<<++numOfError<<". error ORoperator("<<row<<","<<colPre-indexOfBuf+1<<")\n";
fp.seekg(-1,ios::cur);;
updatePutRowCol();
initBuffer();
state=0;
}
break;
case 37:
strcpy(tokenType,"logical ORoperator");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 38:
fp.get(ch);
updateGetRowCol(ch);
insertToBuffer(ch);
if(ch=='=')
state=39;
else
{
rowOfPrint++;
cout<<++numOfError<<". error !operator("<<row<<","<<colPre-indexOfBuf+1<<")\n";
fp.seekg(-1,ios::cur);;
updatePutRowCol();
initBuffer();
state=0;
}
break;
case 39:
strcpy(tokenType,"notEqualOperator");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 42:
strcpy(tokenType,";delimiter");
insertToSymbolTable();
initBuffer();
state=0;
break;
case 70:
initBuffer();
state=0;
break;
case 71:
break;
case 72:
break;
}
} while(! fp.eof());
fp.close();
}
//**************\\main//************************************
int main(int argc, char* argv[])
{
// clrscr();
ifstream fp;
fp.open("test.txt");
if(!fp)
{
cout<<"Cannot open file.";
exit(0);
}
cout<<"list of error:\n\n";
stateDiagram(fp);
cout<<"\nsymbel tabel:\n\n";
print();
// getch();
return 0;
}