#include <fstream>
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define END 0
#define ERR -1
struct token
{
int val;
int type;
int col;
string p;
};
token a;
FILE *fin =fopen("test.txt","r");
static int r=1,col=1;
bool isalpha(char ch)
{
return ('a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z') ? true : false;
}
bool isdigit(char ch)
{
return ('0' <= ch && ch <= '9') ? true : false;
}
bool iskeywords(string str)
{
string kw[18] = {"main","void", "var", "int", "float", "string", "begin", "end", "if", "then", "else", "while", "do", "call", "read", "write", "and", "or"};
for(int i = 0;i<=17;i++)
{
if(str==kw[i])
return true;
}
return false;
}
void show()
{
cout<<'('<<a.type<<',';
if(a.type==3)
printf("%5d",a.val);
else
printf("%5s",a.p.c_str());
printf(",%4d,%4d)\n",r, a.col);
}
void gettoken(){
char tem;
string t;t="";
tem=getc(fin);
while(tem='\n'||tem=='\t'||tem==' '){
if(tem=='\n') {r++;col=1;}
if(tem=='\t') {col+=4;}
if(tem==' ') {a.col=col++;}
tem=getc(fin);
}
if(isalpha(tem)||tem=='$'){
t+=tem;a.col=col++;tem=getc(fin);
while(isalpha(tem)||isdigit(tem)){t+=tem;col++;tem=getc(fin);}
a.p=t;
if (iskeywords(t))
a.type=1;
else
a.type=2;
if (tem!=EOF)
fseek(fin, -1, 1);
t= "";
show();
}
else if(isdigit(tem)){
t+=tem;a.col=col++;tem=getc(fin);
while(isdigit(tem)){t+=tem;col++;tem=getc(fin);}
if(tem='.') {
tem=getc(fin);
while(isdigit(tem)){t+=tem;col++;tem=getc(fin);}
a.type=4;a.p=t;
if(tem!=EOF) fseek(fin,-1,1);
}
a.val=stoi(t);
a.type=3;
if(tem!=EOF) fseek(fin,-1,1);
t="";show();
}
else if(tem=='\"'){
tem=getc(fin);
a.col=col++;
while(tem!='\"'){t+=tem;col++;tem=getc(fin);}
a.type=5;a.p=t;t="";
show();
}
else if(tem=='\''){
tem=getc(fin);a.col=col++;t+=tem;col++;tem=getc(fin);
if(tem=='\''){a.type=6;a.p=t;}
t="";show();
}
else if(tem=='{'||tem=='}'||tem=='('||tem==')'||tem==','||tem==';'){
a.type=7;t+=tem;a.col=col++;a.p=t;t="";show();
}
else if(tem=='+'||tem=='-'||tem=='*'){
a.type=8;t+=tem;a.col=col++;a.p=t;show();
}
else if(tem=='/'){
t+=tem;a.col=col++;tem=getc(fin);
if(tem=='*'){
while(1){
if(tem=='*')
{
tem=getc(fin);
if(tem=='/') break;
}
tem=getc(fin);
}
}
else if(tem=='/'){
while(1){
tem=getc(fin);
if(tem=='\n') break;
}
}
else {fseek(fin,-1,1);a.type=8;a.p=t;t="";show();}
}
else if(tem=='='){
t+=tem;a.col=col++;tem=getc(fin);
if(tem=='=') t+=tem;
fseek(fin,-1,1); a.type=8;a.p=t;t="";show();
}
else if(tem=='>'){
t+=tem;a.col=col++;tem=getc(fin);
if(tem=='=') t+=tem;
fseek(fin,-1,1);a.type=8;a.p=t;t="";show();
}
else if(tem=='<'){
t+=tem;a.col=col++;tem=getc(fin);
if(tem=='=') t+=tem;
fseek(fin,-1,1);a.type=8;a.p=t;t="";show();
}
else {
if(tem==EOF) a.type=END;
else {a.p[0]=tem;a.p[1]='\0';a.type=ERR;}
}
}
int main(){
if(fin==NULL) cout<<"sbzy"<<endl;
while(fin!=NULL) {gettoken();}
return 0;
}