//Written by Mo Kashi //Lexical Analyzer Program #include #include #include #include ofstream fout,error; ifstream fin; int freeze; class node; typedef node* nodeptr; nodeptr box; //nodeptr top; vectortops; class node{ public: void scanner(); bool isValid(char); bool isAlphabet(char); bool isDigit(char); nodeptr tokenize(nodeptr); node(); // ~node(); int s; int i; nodeptr next; int address; string type; string value; int line_number; }; node::node(){ box=NULL; } bool node::isValid(char c){ if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9')|| c=='_'||c=='='||c=='<'||c=='>'||c==';'||c=='.'|| c==','||c=='+'||c=='-'||c=='*'||c=='/'||c==' '|| c=='('||c==')'||c=='['||c==']'||c=='{'||c=='}'||c==':') return true; else return false; }//isValid bool node::isAlphabet(char c){ if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) return true; else return false; }//isAlphabet bool node::isDigit(char c){ if(c>='0'&&c<='9') return true; else return false; }//isDigit nodeptr node::tokenize(nodeptr top){ return top; }// void node::scanner(){ fin.open("c:\\input.txt"); string line; line_number=1; getline(fin,line); while(fin){ s=line.size(); i=0; string token=""; string token1; while(i!=s){ if(isAlphabet(line[i])&&isDigit(line[i-1])&&isDigit(token[0])){ i++; cout<<"Number cannot end up with an alphabet in line "<='a'&&line[i]<='z')||(line[i]>='A'&&line[i]<='Z'))||(((line[i]>='0'&&line[i]<='9')||line[i]=='_')&&token!="")){ token=token+line[i]; i++; if(!(((line[i]>='a'&&line[i]<='z')||(line[i]>='A'&&line[i]<='Z'))||((line[i]>='0'&&line[i]<='9')||line[i]=='_'))||i==s){ box=new node; box->value=token; box->address=line_number; if(token=="and"||token=="not"||token=="or"|| token=="in"||token=="out"||token=="fi"||token=="write"|| token=="do"||token=="else"||token=="if"||token=="int"|| token=="program"||token=="read"||token=="float"|| token=="return"||token=="then"||token=="for") box->type="RES"; else box->type="ID"; if(token.size()>15){ cout<<"Identifier too long in line "<value=token; box->address=line_number; box->type="BRA"; tops.push_back(box); token=""; }//if else if(line[i]==' '){ i++; } else if(line[i]>='0'&&line[i]<='9'){ token=token+line[i]; i++; bool first_decimal=true; if(i='0'&&line[i]<='9')||(line[i]=='.'&&first_decimal)){ if(line[i]=='.') first_decimal=false; token=token+line[i]; i++; if(i==s) break; }//while }//if box=new node; box->value=token; box->type="NUM"; box->address=line_number; tops.push_back(box); if(token.size()>15){ cout<<"Numerical overflow occured in line "<value=token; box->type="OPE"; box->address=line_number; tops.push_back(box); token=""; }//if else if(line[i]=='<'){ token=token+line[i]; i++; if(i!=s){ if(line[i]=='>'||line[i]=='='){ token=token+line[i]; i++; }//if }//if box=new node; box->value=token; box->type="OPE"; box->address=line_number; tops.push_back(box); token=""; }//if else if(line[i]=='>'){ token=token+line[i]; i++; if(i!=s){ if(line[i]=='='){ token=token+line[i]; i++; }//if }//if box=new node; box->value=token; box->type="OPE"; box->address=line_number; tops.push_back(box); token=""; }//if else if(line[i]==';'||line[i]==','||line[i]==':'){ token=token+line[i]; i++; box=new node; box->value=token; box->type="PUN"; box->address=line_number; tops.push_back(box); token=""; }//if else if(line[i]=='+'||line[i]=='-'||line[i]=='*'){ token=token+line[i]; i++; box=new node; box->value=token; box->type="OPE"; box->address=line_number; tops.push_back(box); token=""; }//if else if(line[i]=='/'){ token=token+line[i]; i++; if(line[i]!='*'){ box=new node; box->value=token; box->type="OPE"; box->address=line_number; tops.push_back(box); token=""; }//if else{ token=token+line[i]; i++; box=new node; box->value=token; box->type="COM"; box->address=line_number; tops.push_back(box); token=""; while(line[i]!='/') i++; token=token+line[i-1]+line[i]; i++; box=new node; box->value=token; box->type="COM"; box->address=line_number; tops.push_back(box); token=""; }//else }//if }//while getline(fin,line); line_number=line_number+1; }//while of fin }//scanner // node::~node(){ // destroy(top); // } /* void node::destroy(nodeptr& top){ nodeptr curr,temp; curr=top; while(curr!=NULL){ temp=curr; curr=curr->next; delete temp; }//while top=NULL; }//destroy */ void main(){ fout.open("c:\\output.txt"); error.open("c:\\error.txt"); node obj; obj.scanner(); nodeptr mynode; string TYPE="Type of token"; string VALUE="Value of token"; string ADDRESS="Address of token(line number)"; cout<type; fout<type; for(int i=0;i<(20-mynode->type.size());i++){ cout<<" "; fout<<" "; }//for cout<value; fout<value; for(int i=0;i<(20-mynode->value.size());i++){ cout<<" "; fout<<" "; }//for cout<address<address<>freeze; }//main()