编译原理 五

此程序要逐个检查运行情况,并能当场补全代码。

词法分析程序(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

 源代码如下

#include<stdio.h>
#include"stdlib.h"
#include <string.h>
char* keyword[6]={"begin","if","then","while","do","end"};
int a[99];
char b[20];
int num=0;
bool character(char c)
{
    if(((c<='z')&&(c>='a'))||((c<='Z')&&(c>='A')))
    {
        return true; 
    }
    else
    return false;
}
bool number(char c)
{
    if((c<='9')&&(c>='0'))
    {
        return true; 
    }
    else
    return false;
}
int symbol(char *c)
{

        if(strcmp("+",c)==0)
            return 13;
        if(strcmp("-",c)==0)
            return 14;
        if(strcmp("*",c)==0)
            return 15;
        if(strcmp("/",c)==0)
            return 16;
        if(strcmp(":",c)==0)
            return 17;
        if(strcmp(":=",c)==0)
            return 18;
        if(strcmp(">",c)==0)
            return 20;
        if(strcmp("<=",c)==0)
            return 21;
        if(strcmp("<>",c)==0)
            return 22;
        if(strcmp("<",c)==0)
            return 23;
        if(strcmp(">=",c)==0)
            return 24;
        if(strcmp("=",c)==0)
            return 25;
        if(strcmp(";",c)==0)
            return 26;
        if(strcmp("(",c)==0)
            return 27;
        if(strcmp(")",c)==0)
            return 28;
    return 404;
}
void empty(char *c)
{
     memset(c, 0, 20);
}
int main()
{
    FILE *fp;
    if((fp=fopen("C:\Users\15108\Desktop\test.c","r"))==NULL)
    {
        printf("文件读入错误!");
        exit(0);
    }
    char c[20];
    char n[20];
    char f[20];
    char temp[1];
    empty(f);
    empty(c);
    empty(n);
    int x=0;
    int y=0;
    int z=0;
    char ch;

        do{
            ch=fgetc(fp);
            temp[0]=ch;
            int find=symbol(temp);
            if(find!=404)
            {
                f[z]=ch;
                z++;
            }else if(z!=0){
                int result=symbol(f);
                printf("<%d,%s>
",result,f);    
                z=0;
                empty(f);
            }
            if((character(ch)) || ((x!=0) &&(number(ch))))
            {
                c[x]=ch;
                x++;
            }else if(x!=0){
                for(int i=0;i<6;i++)
                {
                    if(strcmp(keyword[i],c)==0)
                    {
                        printf("<%d,%s>
",i+1,c);
                        x=0;
                        empty(c);
                        break;
                        
                    }else if(i==5)
                    {
                        printf("<%d,%s>
",10,c);
                        x=0;
                        empty(c);
                        break;    
                    }
                }
            }
        if((number(ch)) && (x==0))
        {
                n[y]=ch;
                y++;        
        }
        else if((x==0) &&(y!=0) &&(!number(ch))){
            printf("<%d,%s>
",11,n);
            y=0;
            empty(n);    
        }
        
        
        
        }while(!(ch=='#'));
        
        
        
}

测试文本

if(x>5)
	x=10;
	y++;	
then
	x=x/10;
#
	

结果截图

原文地址:https://www.cnblogs.com/huangwenshuo/p/11648618.html