关键字 |
种别码 |
|
关键字 |
种别码 |
main |
0 |
|
return |
32 |
auto |
1 |
|
变量 |
33 |
short |
2 |
|
int常量 |
34 |
int |
3 |
|
实型常量 |
35 |
long |
4 |
|
char常量 |
36 |
float |
5 |
|
= |
37 |
double |
6 |
|
+ |
38 |
char |
7 |
|
- |
39 |
struct |
8 |
|
* |
40 |
union |
9 |
|
/ |
41 |
enum |
10 |
|
++ |
42 |
typedef |
11 |
|
-- |
43 |
const |
12 |
|
+= |
44 |
usigned |
13 |
|
-= |
45 |
signed |
14 |
|
*= |
46 |
extern |
15 |
|
/= |
47 |
register |
16 |
|
== |
48 |
static |
17 |
|
!= |
49 |
volatile |
18 |
|
> |
50 |
void |
19 |
|
< |
51 |
if |
20 |
|
>= |
52 |
else |
21 |
|
<= |
53 |
switch |
22 |
|
( |
54 |
case |
23 |
|
) |
55 |
for |
24 |
|
[ |
56 |
do |
25 |
|
] |
57 |
while |
26 |
|
{ |
58 |
goto |
27 |
|
} |
59 |
continue |
28 |
|
, |
60 |
break |
29 |
|
: |
61 |
default |
30 |
|
; |
62 |
sizeof |
31 |
|
|
|
C语言词法分析器
保留字表:
"auto","short","int","long","float","double","char","struct"
,"union","enum","typedef","const","unsigned","signed","extern","register"
,"static","volatile","void","if","else","switch","case","for"
,"do","while","goto","continue","break","default","sizeof","return"};
"=","+","-","*","/","++","--","+=","-=",
"*=","/=","==","!=",">","<",">=","<=","(",
")","[","]","{","}",",",":",";" };
for(int i=0;i<26;i++)
{
if(str==symbol[i]) return i+37;
}
return -1;
ch=text[pText];
pText++;
while(1)
{
if(ch==' ') GetChar();
else break;
}
strToken+=ch;
if( ( ch>='a' &&ch<='z') ||( ch>='A' &&ch<='Z') )
return true;
return false;
if( ch>='0' &&ch<='9')
return true;
return false;
for(int i=0;i<=33;i++)
{
if(strToken==key[i]) return i;
}
return -1;
ch=' ';
pText--;
int code;
strToken="";
GetChar();
GetBC();
if(IsLetter()) ///识别标示符和关键字
{
while(IsLetter() || IsDigit() ||ch=='_')
{
Concat();
GetChar();
}
Retract();
code=Reserve();
if(code!=-1) ///识别出来的是常量
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
else ///识别出来的是标示符
cout<<"("<<33<<","<<strToken<<",line="<<line<<")"<<endl;
}
else if(IsDigit()) ///识别整形常量、浮点型常量
{
while(IsDigit())
{
Concat();
GetChar();
}
if(ch!='.') ///识别整形常量
{
Retract();
int temp=atoi(strToken.c_str());
///---------------------二进制显示----------------
int a[128],i;
for(i=0; ;i++)
{
if(temp<=0) break;
a[i]=temp%2;
temp/=2;
}
cout<<"("<<34<<",";
for(int j=i-1;j>=0;j--)
{
cout<<a[j];
}
cout<<",line"<<line<<")"<<endl;
}
else if(ch=='.') ///识别实型常量
{
Concat();
GetChar();
while(IsDigit())
{
Concat();
GetChar();
}
Retract();
cout<<"("<<35<<","<<strToken<<",line="<<line<<")"<<endl;
}
}
else if(ch==39) ///识别字符常量
{
Concat();
GetChar();
while(ch!=39)
{
Concat();
GetChar();
}
cout<<"("<<36<<","<<strToken<<"',line="<<line<<")"<<endl;
}
else if(ch=='=') ///识别=、==
{
Concat();
GetChar();
int code;
if(ch=='=')
{
Concat();
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
}
else
{
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
Retract();
}
}
else if(ch=='+') ///识别+、+=、++
{
Concat();
GetChar();
int code;
if(ch=='=')
{
Concat();
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
}
else if(ch=='+')
{
Concat();
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
}
else
{
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
Retract();
}
}
else if(ch=='-') ///识别-、-=、--
{
Concat();
GetChar();
int code;
if(ch=='=')
{
Concat();
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
}
else if(ch=='-')
{
Concat();
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
}
else
{
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
Retract();
}
}
else if(ch=='*') ///识别*、*=
{
Concat();
GetChar();
int code;
if(ch=='=')
{
Concat();
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
}
else
{
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
Retract();
}
}
else if(ch=='/') ///识别/、/=
{
Concat();
GetChar();
int code;
if(ch=='=')
{
Concat();
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
}
else
{
code=FindInSymbol(strToken);
cout<<"("<<code<<","<<strToken<<",line="<<line<<")"<<endl;
Retract();
}
}
else if(ch==';') cout<<"("<<34<<",;"<<",line="<<line<<")"<<endl;
else if(ch=='(') cout<<"("<<26<<",("<<",line="<<line<<")"<<endl;
else if(ch==')') cout<<"("<<27<<",)"<<",line="<<line<<")"<<endl;
else if(ch=='{') cout<<"("<<30<<",{"<<",line="<<line<<")"<<endl;
else if(ch=='}') cout<<"("<<31<<",}"<<",line="<<line<<")"<<endl;
else if(ch==',') cout<<"("<<32<<",,"<<",line="<<line<<")"<<endl;
else if(ch=='\n') line++;
cout<<"\t\t---------从文件中读------>1----"<<endl;
cout<<"\t\t---------默 认 输入------>2----"<<endl;
cout<<"\t\t---------退 出------>0----"<<endl;
int choice;
while(1)
{
cin>>choice;
if(choice==1)
{
fstream data;
data.open("D:\data.txt");
text.clear();
char temp;
while(1)
{
if(data.eof()) break;
data.get(temp);
text+=temp;
}
cout<<"要分析的C程序内容:"<<endl;
cout<<text<<endl;
break;
}
else if(choice==2)
{
cout<<"要分析的C程序内容:"<<endl;
cout<<text<<endl;
break;
}
else if(choice==0)
exit(0);
}
choice();
while(pText<text.length())
{
Analyse();
}
cout<<strToken;
return 0;
源代码:
#include <iostream>
#include<math.h>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<fstream>
using namespace std;
string key[34]={"main",
string symbol[26]={
char ch; ///存放最新读进的源程序字符
string strToken; ///存放构成单词的字符串
string text="int main()\n{\n int a,b;\n a=10.3211;\n a+=20; if (b==15) break; \n float fa=3.141592653; \n char s='z';\n}"; ///要读进的文本
//string text="main(){ int a,b; a=10; b=a+20;}"; ///要读进的文本
int pText=0; ///搜索指示器
int line=1;
int FindInSymbol(string str)
{
}
void GetChar() ///---------1
{
}
void GetBC() ///-------2
{
}
void Concat() ///----------3
{
}
bool IsLetter() ///---------4
{
}
bool IsDigit() ///---------5
{
}
int Reserve() ///---------6
{
}
void Retract() ///----------7
{
}
void Analyse()
{
}
void choice()
{
}
int main()
{
}