表达式翻译器-1-编译原理

表达式翻译器

一、实验目的

构造一个中缀表达式到后缀表达式的翻译器,初步了解递归下降语法分析原理及语法制导翻译的过程。

1实现的功能:

数字相加(包括多位数字),与字母相加(包括多字母),乘除,加减有优先级顺序的翻译.

编程使用的语言:

涉及的算法:词法分析 左递归下降法

2.结果显示:

   

                   

3.关键代码:

词法分析:

View Code
int isdigit(int t)

{

         if(t>='0'&&t<='9')

                   return 1;

         else return 0;

}

int ischar(int t)

{

         if(t>='A'&&t<='z')

                   return 1;

         else return 0;

}

 

 

int Match1()

{

         int t, i;

         while (1) {

                   t = getchar();

                   if (t == ' ' || t == '\t')

                            ;

                   else if (t == '\n')

                            lineno++;

                   else

                            if (isdigit(t)) {

                                     tokenval =0;

                                     while (isdigit(t)) {

                                               tokenval = tokenval * 10 + t - '0';

                                               t = getchar();

                                     }

                                     ungetc(t, stdin);

                                     //return   tokenval;

                                     LookAhead1=tokenval;

                                     return NUM_TKN;

                            }

                            else if( ischar(t) ) {

                                     i=0;

                                    

                                     do {

                                               lexeme[i++]=t; t = getchar();

                                     }while( ischar(t) || isdigit(t) );

                                    

                                     lexeme[i]='\0';

                                     ungetc(t, stdin);

                                     return ID_TKN;

                            }        else {

                                     tokenval = NONE;

                                     return t;//例如t='+',则把'+'的ASCII作为'+'的TokenName。

                            }

         }

}

左递归下降法:

void Morefactors()

{

    switch( LookAhead ) {

           case '*':

                     temp=   Match1(); Factor(); putchar('*'); Morefactors(); // rest --> + term {print('+')} rest

                     break;

              case '/':

                  temp=  Match1(); Factor(); putchar('/'); Morefactors(); // rest --> - term {print('-')} rest

                     break;

              default:   // rest --> 空

                     break;

       }

}

 

void Moreterms()

{

       switch( LookAhead ) {

           case '+':

                     temp=   Match1(); Term(); putchar('+'); Moreterms(); // rest --> + term {print('+')} rest

                     break;

              case '-':

                     temp=   Match1(); Term(); putchar('-'); Moreterms(); // rest --> - term {print('-')} rest

                     break;

              default:   // rest --> 空

                     break;

       }

}

 all code

View Code
#include <stdio.h>
#include <stdlib.h>
#define NUM_TKN  500
#define ID_TKN    600
#define NONE    NULL
char LookAhead;
int LookAhead1;
 char lexeme[1024];
int lineno = 1,temp=0, tokenval = 0; 
int isdigit(int t)
{
    if(t>='0'&&t<='9')
        return 1;
    else return 0;
}
int ischar(int t)
{
    if(t>='A'&&t<='z')
        return 1;
    else return 0;
}


int Match1()
{
    int t, i;
    while (1) {
        t = getchar();
        if (t == ' ' || t == '\t') 
            ;
        else if (t == '\n') 
            lineno++;
        else 
            if (isdigit(t)) {
                tokenval =0;
                while (isdigit(t)) {
                    tokenval = tokenval * 10 + t - '0';
                    t = getchar();
                }
                ungetc(t, stdin);
                //return    tokenval;
                LookAhead1=tokenval;
                return NUM_TKN;
            }
            else if( ischar(t) ) {
                i=0; 
                
                do {
                    lexeme[i++]=t; t = getchar(); 
                }while( ischar(t) || isdigit(t) );
                
                lexeme[i]='\0';
                ungetc(t, stdin);
                return ID_TKN;
            }    else {
                tokenval = NONE;
                return t;//例如t='+',则把'+'的ASCII作为'+'的TokenName。
            }
    }
}


void Match(char t)
{
    //if( LookAhead==t ) 
        LookAhead = getchar(); //继续往前看后一个字符
   /* else {
        printf("\n表达式错误:Match函数中需要输入的字符为%c,但是实际输入的是%c\n", t, LookAhead );
        exit(1); //结束程序
    }*/

}
void Expr();
void Factor()
{ 
    if(LookAhead=='(')
    {
        Match('(');
        Expr();
        Match(')');
    }

    if(temp==500)
    printf("%d ",LookAhead1);
    else if(temp==600)
    {
        char *p;
        p = lexeme;
        printf("%s ",p);
    }
    Match(LookAhead);
    /*if(LookAhead>='a' && LookAhead<='z')
    {
        printf("%c",LookAhead);
        Match(LookAhead);
    }
    if(LookAhead>='0' && LookAhead<='9')
    {
        printf("%c",LookAhead);
        Match(LookAhead);
    }*/
}

void Morefactors();
void Term()
{
    Factor();
    Morefactors();
}

void Morefactors()
{
    switch( LookAhead ) {
        case '*':
            temp=    Match1(); Factor(); putchar('*'); Morefactors(); // rest --> + term {print('+')} rest
            break;
        case '/':
            temp=    Match1(); Factor(); putchar('/'); Morefactors(); // rest --> - term {print('-')} rest
            break;
        default:   // rest --> 空
            break;
    }
}

void Moreterms()
{
    switch( LookAhead ) {
        case '+':
            temp=    Match1(); Term(); putchar('+'); Moreterms(); // rest --> + term {print('+')} rest
            break;
        case '-':
            temp=    Match1(); Term(); putchar('-'); Moreterms(); // rest --> - term {print('-')} rest
            break;
        default:   // rest --> 空
            break;
    }
}


void Expr()
{
    Term();
    Moreterms();
}




void main()
{    printf("请输入中缀表达式:");
temp=    Match1(); 

//    printf("其后缀表达式为:");
    Expr();
    
/*    if( LookAhead !='\n' ) {
        //例如:3+45
        printf("\n输入的表达式错误,错误的字符:%c\n", LookAhead);
        exit(1);
    }
*/
    printf("\n表达式分析成功!\n");

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值