📄 lexical_analysis.cpp
字号:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define MAX 20
char RW[]="program input output begin end var integer real for to if then else do while write array procedure " ;/* Reserved word */
char ASSIGN[]="+ - * / = > < >= <= <> := .. ";
char BOS[]=", ; : { } [ ] ( ) ";/* Box off the sign */
char EB[]="COMMA SEMI COLON LBRACE RBRACE LMREN RMREN LPAREN RPAREN ";/* Explain for BOS */
int LINE=1,L;
int Judgment(char J[],char B[])
{
int i,j=0,k;
while(J[j]!='\0'){
i=j;
k=0;
while(B[k]==J[i]){
i++;
k++;
}
if((B[k]=='\0')&&(J[i]==' '))
return j;
else
while(J[j++]!=' ');
}
return -1;
}
int Judge_char(char J[],char ch1,char ch2)
{
char O[2];
O[0]=ch1;
O[1]=ch2;
O[2]='\0';
return Judgment(J,O);
}
int Judge_number(char B[])
{
int m=-1,n=0;
while(((B[n]!='\0')&&(B[n]<58)&&(B[n]>47))||(B[n]=='.')){
if(B[n]=='.')
m++;
n++;
}
if((B[n]=='\0')&&(m<=0))
return 1;
else return 0;
}
void fview(FILE *fp,char B[],int n)/* printf B from B[n] to next blank */
{
int i;
char V[10];
for(i=0;B[n]!=' ';i++,n++)
V[i]=B[n];
V[i]='\0';
fprintf(fp,"%s",V);
}
int Get_buffer(FILE *fp,char B[])
{
int i=0;
char ch1,ch2;
ch1=fgetc(fp);
if(ch1==EOF)
return -1;
if((ch1=='\n')||(ch1==9)||(ch1==' ')){
if(ch1=='\n')
LINE++;
return 0;
}
ch2=fgetc(fp);
if((ch2==EOF)||(ch2=='\n')||(ch2==9)||(ch2==' ')){
B[0]=ch1;
B[1]='\0';
fseek(fp,-1L,1);
if(Judge_char(ASSIGN,ch1,'\0')>=0)
return 3;
if(Judge_char(BOS,ch1,'\0')>=0)
return 4;
return 1;
}
else{
fseek(fp,-2L,1);
while((ch1!=' ')&&(ch1!=9)&&(ch1!='\n')){
ch1=fgetc(fp);
ch2=fgetc(fp);
B[i]=ch1;
B[i+1]=ch2;
i++;
if(ch2==EOF)
return 1;
if((ch1=='/')&&(ch2=='/')){
B[i-1]='\0';
while((ch2!='\n')&&(ch2!=EOF))
ch2=fgetc(fp);
if(ch2=='\n')
fseek(fp,-1L,1);
return 1;
}/* delete "//......" */
if((ch1=='/')&&(ch2=='*')){
B[i-1]='\0';
L=LINE;
while(ch2!=EOF){
ch1=fgetc(fp);
ch2=fgetc(fp);
fseek(fp,-1L,1);
if(ch1=='\n')
LINE++;
if((ch1=='*')&&(ch2=='/')){
fseek(fp,1L,1);
return 1;
}
}// delete "/*...*/"
return -2;
}
fseek(fp,-1L,1);
if(Judge_char(ASSIGN,ch1,ch2)>=0){
B[i]=ch2;
B[i+1]='\0';
fseek(fp,1L,1);
return 2;
}
if(Judge_char(ASSIGN,ch1,'\0')>=0){
B[i]='\0';
return 3;
}
if(Judge_char(BOS,ch1,'\0')>=0){
B[i]='\0';
return 4;
}
}/* end while */
if(ch1=='\n')
fseek(fp,-1L,1);
B[i-1]='\0';
return 1;
}/* end else */
} /* -1:end -2:error 0:jump this time 1:get buffer 2-4:get sign */
void Print_result(FILE *fp,char B[],int n)
{
int i=0,line;
char ch1,ch2;
char V[10];
while(B[i]!='\0')
i++;
ch1=B[i-1];
ch2=B[i-2];
if(B[i+1]=='*')
line=L;
else
line=LINE;
switch(n){
case 1:{
if(B[0]=='\0')
break;
if(Judgment(RW,B)>=0){
fprintf(fp," <token lineno=%d string=\"%s\" type=%s ></token>\n",line,B,B);
}
else if(Judge_number(B)&&(B[0]!='.'))
fprintf(fp," <token lineno=%d string=\"%s\" type=NUMBER ></token>\n",line,B);
else if(B[0]!='.')
fprintf(fp," <token lineno=%d string=\"%s\" type=ID ></token>\n",line,B);
break;
}
case 2:{
B[i-2]='\0';
Print_result(fp,B,1);
fprintf(fp," <token lineno=%d string=\"%c%c\" type=ASSIGN ></token>\n",LINE,ch2,ch1);
break;
}
case 3:{
B[i-1]='\0';
Print_result(fp,B,1);
fprintf(fp," <token lineno=%d string=\"%c\" type=ASSIGN ></token>\n",LINE,ch1);
break;
}
case 4:{
B[i-1]='\0';
Print_result(fp,B,1);
fprintf(fp," <token lineno=%d string=\"%c\" type=",LINE,ch1);
fview(fp,EB,Judge_char(BOS,ch1,'\0')/2*7);
fprintf(fp," ></token>\n");
break;
}
}
}
int Lexical_analysis()
{
FILE *fp1,*fp2;
int ES;
char ch;
char filename[10],buffer1[MAX],buffer2[MAX];
printf("Enter name of the program file? ");
gets(filename);
if((fp1=fopen(filename,"a"))==NULL){
printf("Can not open file!\n");
return 0;
}
fputc('/',fp1);
fputc('/',fp1);
fclose(fp1);
fp1=fopen(filename,"r");
fseek(fp1,-3L,2);
if(fgetc(fp1)!='.'){
printf("Program not ended by '.'!");
fclose(fp1);
return 0;
}
fseek(fp1,0L,0);
printf("Enter name of the result file:");
scanf("%s",&filename);
fp2=fopen(filename,"a");
fprintf(fp2,"<?xml version=\"1.0\"?>\n<root>\n");
do{
ES=Get_buffer(fp1,buffer1);
if(ES&&(ES!=-1)&&(buffer1[0]!='\0'))
Print_result(fp2,buffer1,ES);
}while(ES>=0);
if(ES==-2){
printf("Error in line %d: Explain dismatch!",L);
Print_result(fp2,buffer1,1);
fprintf(fp2,"</root>\nError in line %d: Explain dismatch!",L);
}
else
fprintf(fp2,"</root>\n");
fclose(fp1);
fclose(fp2);
}
main()
{
Lexical_analysis();
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -