• 1022词法分析程序源代码


    #include <stdio.h>
    #include <string.h>

    char prog[80],token[8],ch; //token是存放的字符串 ch是字符 prog是缓冲区
    int syn,p,m,n,sum; //syn是种别码 p是缓冲区prog的指针 m是token的指针 n是指针数组rwtab的下标 sum是存放整数型的字
    char *rwtab[6]={"begin","if","then","while","do","end"}; //定义关键字

    void scaner(void) ;
    main()
    {
    p=0;
    printf(" Please input the source code: ");

    do{
    scanf("%c",&ch); //储存输入代码
    prog[p++]=ch;
    }while(ch!='#');

    p=0;
    do{
    scaner();
    switch(syn)
    {
    case 11:
    printf("( %d,%d ) ",syn,sum);
    break;

    case -1:
    printf("you have input a wrong string ");
    return 0;
    break;

    default:
    printf("( %d,%s ) ",syn,token);
    break;
    }
    }while(syn!=0);

    printf(" Over. ");
    }

    void scaner(void)
    {
    sum=0;

    for(m=0;m<8;m++)
    token[m++]= NULL;

    ch=prog[p++];
    m=0;

    while((ch==' ')||(ch==' '))
    ch=prog[p++];

    if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
    {
    while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9')))
    {
    token[m++]=ch; //除6个关键字之外的字符的处理
    ch=prog[p++];
    }

    p--;
    syn=10;

    for(n=0;n<6;n++)
    if(strcmp(token,rwtab[n])==0) //判断是哪个关键字
    {
    syn=n+1;
    break;
    }
    }
    else if((ch>='0')&&(ch<='9'))
    {
    while((ch>='0')&&(ch<='9'))
    {
    sum=sum*10+ch-'0';
    ch=prog[p++];
    }
    p--;
    syn=11;
    }
    else
    {
    switch(ch)
    {
    case '<':
    token[m++]=ch;
    ch=prog[p++];
    if(ch=='=')
    {
    syn=22;
    token[m++]=ch;
    }
    else
    {
    syn=20;
    p--;
    }
    break;

    case '>':
    token[m++]=ch;
    ch=prog[p++];
    if(ch=='=')
    {
    syn=24;
    token[m++]=ch;
    }
    else
    {
    syn=23;
    p--;
    }
    break;

    case '+':
    token[m++]=ch;
    ch=prog[p++];
    if(ch=='+')
    {
    syn=17;
    token[m++]=ch;
    }
    else
    {
    syn=13;
    p--;
    }
    break;

    case '-':
    token[m++]=ch;
    ch=prog[p++];
    if(ch=='-')
    {
    syn=29;
    token[m++]=ch;
    }
    else
    {
    syn=14;
    p--;
    }
    break;

    case '=':
    token[m++]=ch;
    ch=prog[p++];
    if(ch=='=')
    {
    syn=25;
    token[m++]=ch;
    }
    else
    {
    syn=18;
    p--;
    }
    break;

    case '*': syn=15;token[m++]=ch; break;
    case '/': syn=16;token[m++]=ch; break;
    case '(': syn=27;token[m++]=ch; break;
    case ')': syn=28;token[m++]=ch; break;
    case ';': syn=26;token[m++]=ch; break;
    case '#': syn=0;token[m++]=ch; break;
    case ':': syn=17;token[m++]=ch; break;
    default: syn=-1; break;
    }
    }
    token[m++]='';
    }

  • 相关阅读:
    windows安装psycopg2问题解决方案
    error: Setup script exited with error: Unable to find vcvarsall.bat
    python web server comparison
    重置mysql root密码
    Nginx + uWSGI + web.py 搭建示例
    《凉州曲》——吴践道
    C#代理服务器
    OpenGL 分形 Sierpinski镂垫
    Chap02_递归与分治_判断两棵二叉树是否同构
    OpenGL C#绘图环境配置
  • 原文地址:https://www.cnblogs.com/luliqin/p/4902386.html
Copyright © 2020-2023  润新知