?? 編譯pl0詞法分析器 .cpp
字號:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>
//********************* 變量定義 **************************************
#define al 10
#define norw 13
char *SourceFile;
int SourceFLength;
int CurrentPosition;
char ch;
char a[al+1];
char sym[30];
char word[norw+1][al+1]={" ","begin ","call ","const ",
"do ","end ","if ",
"odd ","procedure ","read ",
"then ","var ","while ",
"write "};
//********************* 成員函數定義 ********************************
void Getche( )
{
CurrentPosition++;
if (CurrentPosition>=SourceFLength)
{
cout<<endl<<"PL/0 詞法分析結束!"<<endl;
exit(1) ;
}
ch=SourceFile[CurrentPosition];
}
void getsym()
{
//----------------------------------------------------------保留字
while (ch==' '||ch=='\r') Getche();
int k;
int kk=al;
if ((ch>='a')&&(ch<='z'))
{
k=-1;
do
{
if (k<al)
{ k++;
a[k]=ch;
}
Getche();
}
while ((ch>='a')&&(ch<='z')||(ch>='0')&&(ch<='9'));
if (k>=kk-1)
kk=k;
else
do
{
a[kk-1]=' ' ;
kk-- ;
}
while (kk-1>k);
a[al]='\0';
int i,j;
char id[al+1];
i=1;j=norw;
strcpy(id,a);
do
{
k=(i+j)/2;
if (strcmp(id,word[k])<=0)
j=k-1;
if (strcmp(id,word[k])>=0)
i=k+1;
}
while (i<=j);
if (i-1>j)
strcpy(sym,"保留字");
else
strcpy(sym,"ident");
printf("%-10s",sym);
cout<<a<<endl;
return;
}
//-------------------------------------------------------------常數
if ((ch>='0')&&(ch<='9'))
{
char shu[2];
k=0;
int num=0;
strcpy(sym,"常數");
do
{
shu[0]=ch;
shu[1]='\0';
num=10*num+atoi(shu);
k++;
Getche();
}
while ((ch>='0')&&(ch<='9'));
if(k<13)
{ printf("%-10s",sym);
cout<<num<<endl; }
else
cout<<"常數太大!!!"<<endl;
return;
}
//-------------------------------------------------------------運算符
if (ch==':')
{
Getche();
if (ch=='=')
{ cout<<"運算符 :="<<endl;
Getche();
}
else
cout<<"未定義 :"<<endl;
return;
}
if (ch=='>')
{
Getche();
if (ch=='=')
{ cout<<"運算符 >="<<endl;
Getche();
}
else
cout<<"運算符 >"<<endl;
return;
}
if (ch=='<')
{
Getche();
if (ch=='=')
{ cout<<"運算符 <="<<endl;
Getche();
}
else
cout<<"運算符 <"<<endl;
return;
}
//-------------------------------------------------------------注釋
if (ch=='/')
{ Getche();
if (ch=='/')
{ while (ch!='\r') Getche();}
else
if (ch=='*')
{
while(1)
{ Getche();
if (ch=='*')
{ Getche();
if (ch=='/')
break;
}
}
Getche();
}
else
cout<<"運算符 /"<<endl;
return;
}
//--------------------------------------------------------運算符+-*/
if ((ch=='=')||(ch=='+')||(ch=='-')||(ch=='*'))
{
cout<<"運算符 "<<ch<<endl;
Getche();
return;
}
//--------------------------------------------------------界符
if ((ch=='.')||(ch==',')||(ch==';')||(ch=='(')||(ch==')'))
{
cout<<"界符 "<<ch<<endl;
Getche();
return;
}
//--------------------------------------------------------其他符號
cout<<"未定義 "<<ch<<endl;
Getche();
}
//***********************************************************主函數
void main()
{ char c;
do {
cout<<"請輸入被詞法分析的程序源代碼:(按CTRL+Z結束)"<<endl;
SourceFile=(char *) malloc(sizeof(char)*500);
int index=0;
while(1)
{ ch=getche();
if (ch=='\r') // 回車鍵
puts(""); //換行
if (ch==26 ) // Ctrl+z
{
puts("\n程序源代碼輸入完畢!!!\n");
break;
}
if (index<500)
{ SourceFile[index]=ch;
index++;
}
else
{ printf(" \nOut of buffer within 500 byte!\n ");
break;
}
}
SourceFile[index]='\0';
SourceFLength=strlen(SourceFile);
if (SourceFLength<1)
{
printf("退出程序!\n");
exit(1);
}
CurrentPosition=-1;
ch=' ';
printf("\n");
while(1)
getsym();
free(SourceFile);
cout<<"是否還要再輸新的程序分析???(是的話按Y或y)"<<endl;
cin>>c;
}while(c=='Y'&&c=='y');
}
//例如程序源代碼
//gets(SourceFile);
//SourceFile=" const a=35,b=49 ;
//var c,d,e;
//procedure p;
//var g ;
//begin
//g = a*b
//end
//begin d= a+b;
//if d>=a then c=a end ";
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -