博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
0916 编译原理第二次上机作业
阅读量:5090 次
发布时间:2019-06-13

本文共 1984 字,大约阅读时间需要 6 分钟。

#include <stdio.h>                 

#include <string.h>               /*定义字符串库函数*/

#include <conio.h>                  /*提供有关屏幕窗口操作函数*/

#include <ctype.h>                  /*分类函数*/

char prog[80]={'\0'},

     token[8];                     /*存放构成单词符号的字符串*/

char ch;

int syn,                           /*存放单词字符的种别码*/

      n,

    sum,                           /*存放整数型单词*/

    m,p;                           /*p是缓冲区prog的指针,m是token的指针*/

char *rwtab[6]={"begin","if","then","while","do","end"};

void scaner(){

    m=0;

    sum=0;

    for(n=0;n<8;n++)

        token[n]='\0';

    ch=prog[p++];

    while(ch==' ')

        ch=prog[p++];

    if(isalpha(ch))    /*ch为字母字符*/{

        while(isalpha(ch)||isdigit(ch))    /*ch 为字母字符或者数字字符*/{

           token[m++]=ch;

           ch=prog[p++];}

        token[m++]='\0';

        ch=prog[p--];

        syn=10;

        for(n=0;n<6;n++)

            if(strcmp(token,rwtab[n])==0)    /*字符串的比较*/{

                syn=n+1;

                break;}}

    else

        if(isdigit(ch))    /*ch是数字字符*/{

            while(isdigit(ch))    /*ch是数字字符*/{

                sum=sum*10+ch-'0';

                ch=prog[p++];}

            ch=prog[p--];

            syn=11;}

        else

            switch(ch){

                case'<':m=0;token[m++]=ch;ch=prog[p++];

                        if(ch=='>'){

                            syn=21;

                            token[m++]=ch;}

                        else if(ch=='='){

                            syn=22;

                            token[m++]=ch;}

                            else{

                                 syn=20;

                                 ch=prog[p--];}

                        break;

                case'>':m=0;token[m++]=ch;ch=prog[p++];

                        if(ch=='='){

                            syn=24;

                            token[m++]=ch;}

                        else{

                            syn=23;

                            ch=prog[p--];}

                        break;

             case':':m=0;token[m++]=ch;ch=prog[p++];

                     if(ch=='='){

                         syn=18;

                         token[m++]=ch;}

                     else{

                         syn=17;

                         ch=prog[p--];}

                     break;

             case'+':syn=13;token[0]=ch;break;

             case'-':syn=14;token[0]=ch;break;

             case'*':syn=15;token[0]=ch;break;

             case'/':syn=16;token[0]=ch;break;

             case'=':syn=25;token[0]=ch;break;

             case';':syn=26;token[0]=ch;break;

             case'(':syn=27;token[0]=ch;break;

             case')':syn=28;token[0]=ch;break;

             case'#':syn=0;token[0]=ch;break;

             default:syn=-1;}}

main()

{

    printf("\n\nThe significance of the figures:\n"

           "1.figures 1 to 6 said Keyword\n"

           "2.figures 10 and 11 said Other indicators\n"

           "3.figures 13 to 28 said Operators\n");

    

p=0;

    printf("\nplease input string:\n");

    do {

           ch=getchar();

           prog[p++]=ch;

       }while(ch!='#');

     

p=0;

    do{

        scaner();

        switch(syn){

            case 11: printf("(%d,%d)\n",syn,sum);break;

            case -1: printf("\n ERROR;\n");break;

            default: printf("(%d,%s)\n",syn,token);

}

     }while(syn!=0);

    getch();

}

转载于:https://www.cnblogs.com/alfredzhu/p/4826929.html

你可能感兴趣的文章
发布我制作的jQuery贪吃蛇游戏
查看>>
.net操作AD域
查看>>
hash算法
查看>>
ashx.cs 读写session
查看>>
Java版本
查看>>
LINUX手动查看和修改MTU值的方法
查看>>
Linux buffer/cache异同
查看>>
MySQL数据库my.cnf性能参数如何调优
查看>>
特征预处理
查看>>
Setup Apache2 in Debian 9 and enable two ports for two sites
查看>>
商品的价格梯度
查看>>
图形学-剔除
查看>>
人生哲学
查看>>
JAVA调用.NET的WEBSERVICE
查看>>
Selenium+Python浏览器调用:Firefox
查看>>
nohup 详解
查看>>
树莓派实现摄像头监控(使用motion和mjpg-streamer)
查看>>
《转》推荐系统经典论文文献及业界应用
查看>>
webpack的像素转vw单位的loader插件
查看>>
javascript高级程序设计一书----关于创建和对象继承的总结
查看>>