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

📄 new7-6.c

📁 用C开发的C语言编译器
💻 C
📖 第 1 页 / 共 3 页
字号:
#include <stdio.h>
#include <ctype.h>
#define BSIZE 128      /*--lexbuf数组最大值--*/
#define NONE -1
#define EOS '\0'
#define NUM 101
#define ID 102
#define operater 103   /*--其他操作符--*/
#define EQ 104         /*--=--*/
#define sign 105       /*--其他符号--*/
#define DONE 106       /*--分析完成--*/
#define LH 107         /*--{--*/
#define RH 108         /*--}--*/
#define Lcircle 109    /*--(--*/
#define Rcircle 110    /*--)--*/
#define SEMI 111       /*--;--*/
#define LBcircle 112   /*--[--*/
#define RBcircle 113   /*--]--*/
#define LT 114         /*--<--*/
#define RT 115         /*-->--*/
#define RE 116         /*-->=--*/
#define LE 117         /*--<=--*/
#define NOTEQUEL 118   /*--!=--*/
#define COMMA 119      /*--,--*/
#define EQA 120        /*--==--*/
#define PLUS 121       /*--+--*/
#define MINUS 122      /*-----*/
#define TIMES 123      /*--*--*/
#define OVER 124       /*--/--*/
#define PEQ 125        /*--+=--*/
#define MEQ 126        /*---=--*/
#define PP 127        /*--++--*/
#define MM 128        /*------*/
#define STRMAX 6000    /*--lexemes数组最大值--*/
#define SYMMAX 4000    /*--symtable数组最大值--*/
#define MAXSIZE 1000   /*--语义栈最大值--*/
/***************************全局定义*******************************************/


/***************************符号表结构体***************************************/
struct entry {
    char *lexptr;
    int token;
};
struct entry symtable[SYMMAX];        /*--结构体字母表--*/
struct entry yuyi[SYMMAX];

/***************************全局可操作文件声明*********************************/
FILE *fp;                             /*--读文件指针--*/
FILE *outp;                           /*--输出词法分析文件指针--*/
FILE *errorp;                         /*--输出错误文件指针--*/
FILE *IDp;                            /*--输出单词序列文件指针--*/
FILE *exp;                            /*--输出语法分析产生式序列--*/
FILE *yuyip;                          /*--输出语义信息--*/


/***************************全局其他变量声明***********************************/
int lastchar = -1;
int lastentry = 0;
int lineno = 1;
int tokenval = NONE;
int lookahead;
char tokenchar;
char lexbuf[BSIZE];
char lexemes[STRMAX];
char *stringi="";
int w=1;
int alastchar = -1;
int alastentry = 0;

/*********************************对栈的操作***********************************/


struct stack
{
    char *s[MAXSIZE];
    int top;
};

struct stack *ST;


char push(struct stack *ST,char *x)
{
    if(ST->top==MAXSIZE-1)
        {printf("Stack up overflow!\n");
         fprintf(errorp,"Stack up overflow!\n");}
    else
        {
            ST->top++;
            ST->s[ST->top]=*x;
            return(*x);
        }
}


void pop(struct stack *ST)
{
    if(ST->top==0)
        {printf("Stack down overflow!\n");
         fprintf(errorp,"Stack down overflow!\n");}
    else
        ST->top--;
}


char *stop(struct stack *ST)
{
    if(ST->top==0)
        {printf("The stack is empty!\n");
         fprintf(errorp,"The stack is empty!\n");}
    else

        return(ST->s[ST->top]);
}


int isempty(struct stack *ST)
{
    if(ST->top==0)
        return(1);
    else
        return(0);
}


/***************************符号表初始化关键字*********************************/
struct entry keyword[]={             /*--结构体关键字字母表--*/
"auto",1,
"break",2,
"case",3,
"char",4,
"const",5,
"continue",6,
"default",7,
"do",8,
"double",9,
"else",10,
"enum",11,
"extern",12,
"float",13,
"for",14,
"goto",15,
"if",16,
"int",17,
"long",18,
"register",19,
"return",20,
"short",21,
"signed",22,
"sizeof",23,
"static",24,
"struct",25,
"switch",26,
"typedef",27,
"union",28,
"unsigned",29,
"void",30,
"volatile",31,
"while",32,
0,0
};


/***************************初始化函数*****************************************/
init()                                      /*--初始化函数,插入32个关键字--*/
{
    struct entry *p;
    for(p=keyword;(p->token!=0);p++)
        insert(p->lexptr,p->token);
        printf("Insert ok!\n");
        fprintf(outp,"Insert ok!\n");
}


/***************************符号表查询函数*************************************/
int lookup(s)                                /*--查询是否是在字母表里--*/
{
    int p;
    for(p=lastentry;p>0;p--)
        if(strcmp(symtable[p].lexptr,s)==0)
        return p;
    return 0;
}

/***************************语义表查询函数*************************************/
int alookup(as)                                /*--查询是否是在语义表里--*/
{
    int p;
    for(p=alastentry;p>0;p--)
        if(strcmp(yuyi[p].lexptr,as)==0)
        return p;
    return 0;
}

/***************************插入符号表函数*************************************/
int insert(s,tok)                            /*--插入新的字母表信息--*/
{
    int len;
    len=strlen(s);
    if(lastentry+1>=SYMMAX)
        {printf("Symbol table full!\n");
         fprintf(outp,"Symbol table full!\n");
         fprintf(errorp,"Symbol table full!\n");}
    if(lastchar+len+1>=STRMAX)
        {printf("Lexemes array full!\n");
         fprintf(outp,"Lexemes array full!\n");
         fprintf(errorp,"Lexemes array full!\n");}
    lastentry = lastentry + 1;
    symtable[lastentry].token=tok;
    symtable[lastentry].lexptr= &lexemes[lastchar+1];
    lastchar=lastchar+len+1;
    strcpy(symtable[lastentry].lexptr,s);
    if(lastentry>32)
    {
     printf("<%s,%d>\n",s,symtable[lastentry].token);
     fprintf(outp,"<%s,%d>\n",s,symtable[lastentry].token);
    }
    return lastentry;
}

/****************************插入语义表操作************************************/
int ainsert(as,atok)                            /*--插入新的语义表信息--*/
{
    int len;
    len=strlen(as);
    if(alastentry+1>=SYMMAX)
        {printf("Symbol table full!\n");
         fprintf(outp,"Symbol table full!\n");
         fprintf(errorp,"Symbol table full!\n");}
    if(alastchar+len+1>=STRMAX)
        {printf("Lexemes array full!\n");
         fprintf(outp,"Lexemes array full!\n");
         fprintf(errorp,"Lexemes array full!\n");}
    alastentry = alastentry + 1;
    yuyi[alastentry].token=atok;
    yuyi[alastentry].lexptr= &lexemes[alastchar+1];
    alastchar=alastchar+len+1;
    strcpy(yuyi[alastentry].lexptr,as);
    return alastentry;
}

/********************************************************/
/*                   编译原理实验                       */
/*                   作者:软件四班王磊                 */
/*                   学号:1023710411                   */
/*                   实现功能词法分析                   */
/*********************************************************/
/***************************词法分析函数***************************************/
int lexan()
{
    char t;
    t = fgetc(fp);                            /*--判断文件结尾--*/
    u:      if(feof(fp))
                 return DONE;
            else if(t==' '||t=='\t')
                 {t= fgetc(fp);goto u;}
            else if(t=='\n')
                {lineno = lineno + 1 ;
                 t=fgetc(fp);goto u;}
            else if(isdigit(t))                /*--判断数字--*/
                {   int p,b=0;
                    while(isdigit(t)||t=='.')
                        {
                            lexbuf[b]=t;
                            t=fgetc(fp);
                            b=b+1;
                            if(b>=BSIZE)
                                {printf("Complier error!\n");
                                fprintf(outp,"Complier error!\n");
                                fprintf(errorp,"Complier error!\n");}
                        }
                    if(isalpha(t))
                    {
                      if(t=='E')
                      {   t=fgetc(fp);
                          if(t=='+'||t=='-'||t=='')
                          {
                           lexbuf[b]='E';
                           b=b+1;
                           lexbuf[b]=t;
                           b=b+1;
                           t=fgetc(fp);
                           while(isdigit(t))
                             {
                              lexbuf[b]=t;
                              t=fgetc(fp);
                              b=b+1;
                              if(b>=BSIZE)
                                {printf("Complier error!\n");
                                fprintf(outp,"Complier error!\n");
                                fprintf(errorp,"Complier error!\n");}
                             }
                          lexbuf[b]=EOS;
                          p=insert(lexbuf,NUM);
                          tokenval=p;
                          }
                          else
                           ungetc(t,fp);
                      }
                      else
                      {
                       while(isalpha(t))
                       {
                            lexbuf[b]=t;
                            t=fgetc(fp);
                            b=b+1;
                            if(b>=BSIZE)
                                {printf("Complier error!\n");
                                fprintf(outp,"Complier error!\n");
                                fprintf(errorp,"Complier error!\n");}
                        }
                      lexbuf[b]=EOS;
                      printf("Error string:%s\n",lexbuf);
                      fprintf(outp,"Error string:%s\n",lexbuf);
                      fprintf(errorp,"Error string:%s\n",lexbuf);
                      ungetc(t,fp);
                      }
                    }
                    else
                        {lexbuf[b]=EOS;
                         p=insert(lexbuf,NUM);
                         tokenval=p;
                         ungetc(t,fp);}
                    return symtable[p].token;
                }
            else if(isalpha(t))                      /*--判断字母--*/
                {
                    int p , b=0 ;
                    while(isalnum(t)||t=='_')
                        {
                            lexbuf[b]=t;
                            t=fgetc(fp);
                            b=b+1;
                            if(b>=BSIZE)
                                {printf("Complier error!\n");
                                 fprintf(outp,"Complier error!\n");
                                 fprintf(errorp,"Complier error!\n");}
                        }
                    lexbuf[b]=EOS;
                 p=lookup(lexbuf);
                 if(p==0)
                    {p=insert(lexbuf,ID);
                    if(symtable[p].token==102)
                    fprintf(IDp,"<%s,%d>\n",lexbuf,symtable[p].token);}
                 else
                    {printf("<%s,%d>\n",lexbuf,symtable[p].token);
                     fprintf(outp,"<%s,%d>\n",lexbuf,symtable[p].token);}
                 tokenval=p;
                 ungetc(t,fp);
                 return symtable[p].token;
                 }
            else if(isspace(t)==0&&isalnum(t)==0)      /*--判断各种符号--*/
                { int p;
                    switch(t){
                            case '+':
                             {
                                t = fgetc(fp);
                                if(t=='=')
                                p=insert("+=",PEQ);
                                else if(t=='+')
                                p=insert("++",PP);
                                else
                                {p=insert("+",PLUS);
                                 ungetc(t,fp);
                                 }
                             }
                            break;
                            case '-':
                              {
                                t=fgetc(fp);
                                if(t=='=')
                                p=insert("-=",MEQ);
                                else if(t=='-')
                                p=insert("--",MM);
                                else
                                {p=insert("-",MINUS);
                                 ungetc(t,fp);
                                 }
                              }
                            break;
                            case '*':
                              {
                                t=fgetc(fp);
                                if(t=='=')

⌨️ 快捷键说明

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