资源简介

Cminus的词法、语法分析程序。各种细节处理比较好了,输入cminus程序,输出带格式的语法分析结果,给编译原理的学习提供思路

资源截图

代码片段和文件信息

#include “shiyan2.h“

/*
word analyse
*/

enum Syn_Type syn;
extern FILE* INPUT_FILE;
extern FILE* OUTPUT_FILE;
char row[Max_row_num+HEADSPACE];//the present row of inputfile
int row_num;
int now_row;
char now[Max_word_length+HEADSPACE];//the present word or number or symbol
char Ctmp[Max_word_length];
char ch;
int p;
int end;

const char Key_words[Key_words_num][Max_word_length]= {“int““void““if““else““return““while“};

const char Cexplain[CifaNum][Max_word_length]=
{
    “Int“
    “Void“
    “If“
    “Else“
    “Return“
    “While“
    “Id“
    “Digit“
    “Op_Mul“
    “Op_Add“
    “Br_Sq_Right“
    “Br_Big_Right“
    “Str“
    “Comma“
    “Compare“
    “Eql“
    “Sem“
    “Br_Sm_Left“
    “Br_Sm_Right“
    “Br_Sq_Left“
    “Br_Big_Left“
    “Comment“
    “WORDERROR“
};

int isChar(char ch)
{
    if(ch>=‘A‘&&ch<=‘Z‘) return 1;
    if(ch>=‘a‘&&ch<=‘z‘) return 1;
    return 0;
}

int isNum(char ch)
{
    if(ch>=‘0‘&&ch<=‘9‘) return 1;
    return 0;
}

int getNextLine()
{
    if(fgets(rowMax_row_numINPUT_FILE)==NULL) return FILEEOF;
    now_row++;
    p=0;
    row_num=strlen(row);
    return 0;
}

int analyse()
{
    syn=WORDERROR;
    int np=0ctp=0i;
    while(p>=row_num-1)
        if(getNextLine()==FILEEOF)
            return FILEEOF;
    while(row[p]==‘ ‘||row[p]==9)
    {
        p++;
        while(p>=row_num-1)
            if(getNextLine()==FILEEOF) return FILEEOF;
    }
    ch=row[p++];
    if(isChar(ch))
    {
        now[np++]=ch;
        ch=row[p];
        while(isChar(ch)||isNum(ch))
        {
            p++;
            if(np                now[np++]=ch;
            ch=row[p];
        }
        if(np>=Max_word_length)
            error(Id_TooLong);//!
        now[np]=0;
        syn=_Id;
        for(i=0; i            if(strcmp(nowKey_words[i])==0)
            {
                syn=(enum Syn_Type)i;
                break;
            }
    }
    else if(isNum(ch))
    {
        now[np++]=ch;
        ch=row[p];
        while(isNum(ch))
        {
            p++;
            if(np==Max_word_length)
            {
                error(Num_Toolarge); //!
                ch=row[p];
                while(isNum(ch))
                {
                    p++;
                    ch=row[p];
                }
                now[0]=‘0‘;
                np=1;
                break;
            }
            now[np++]=ch;
            ch=row[p];
        }
        now[np]=0;
        syn=_Digit;
    }
    else
    {
        Ctmp[ctp++]=ch;
        Ctmp[ctp]=0;
        switch(ch)
        {
        case ‘*‘:
            syn=_Op_Mul;
            break;
        case ‘/‘:
            ch=row[p];
            if(ch==‘*‘)
            {
                strcpy(Ctmp“omitted“);
                p++;
                while(true)
                {
     

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-11-21 13:05  shiyan2.6\
     目录           0  2013-11-21 13:05  shiyan2.6\bin\
     目录           0  2013-11-21 13:05  shiyan2.6\bin\Debug\
     文件       93123  2013-06-16 23:37  shiyan2.6\bin\Debug\shiyan2.exe
     文件        6862  2013-06-04 20:11  shiyan2.6\cifa.cpp
     文件        4464  2013-06-04 23:10  shiyan2.6\error.cpp
     文件       11673  2013-06-16 23:36  shiyan2.6\expression.cpp
     文件        2902  2013-06-04 23:12  shiyan2.6\grammar.cpp
     文件         324  2013-06-16 23:37  shiyan2.6\in1.txt
     文件         217  2013-06-16 23:17  shiyan2.6\in2.txt
     文件         971  2013-06-04 22:09  shiyan2.6\in3.txt
     文件         571  2013-06-04 19:40  shiyan2.6\init.cpp
     文件        1389  2013-06-04 21:59  shiyan2.6\IO.cpp
     文件          97  2013-05-29 23:04  shiyan2.6\main.cpp
     目录           0  2013-11-21 13:05  shiyan2.6\obj\
     目录           0  2013-11-21 13:05  shiyan2.6\obj\Debug\
     文件       12454  2013-06-16 23:15  shiyan2.6\obj\Debug\cifa.o
     文件        7752  2013-06-16 23:15  shiyan2.6\obj\Debug\error.o
     文件       12475  2013-06-16 23:37  shiyan2.6\obj\Debug\expression.o
     文件       29406  2013-06-16 23:15  shiyan2.6\obj\Debug\grammar.o
     文件        3192  2013-06-16 23:15  shiyan2.6\obj\Debug\init.o
     文件        4202  2013-06-16 23:15  shiyan2.6\obj\Debug\IO.o
     文件        2243  2013-06-16 23:15  shiyan2.6\obj\Debug\main.o
     文件       14104  2013-06-16 23:15  shiyan2.6\obj\Debug\WordFun.o
     文件        1749  2013-06-17 09:52  shiyan2.6\out1.txt
     文件        1397  2013-06-16 23:30  shiyan2.6\out2.txt
     文件        3936  2013-06-16 23:33  shiyan2.6\out3.txt
     文件        1598  2013-06-16 23:38  shiyan2.6\shiyan2.6.cbp
     文件        3028  2013-11-26 20:46  shiyan2.6\shiyan2.6.depend
     文件        1769  2013-11-26 20:47  shiyan2.6\shiyan2.6.layout
     文件        2921  2013-06-16 23:15  shiyan2.6\shiyan2.h
............此处省略6个文件信息

评论

共有 条评论