05 词法分析程序的设计与实验

参考文档:

https://blog.csdn.net/qq_41386300/article/details/83270557

https://blog.csdn.net/yang03_26/article/details/80841670

词法分析程序(Lexical Analyzer)要求:

- 从左至右扫描构成源程序的字符流

-  识别出有词法意义的单词(Lexemes

-  返回单词记录(单词类别,单词本身)

-  滤掉空格

-  跳过注释

-  发现词法错误

程序结构:

输入:字符流(什么输入方式,什么数据结构保存)

处理:

–遍历(什么遍历方式)

–词法规则

输出:单词流(什么输出形式)

–二元组

单词类别:

1.标识符(10)

2.无符号数(11)

3.保留字(一词一码)

4.运算符(一词一码)

5.界符(一词一码)

单词符号

种别码

单词符号

种别码

begin

1

:

17

if

2

:=

18

then

3

<

20

while

4

<=

21

do

5

<>

22

end

6

>

23

l(l|d)*

10

>=

24

dd*

11

=

25

+

13

;

26

-

14

(

27

*

15

)

28

/

16

#

0

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 void match();  //匹配的方法
  5 char temp[500],test[500];
  6 char ch;
  7 const char *keyword[6]= {"begin","if","then","while","do","end"};   //存储保留字
  8 int i=0,num,n;
  9 
 10 int main() {
 11 
 12     printf("	
请输入程序,以#号结束:
");
 13     ch=getchar();
 14     while(ch!='#') {
 15         temp[i]=ch;
 16         ch=getchar();
 17         i++;
 18     }
 19     
 20     temp[i]='#';
 21     i++;
 22     temp[i]='';
 23     i=0;
 24     match();
 25     
 26     while(num!=0){
 27         match();
 28     }
 29     printf("
词法分析完毕!");
 30 }
 31 
 32 void match() {
 33     ch=temp[i++];
 34     while(ch==' ') {
 35         ch=temp[i++];
 36     }
 37 
 38     if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) {        //保留字与标识符判断 
 39         n=0;
 40         num=10;
 41         char a=ch;
 42         
 43         do {
 44             test[n++]=ch;
 45             ch=temp[i++];
 46         }while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9'));     //字符串数组复制
 47         
 48         for(n=0; n<6; n++) {
 49             if(strcmp(test,keyword[n])==0) {  //判断两个字符串是否一致
 50                 num=n+1;   
 51                 printf("[%s,%d]
",test,num);
 52             }
 53         }
 54         
 55         i--;
 56         
 57         if(num==10) {
 58             ch=a;
 59             printf("[%c,%d]
",ch,num);
 60         }
 61     }
 62 
 63     if(ch>='0'&&ch<='9') {    //数字判断 
 64         num=11;
 65         printf("[%c,%d]
",ch,num);
 66     } 
 67     else { 
 68         switch(ch) {    //运算符界符的判断 
 69             case '#':num=0;printf("[%c,%d]
",ch,num);break;
 70             case '+':num=13;printf("[%c,%d]
",ch,num);break;
 71             case '-':num=14;printf("[%c,%d]
",ch,num);break;
 72             case '*':num=15;printf("[%c,%d]
",ch,num);break;
 73             case '/':num=16;printf("[%c,%d]
",ch,num);break;
 74             case ':':
 75                 ch=temp[i++];
 76                 if(ch=='=') {
 77                     num=18;
 78                     printf("[:%c,%d]
",ch,num);
 79                 } 
 80                 else {
 81                     i--;
 82                     ch=temp[i];
 83                     num=17;
 84                     printf("[%c,%d]
",ch,num);
 85                 }break;
 86                 
 87             case '<':
 88                 ch=temp[i++];
 89                 if(ch=='=') {
 90                     num=21;
 91                     printf("[<%c,%d]
",ch,num);
 92                 } else if(ch='>') {
 93                     num=22;
 94                     printf("[<%c,%d]
",ch,num);
 95                 } else {
 96                     i--;
 97                     ch=temp[i];
 98                     num=20;
 99                     printf("[%c,%d]
",ch,num);
100                 }break;
101                 
102             case '>':
103                 ch=temp[i++];
104                 if(ch=='=') {
105                     num=24;
106                     printf("[>%c,%d]
",ch,num);
107                 } else {
108                     i--;
109                     ch=temp[i];
110                     num=23;
111                     printf("[%c,%d]
",ch,num);
112                 }
113                 break;
114             case '=':num=26;printf("[%c,%d]
",ch,num);break;
115             case ';':num=27;printf("[%c,%d]
",ch,num);break;
116             case '(':num=27;printf("[%c,%d]
",ch,num);break;
117             case ')':num=28;printf("[%c,%d]
",ch,num);break;
118         }
119     }
120 }  

 

原文地址:https://www.cnblogs.com/HvYan/p/11656489.html