⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lex.cpp

📁 一个很号用的词法分析程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                    state = 8;
                else
                    state = fail();
                break;
            case 8:
                c = NextChar();
                if(isdigit(c))
                    state = 8;
                else if(c == '.')
                    state = 9;
                else if(c == 'E')
                {
                    state = 11;
                }
                else
                    state = fail();
                break;
            case 9:
                c = NextChar();
                if(isdigit(c))
                    state = 10;
                else
                    state = fail();
                break;
            case 10:
                c = NextChar();
                if(isdigit(c))
                    state = 10;
                else if(c == 'E')
                    state = 11;
                else
                    state = fail();
                break;
            case 11:
                c = NextChar();
                if(c == '+' || c == '-')
                    state = 12;
                else if(isdigit(c))
                    state = 13;
                else
                    state = fail();
                break;
            case 12:
                c = NextChar();
                if(isdigit(c))
                    state = 13;
                else
                    state = fail();
                break;
            case 13:
                c = NextChar();
                if(isdigit(c))
                    state = 13;
                else
                {
                    /* state = 14; */
                    Retract(1);
                    install_num();
                    return NUM;
                }
                break;
            case 15:
                c = NextChar();
                if(isdigit(c))
                    state  = 16;
                else
                    state = fail();
                break;
            case 16:
                c = NextChar();
                if(isdigit(c))
                    state = 16;
                else if(c == '.')
                    state = 17;
                else
                    state = fail();
                break;
            case 17:
                c = NextChar();
                if(isdigit(c))
                    state = 18;
                else
                    state = fail();
                break;
            case 18:
                c = NextChar();
                if(isdigit(c))
                    state = 18;
                else
                {
					/*state = 19;*/
                    Retract(1);
                    install_num();
					return NUM;
                }
                break;
            case 20:
                c = NextChar();
                if(isdigit(c))
                    state = 21;
                else
                    state = fail();
                break;
            case 21:
                c = NextChar();
                if(isdigit(c))
                    state = 21;
				else if(c == 'L' || c == 'l' || c == 'D' || c == 'd')
				{
					//state = 23
					install_num();
					return NUM;
				}
                else
                {
                    /*state = 22;*/
                    Retract(1);
                    install_num();
					return NUM;
                }
                break;
            case 42:
                c = NextChar();
                if(c == '\'')
                    state = 43;
                else if(c == '\"')
                    state = 49;
                else
                    state = fail();
                break;
            case 43:
                c = NextChar();
                if(c == '\\')
                    state = 44;
                else if(isascii(c))
                    state = 47;
                else
                    state = fail();
                break;
            case 44:
                c = NextChar();
                if(c == '\'')
                {
                    /* state = 46; */
                    install_char();
                    return CHAR;
                }
				else if(isalnum(c) || (c == '\\'))
                    state = 44;
                else
                {
                    Error("Bad change-meaning value");
                    Recover();
                }
                break;
            case 47:
                c = NextChar();
                if(c == '\'')
                {
                    /* state = 46; */
                    install_char();
					return CHAR;
                }
                else
                {
                    /*state = fail();*/
                    Error("The char should only contain one word");
                    Recover();
                }
                break;
            case 49:
                c = NextChar();
                if(isalnum(c))
                    state = 49;
                else if(c == '\"')
                {
                    /*state = 50;*/
                    install_string();
					return STRING;
                }
                break;
            case 70:
                c = NextChar();
                if(c == '+')
                    state = 71;
				else if(c == '-')
					state = 75;
				else if(c == '*')
					state = 80;
				else if(c == '/')
					state = 83;
				else if(c == '%')
					state = 86;
                else
                    state = fail();
                break;
            case 71:
                c = NextChar();
                if(c == '+')
                {
                    /* state = 73; */
                    fprintf(Lexfp,"<op,DADD>\n");
                    return OP;

                }
                else if(c == '=')
                {
                    /* state = 74; */
                    fprintf(Lexfp,"<op,EADD>\n");
                    return OP;
                }
                else
                {
                    Retract(1);
                    fprintf(Lexfp,"<op,ADD>\n");
                    return OP;
                }
            case 75:
                c = NextChar();
                if(c == '-')
                {
                    fprintf(Lexfp,"<op,DSUB>\n");
                    return OP;
                }
                else if(c == '=')
                {
                    fprintf(Lexfp,"<op,ESUB>\n");
                    return OP;
                }
                else if(c == '>')
                {
                    fprintf(Lexfp,"<op,PTR>\n");
                    return OP;
                }
                else
                {
                    Retract(1);
                    fprintf(Lexfp,"<op,SUB>\n");
                    return OP;
                }
            case 80:
                c = NextChar();
                if(c == '=')
                {
                    fprintf(Lexfp,"<op,EMUL>\n");
                    return OP;
                }
                else
                {
                    Retract(1);
                    fprintf(Lexfp,"<op,MUL>\n");
                    return OP;
                }
            case 83:
                c = NextChar();
                if(c == '=')
                {
                    fprintf(Lexfp,"<op,EDIV>\n");
                    return OP;
                }
                else
                {
                    Retract(1);
                    fprintf(Lexfp,"<op,DIV>\n");
                    return OP;
                }
            case 86:
                c = NextChar();
                if(c == '=')
                {
                    fprintf(Lexfp,"<op,EMOD>\n");
                    return OP;
                }
                else
                {
                    Retract(1);
                    fprintf(Lexfp,"<op,MOD>\n");
                    return OP;
                }
            case 90:
                c = NextChar();
                if(c == '<')
                    state = 91;
                else if(c == '>')
                    state = 94;
                else if(c == '!')
                    state = 97;
                else if(c == '=')
                    state = 100;
                else
                    state = fail();
                break;
            case 91:
                c = NextChar();
                if(c == '=')
                {
                    fprintf(Lexfp,"<relop,LE>\n");
                    return RELOP;
                }
                else
                {
                    Retract(1);
                    fprintf(Lexfp,"<relop,LT>\n");
                    return RELOP;
                }
            case 94:
                c = NextChar();
                if(c == '=')
                {
                    fprintf(Lexfp,"<relop,GE>\n");
                    return RELOP;
                }
                else
                {
                    Retract(1);
                    fprintf(Lexfp,"<relop,GT>\n");
                    return RELOP;
                }
            case 97:
                c = NextChar();
                if(c == '=')
                {
                    fprintf(Lexfp,"<relop,NE>\n");
                    return RELOP;
                }
                else
                {
                    Retract(1);
                    fprintf(Lexfp,"<Logop,NOT>\n");
                    return LOGOP;
                }
            case 100:
                c = NextChar();
                if(c == '=')
                {
                    fprintf(Lexfp,"<relop,EQ>\n");
                    return RELOP;
                }
                else
                {
                    Retract(1);
                    fprintf(Lexfp,"<op,VAL>\n");
                    return OP;
                }
            case 110:
                c = NextChar();
                if(c == '|')
                    state = 111;
                else if(c == '&')
                    state = 113;
                else
                    state = fail();
                break;
            case 111:
                c = NextChar();
                if(c == '|')
                {
                    fprintf(Lexfp,"<Logop,OR>\n");
                    return LOGOP;
                }
                else
                    state = fail();
                break;
            case 113:
                c = NextChar();
                if(c == '&')
                {
                    fprintf(Lexfp,"<Logop,AND>\n");
                    return LOGOP;
                }
                else
                    state = fail();
                break;
            case 120:
                c = NextChar();
                if(c == '$' || c == '^' || c == '@')
                {
                    Error("Not expected letter");
                    Recover();
                    start = 0;
                    break;
                }
                fprintf(Lexfp,"<%c, >\n",c);
                return c;
        }
    }
}

bool Lex(char *filename)//对指定文件进行词法分析
{
    int completed = 0;

    InitIdTable();

    /* 打开文件:源代码文件、错误文件、符号表文件、标识符文件 */
    if((Srcfp = fopen(filename,"r")) == NULL)
    {
        printf("\nError on open the source file!\n");
        exit(1);
    }
    if((Errfp = fopen("error.txt","w")) == NULL)
    {
        printf("\nError create the file!");
        exit(1);
    }
    if((Idfp = fopen("idtable.txt","w")) == NULL)
    {
        printf("\nError create the file!");
        exit(1);
    }
    if((Lexfp = fopen("Lexicalized.txt","w")) == NULL)
    {
        printf("\nError create the file!");
        exit(1);
    }
	FillWholeBuffer();
    forward = NULL;
    token_beginning = InputBuffer;

    while(!completed)
    {
		NextToken();
		state = 1;
		start = 1;

		token_beginning = forward;
		token_beginning++;
    }
	OutputIdTable();
    getch();
	return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -