M - 翻译布尔表达式(C语言版)

Description
大家都学过了布尔表达式的翻译,其中有一个拉链-回填技术,这次我们就练习这个技术。

Input
输入为一行字符串,例如: a < b or c < d and e < f

每个符号都用空格间隔。

其中逻辑运算符包含 and 和 or , 关系运算符包含 < 、> 、<= 、 >= 、== 、 != 。

Output
假链跳到0,真链跳到1,表达式序号从100开始排。

Sample
在这里插入图片描述

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct words{
    char op[4];
    char left;
    char right;
}W;

W s[1000];
int i;
int t = 0;
int count = 100;
int TRUE = 1;
int FALSE = 100;
int cur = 0;
int begin = 0;
char str[1000];
char temp[10];

int main(){
    gets(str);
    int len = strlen(str);
    str[len] = ' ';
    str[len+1] = '#';
    str[len+2] = '\0';
    len = strlen(str);

    for(i = 0;i< len;i++){
        if(str[i] == ' '){
            continue;
        }else{
            temp[t++] = str[i];
            if(str[i] == '#'){
                FALSE = 0;
                for(;begin<cur-1;begin++){
                    printf("%d(j%s,%c,%c,%d)\n",count,s[begin].op,s[begin].left,s[begin].right,count+2);
                    count++;
                    printf("%d(j,_,_,%d)\n",count,FALSE);
                    FALSE = count++;
                }
                 printf("%d(j%s,%c,%c,%d)\n",count,s[begin].op,s[begin].left,s[begin].right,TRUE);
                    TRUE = count++;
                    printf("%d(j,_,_,%d)\n",count,FALSE);
                    count++;
                    t = 0;
                    temp[t] = '\0';
                    cur++;
            }
            if(t==2){
                temp[t] = '\0';
                if(strcmp("or",temp) == 0){
                    FALSE +=2;
                    for(;begin<cur-1;begin++){
                    printf("%d(j%s,%c,%c,%d)\n",count,s[begin].op,s[begin].left,s[begin].right,count+2);
                    count++;
                    printf("%d(j,_,_,%d)\n",count,FALSE);
                    FALSE = count++;
                }
                 printf("%d(j%s,%c,%c,%d)\n",count,s[begin].op,s[begin].left,s[begin].right,TRUE);
                    TRUE = count++;
                    printf("%d(j,_,_,%d)\n",count,FALSE);
                    count++;
                    t = 0;
                    temp[t] = '\0';
                    begin = cur;
                }
            }
            if(t==3){
                temp[t] = '\0';
                if(strcmp("and",temp) == 0){
                    FALSE+=2;
                    t = 0;
                    temp[t] = '\0';
                }else if(str[i] != '='){
                    s[cur].left = temp[0];
                    s[cur].right = temp[2];
                    temp[0] = temp[1];
                    temp[1] = '\0';
                    strcpy(s[cur].op,temp);
                    t=0;
                    temp[t] = '\0';
                    cur++;
                }
            }
            if(t==4){
                s[cur].left = temp[0];
                    s[cur].right = temp[3];
                    temp[0] = temp[1];
                    temp[1] = temp[2];
                    temp[2] = '\0';
                    strcpy(s[cur].op,temp);
                    t=0;
                    temp[t] = '\0';
                    cur++;
            }
        }


    }
    return 0;

}

本人这学期开始学编译原理这门课,做题的时候在网上找到的参考代码几乎都是C++的,所以我打算分享一份C语言版的给朋友们,我只选择了其中一部分题,如果有不会的而且我还没发的,可以私聊我,我们一起讨论

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

又菜又爱分享的小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值