加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

编译原理之语义分析(C语言)

(2011-04-18 00:19:32)
标签:

杂谈

编译原理之语义分析*(C语言)
一 继承的词法来自

http://blog.sina.com.cn/s/blog_67c9fc300100srad.html

继承的语法来自

http://blog.sina.com.cn/s/blog_67c9fc300100srar.html

============================================================================
//语义分析
//main.c
//@author langx


#include "stdio.h"                  //定义I/O库所用的某些宏和变量
#include "string.h"                 //定义字符串库函数
#include "conio.h"                  //提供有关屏幕窗口操作函数
#include "ctype.h"                  //分类函数
#define LEN 6
char prog[80]= {'\0'},
               token[80]= {'\0'};                    //存放构成单词符号的字符串
char ch;
int syn,                           //存放单词字符的种别码
    n,
    sum,                           //存放整数型单词
    m,p;                           //p是缓冲区prog的指针,m是token的指针
char *rwtab[LEN]= {"begin","if","then","while","do","end"};
int kk,q;
struct
{
    char result1[8];
    char ag11[8];
    char op1[8];
    char ag21[8];
} quad[20];
char *factor();
char *expression_r();
int yucu();
char *term();
int statement();
int lrparser();
char *newtemp();
scaner();
emit(char *result,char *ag1,char *op,char *ag2);

int lrparser()
{
    int schain=0;
    kk=0;
    if (syn==1)
    {
        scaner();
        schain=yucu();
        if(syn==6)
        {
            scaner();
            if((syn==0)&&(kk==0))  printf("Success!\n");
        }
        else
        {
            if(kk!=1)printf("short of 'end' !\n");
            kk=1;
            getch();
            exit(0);
        }
    }
    else
    {
        printf("short of 'begin' !\n");
        kk=1;
        getch();
        exit(0);
    }
    return (schain);
}
int yucu()
{
    int schain=0;
    schain=statement();
    while(syn==26)
    {
        scaner();
        schain=statement();
    }
    return (schain);
}
int statement()
{
    char tt[8],eplace[8];
    int schain=0;
    if (syn==10)
    {
        strcpy(tt,token);
        scaner();
        if(syn==18)
        {
            scaner();
            strcpy(eplace,expression_r());
            emit(tt,eplace,"","");
            schain=0;
        }
        else
        {
            printf("short of sign ':=' !\n");
            kk=1;
            getch();
            exit(0);
        }
        return (schain);
    }
}
char *expression_r()
{
    char *tp,*ep2,*eplace,*tt;
    tp=(char *)malloc(12);
    ep2=(char *)malloc(12);
    eplace=(char *)malloc(12);
    tt=(char *)malloc(12);
    strcpy(eplace,term());
    while((syn==13)||(syn==14))
    {
        if (syn==13)strcpy(tt,"+");
        else strcpy(tt,"-");
        scaner();
        strcpy(ep2,term());
        strcpy(tp,newtemp());
        emit(tp,eplace,tt,ep2);
        strcpy(eplace,tp);
    }
    return (eplace);
}
char *term()
{
    char *tp,*ep2,*eplace,*tt;
    tp=(char *)malloc(12);
    ep2=(char *)malloc(12);
    eplace=(char *)malloc(12);
    tt=(char *)malloc(12);
    strcpy(eplace,factor());
    while((syn==15)||(syn==16))
    {
        if (syn==15)strcpy(tt,"*");
        else strcpy(tt,"/");
        scaner();
        strcpy(ep2,factor());
        strcpy(tp,newtemp());
        emit(tp,eplace,tt,ep2);
        strcpy(eplace,tp);
    }
    return (eplace);
}
char *factor()
{
    char *fplace;
    fplace=(char *)malloc(12);
    strcpy(fplace,"");
    if(syn==10)
    {
        strcpy(fplace,token);
        scaner();
    }
    else if(syn==11)
    {
        itoa(sum,fplace,10);
        scaner();
    }
    else if(syn==27)
    {
        scaner();
        fplace=expression_r();
        if(syn==28) scaner();
        else
        {
            printf("error on ')' !\n");
            kk=1;
            getch();
            exit(0);
        }
    }
    else
    {
        printf("error on '(' !\n");
        kk=1;
        getch();
        exit(0);
    }
    return (fplace);
}
char *newtemp()
{
    char *p;
    char m[8];
    p=(char *)malloc(8);
    kk++;
    itoa(kk,m,10);
    strcpy(p+1,m);
    p[0]='t';
    return(p);
}
scaner()
{
    sum=0;
    for(m=0; m<8; m++)token[m++]=NULL;
    m=0;
    ch=prog[p++];
    while(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;
            ch=prog[p++];
        }
        p--;
        syn=10;
        token[m++]='\0';
        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 '<':
            m=0;
            ch=prog[p++];
            if(ch=='>')
            {
                syn=21;
            }
            else if(ch=='=')
            {
                syn=22;
            }
            else
            {
                syn=20;
                p--;
            }
            break;
        case '>':
            m=0;
            ch=prog[p++];
            if(ch=='=')
            {
                syn=24;
            }
            else
            {
                syn=23;
                p--;
            }
            break;
        case ':':
            m=0;
            ch=prog[p++];
            if(ch=='=')
            {
                syn=18;
            }
            else
            {
                syn=17;
                p--;
            }
            break;
        case '+':
            syn=13;
            break;
        case '-':
            syn=14;
            break;
        case '*':
            syn=15;
            break;
        case '/':
            syn=16;
            break;
        case '(':
            syn=27;
            break;
        case ')':
            syn=28;
            break;
        case '=':
            syn=25;
            break;
        case ';':
            syn=26;
            break;
        case '#':
            syn=0;
            break;
        default:
            syn=-1;
            break;
        }
}
emit(char *result,char *ag1,char *op,char *ag2)
{
    strcpy(quad[q].result1,result);
    strcpy(quad[q].ag11,ag1);
    strcpy(quad[q].op1,op);
    strcpy(quad[q].ag21,ag2);
    q++;
}
main()
{
    int j;
    printf("\nThe significance of the figures:\n"
           "1.figures 1 to 6 said Keyword\n"
           "2.figures 10 and 11 said Other indicators\n"
           "3.figures 13 to 28 said Operators\n");

    q=p=kk=0;

    printf("\nplease input string: (end with '#')\n");
    do
    {
        scanf("%c",&ch);
        prog[p++]=ch;
    }
    while(ch!='#');
    p=0;
    scaner();
    lrparser();
    if(q>19)printf(" to long sentense!\n");
    else for (j=0; j
    getch();
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有