插你个值
mingw opengl 环境配置笔记

编译原理实验一 PL/0语言词法分析

scturtle posted @ 2010年10月02日 23:21 in 各种实验 , 2710 阅读

代码冗长但不恶心,自我感觉良好~

有问题再改,电脑准备拿去修,贴上来备份

#include <cstdio>
#include <cstring>
#include <iostream>
#define ERROR -1
#define ID 1
#define INT 2
#define PLUS 3
#define REDUCE 4
#define MUTIL 5
#define DIV 6
#define COMMA 7
#define SEMICOLON 8
#define LPAR 9
#define RPAR 10
#define LBRACE 11
#define RBRACE 12
#define SET 14
#define BIGGER 15
#define BE 16
#define LESS 17
#define LE 18
#define EQUAL 19
#define NOTEQUAL 20
using namespace std;
char symname[30][10]={"ERROR","ID","INT","PLUS","REDUCE","MUTIL","DIV","COMMA","SEMICOLON","LPAR",
                    "RPAR","LBRACE","RBRACE","","SET","BIGGER","BE","LESS","LE","EQUAL","NOTEQUAL"};
char keywords[13][10]={"CONST","VAR","procedure","begin","end","ood","if","then","call","while","do","read","write"};
char ids[100][20];int idcnt;//变量表
char ints[100][20];int intcnt;//常数表
int sym[1000];int symcnt;int symid[1000];//符号表
bool back;//回溯标志
char ch;int pos,code,value;
char strtoken[20];

char getChar()
{
    if(back) { back=0; return ch;}
    else return getchar();
}
void retract(){back=1;}
void getbc()//跳过空白符
{
    while(ch!=EOF&&ch!='.'&&isspace(ch)) ch=getchar();
}
int reserve()//查关键字表 没有返回0
{
    for(int i=0;i<13;i++)
        if(strcmp(keywords[i],strtoken)==0) return i+30;
    return 0;
}
//插入变量表或常量表
int insertId()
{
    for(int i=0;i<idcnt;i++) if(!strcmp(ids[i],strtoken)) return i;
    strcpy(ids[idcnt],strtoken);return idcnt++;
}
int insertConst() { strcpy(ints[idcnt],strtoken);return intcnt++; }
int getsym()
{
    memset(strtoken,0,sizeof(strtoken));
    pos=0;ch=getChar();getbc();
    /////////////////////////////////
    if(isalpha(ch))
    {
        while(isalnum(ch))
            strtoken[pos++]=ch, ch=getChar();
        retract();
        code=reserve();
        if(code==0)//标识符
        {
            value=insertId();
            return ID;
        }//关键字
            return code;
    }
    /////////////////////////////////
    else if(isdigit(ch))//数字
    {
        while(isdigit(ch))
            strtoken[pos++]=ch, ch=getChar();
        retract();
        value=insertConst();
        return INT;
    }
    else//算符和界符
    {
        strtoken[pos++]=ch;
        switch(ch)
        {
            case ',':return COMMA;
            case '+':return PLUS;
            case '-':return REDUCE;
            case '*':return MUTIL;
            case '/':return DIV;
            case ';':return SEMICOLON;
            case '(':return LPAR;
            case ')':return RPAR;
            case '{':return LBRACE;
            case '}':return RBRACE;
            case '=':return EQUAL;
            case '#':return NOTEQUAL;
            case ':':
                     ch=getchar();
                     if(ch!='=') return ERROR;
                     strtoken[pos++]=ch;return SET;
            case '>':
                     ch=getchar();
                     if(ch!='=') {retract();return BIGGER;}
                     strtoken[pos++]=ch;return BE;
            case '<':
                     ch=getchar();
                     if(ch!='=') {retract();return LESS;}
                     strtoken[pos++]=ch;return LE;
            default:return ERROR;
        }
    }
}
int main()
{
    freopen("in","r",stdin);
    //freopen("out","w",stdout);
    back=0;intcnt=0;idcnt=0;symcnt=0;
    while(1)
    {
        code=getsym();
        sym[symcnt]=code;
        if(code==ID||code==INT)
            symid[symcnt++]=value;
        else symid[symcnt++]=-1;
        ////////////////////SHOW////////////////////////
        printf("%s",strtoken);
        for(int i=0;i<3-pos/8;i++) printf("\t");
        if(code<20)
        {
            printf("%s",symname[code]);
            for(int i=0;i<3-strlen(symname[code])/8;i++) printf("\t");
        }
        else
        {
            printf("SYSYM");
        }
        if(code==ID||code==INT) printf("%d\n",value);
        else puts("");
        if(ch==EOF||ch=='.') break;
    }
}
instagram löschen li 说:
2023年7月19日 22:20

Aus persönlichen Gründen werden einige Benutzer ihre Instagram-Konten löschen. Bevor Sie ihr Konto löschen, sollten Sie genau prüfen, ob Sie es wirklich wollen oder nicht. Weil es so einfach ist, sich anzumelden und Ihr Konto jederzeit zu kündigen. instagram löschen link Sobald Sie mit dem Löschen fortfahren, gibt es keine Möglichkeit, das Konto wiederherzustellen, und Sie verlieren alle Ihre Timeline-Posts, Reels, Follower, Kommentare und Favoriten.

Maha Board 7th Clas 说:
2023年7月20日 17:08

Students Should Download the Respective Stream wise Maharashtra 7th Class Exam Syllabus 2024 Subjects of Hindi, English, Mathematics, Science, Social Science etc, Pdf Format Provide Check the same From our page here, So, Students need to Start Their Preparation by first Downloading the Stream wise Maharashtra Class Curriculum Syllabus 2024 Latest Edition.Maharashtra 7th Class Syllabus 2024 Provided here is Maha Board 7th Class Syllabus 2024 same as Published by the Maharashtra State Board of Secondary & Higher Secondary Education on its official website.

civaget 说:
2023年12月07日 23:38

Discover the best 유흥사이트 for a night filled with excitement. Explore various entertainment hotspots and plan your next adventure.

civaget 说:
2023年12月09日 21:06

Playing 에볼루션카지노 is an immersive experience. The live blackjack and baccarat are my favorites. It's the epitome of online casino excellence.

civaget 说:
2023年12月13日 18:37

안전카지노's quick deposit and withdrawal process is a game-changer for busy players.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter