PL/0 词法分析器
1 #include<stdio.h>
2 #include <ctype.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 typedef enum SymEnum
7 {
8 Identifier=0, //标识符
9 Const=1, //常数
10 Key=2, //关键字
11 Operator=3, //运算符
12 Delimiter=4 //界符
13 } SymEnum;
14
15 char KeyWord[13][15]; //关键字
16
17 void Init()
18 {
19 strcpy(&KeyWord[0][0],"begin");
20 strcpy(&KeyWord[1][0],"call");
21 strcpy(&KeyWord[2][0],"const");
22 strcpy(&KeyWord[3][0],"do");
23 strcpy(&KeyWord[4][0],"end");
24 strcpy(&KeyWord[5][0],"if");
25 strcpy(&KeyWord[6][0],"odd");
26 strcpy(&KeyWord[7][0],"procedure");
27 strcpy(&KeyWord[8][0],"read");
28 strcpy(&KeyWord[9][0],"then");
29 strcpy(&KeyWord[10][0],"var");
30 strcpy(&KeyWord[11][0],"while");
31 strcpy(&KeyWord[12][0],"write");
32 }
33
34 //判断一个单词是否为关键字
35 int IsKeyWord(char *word)
36 {
37 int i=0,j=12;
38 while(i<=j)
39 {
40 int k=(i+j)/2;
41 if(strcmp(word,KeyWord[k])<=0)
42 j=k-1;
43 if(strcmp(word,KeyWord[k])>=0)
44 i=k+1;
45 }
46 return i-1>j ? 1 : 0;
47 }
48
49 //判断一个单词是否为界符
50 int IsDelimiter(char word)
51 {
52 if(word==','||word==';'||word=='.'||word=='('||word==')')
53 return 1;
54 return 0;
55 }
56
57 //判断一个单词是否为操作符
58 int IsOperator(char *word)
59 {
60 if(word[0]=='+'||word[0]=='-'||word[0]=='*'||word[0]=='/'||word[0]=='<'||word[0]=='>'||strcmp(word,":=")==0||strcmp(word,">=")==0||strcmp(word,"<=")==0||word[0]=='#'||word[0]=='=')
61 return 1;
62 return 0;
63 }
64
65 //判断一个单词是否是常数
66 int IsConst(char *word)
67 {
68 if(word[0]>='0'&&word[0]<='9')
69 return 1;
70 return 0;
71 }
72
73 //判断连个字符是否属于相同类型
74 int IsSame(char f,char s)
75 {
76 int bf = (f>='0'&&f<='9'||f>='a'&&f<='z'||f>='A'&&f<='Z')? 1 : 0 ;
77 int bs = (s>='0'&&s<='9'||s>='a'&&s<='z'||s>='A'&&s<='Z')? 1 : 0 ;
78 return bf == bs;
79 }
80
81 //判断 word 的类型
82 SymEnum TypeOfWord(char *word)
83 {
84 if(IsKeyWord(word))
85 return Key;
86 if(IsConst(word))
87 return Const;
88 if(IsOperator(word))
89 return Operator;
90 return Identifier;
91 }
92
93 int GetSym()
94 {
95 FILE *fp,*fout;
96 if((fp=fopen("PL0.txt","r"))==NULL || (fout=fopen("source.txt","w+"))==NULL)
97 {
98 printf("Open Error!
");
99 exit(0);
100 }
101 SymEnum SYM; //存放每个单词的类别,用内部编码形式表示;
102 char word[50]; //存储单词
103 word[0]='