simple lexical analyzer
Compilation principle course lexical analysis program, written in C++ with relatively simple function implementation, users who need it can expand and modify their own ideas on this basis. Function: Read the code in a txt file under the path set by yourself , and then save the analysis results in another txt file under the path set by yourself , and add the following code:
/* Name: Lexical Analyzer Copyright: Author: ycc Date: 24/03/17 11:24 Description: The lexical analyzer is used to read txt text, where each distinct word must be separated by spaces */ #include<iostream> #include<fstream> using namespace std; intmain() { char a[200]; string b[200]; static int j=0; int i; int count; int flag; ifstream infile("C:/Users/Y/Desktop/new.txt ",ios::in);//Load the input file with your own set path ofstream outfile("C:/Users/Y/Desktop/old.txt",ios::out);//Load the output file with your own set path for(i=0;i<200;i++) { a[i]=infile.get();//The input file is read into the array a[] } for(i=0;i<200;i++)//Start character recognition in array a[] { switch(a[i]) { case '('://Identifier ( { b[j]=a[i]; outfile<<"<1,"<<b[j]<<">"<<endl; } ;break; case ')'://identifier) { b[j]=a[i]; outfile<<"<2,"<<b[j]<<">"<<endl; } ;break; case '+': { if(a[i+1]=='=')//Identifier+= { b[j]=a[i]; b[j]+=a[i+1]; i++; outfile<<"<3,"<<b[j]<<">"<<endl; } else if(a[i+1]=='+')//Identifier++ { b[j]=a[i]; b[j]+=a[i+1]; i++; outfile<<"<22,"<<b[j]<<">"<<endl; } else//Identifier+ { b[j]+=a[i]; outfile<<"<4,"<<b[j]<<">"<<endl; } };break; case '-': { if(a[i+1]=='=')//Identifier-= { b[j]=a[i]; b[j]+=a[i+1]; i++; outfile<<"<5,"<<b[j]<<">"<<endl; } else if(a[i+1]=='-')//Identifier-- { b[j]=a[i]; b[j]+=a[i+1]; i++; outfile<<"<23,"<<b[j]<<">"<<endl; } else//Identifier- { b[j]+=a[i]; outfile<<"<6,"<<b[j]<<">"<<endl; } };break; case '*': { if(a[i+1]=='=')//Identifier*= { b[j]=a[i]; b[j]+=a[i+1]; i++; outfile<<"<7,"<<b[j]<<">"<<endl; } else//Identifier* { b[j]+=a[i]; outfile<<"<8,"<<b[j]<<">"<<endl; } };break; case '/': { if(a[i+1]=='=')//Identifier/= { b[j]=a[i]; b[j]+=a[i+1]; i++; outfile<<"<9,"<<b[j]<<">"<<endl; } else if(a[i+1]=='/')////Identifier// { while(a[i+2]!='\n') { i++; }outfile<<"<10(1),"<<"注释"<<">"<<endl; } else////Identifier/ { b[j]+=a[i]; outfile<<"<10,"<<b[j]<<">"<<endl; } };break; case ';'://identifier; { b[j]=a[i]; outfile<<"<11,"<<b[j]<<">"<<endl; };break; case '<': { if(a[i+1]=='=')//Identifier<= { b[j]=a[i]; b[j]+=a[i+1]; i++; outfile<<"<12,"<<b[j]<<">"<<endl; } else//Identifier< { b[j]+=a[i]; outfile<<"<13,"<<b[j]<<">"<<endl; } };break; case '>': { if(a[i+1]=='=')//Identifier>= { b[j]=a[i]; b[j]+=a[i+1]; i++; outfile<<"<14,"<<b[j]<<">"<<endl; } else//identifier> { b[j]+=a[i]; outfile<<"<15,"<<b[j]<<">"<<endl; } };break; case '=': { if(a[i+1]=='=')//Identifier== { b[j]=a[i]; b[j]+=a[i+1]; i++; outfile<<"<16,"<<b[j]<<">"<<endl; } else//Identifier = { b[j]+=a[i]; outfile<<"<17,"<<b[j]<<">"<<endl; } };break; } if (a[i]==' '||a[i]=='\n') //Identifier spaces and newlines { j++; } if(a[i]>='0'&&a[i]<='9'||a[i]=='.')//Identify decimals and integers { b[j]+=a[i]; if(a[i+1]==' '||a[i+1]=='\n')//Identify decimals, the following statement controls the number of digits { if(a[i-1]=='.'||a[i-2]=='.'||a[i-3]=='.'||a[i-4]=='.'||a[i-5]=='.'||a[i-6]=='.') outfile<<"<18(1),"<<b[j]<<">"<<endl; else outfile<<"<18,"<<b[j]<<">"<<endl;//识别整数 } } if(a[i]>='a'&&a[i]<='z') //Keyword identification, add the required keywords yourself { b[j]+=a[i]; if(a[i+1]==' '||a[i+1]=='\n') { if(b[j]=="while") outfile<<"<19(1),"<<b[j]<<">"<<endl; else if(b[j]=="int") outfile<<"<19(2),"<<b[j]<<">"<<endl; else if(b[j]=="float") outfile<<"<19(3),"<<b[j]<<">"<<endl; else if(b[j]=="do") outfile<<"<19(4),"<<b[j]<<">"<<endl; else if(b[j]=="public") outfile<<"<19(5),"<<b[j]<<">"<<endl; else if(b[j]=="void") outfile<<"<19(6),"<<b[j]<<">"<<endl; else if(b[j]=="if") outfile<<"<19(7),"<<b[j]<<">"<<endl; else outfile<<"<19,"<<b[j]<<">"<<endl;//Identify custom variables } } if(a[i]==':') { if(a[i+1]=='=')//Identifier:= { b[j]=a[i]; b[j]+=a[i+1]; outfile<<"<20,"<<b[j]<<">"<<endl; i++; } else//Identifier : { b[j]+=a[i]; outfile<<"<21,"<<b[j]<<">"<<endl; } } } cout<<"Compilation completed"; //The dos interface is displayed, the lexical recognition is successful outfile.close();//Close the file infile.close();//Close the file return 0; }
The results are shown as follows (I took two screenshots together) :