本文介绍了模拟布尔在野牛用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用C和野牛逻辑计算器,但我有麻烦了,因为C没有一个boolean类型。

I am trying to make a logic calculator using C and bison, but I'm having trouble because C does not have a boolean type.

这是我的Flex规则的一部分:

This is part of my Flex rules:

"TRUE" |
"T"    |
"t"  {yylval = 1; return TRUE; }

"FALSE" |
"F"    |
"f"  {yylval = 0; return TRUE; }

这是我的野牛规则的一部分:

This is part of my Bison rules:

line:
        EOL
        | exp EOL {printf("%d %d %d \n"), $1, $2,$$;}
        ;

exp: TRUE
   | FALSE
   ;

这是输出的时候我类型T在我的计算器其次是EOL(行尾):

This is the output when I type T followed by EOL (end of line) in my calculator:

10 12 1

10是换行ASCII,12为回车ASCII和1是起始的ASCII
我有同样的输出F。

10 is ascii for newline, 12 is ascii for carriage return and 1 is Ascii for startI have the same output for F.

我怎样才能使它所以1是$ 1,如果我进入一个T和0为$ 1,如果我进入一架F?

How can I make it so 1 is in $1 if I enter a T and 0 is in $1 if I enter a F?

推荐答案

我不是野牛的专家,它已经因为我已经使用了很长一段时间,所以我建议你读的因为我觉得你的Flex是错误的。你的规则需要返回一个记号类型,不是真的。在你野牛你有一个假的记号类型,但没有规定返回类型。你想要的是

I'm no Bison expert and it's been a long time since I've used it, so I suggest that you read http://flex.sourceforge.net/manual/ because I think your Flex is wrong. Your rules need to return a token type, not TRUE. In your Bison you have a FALSE token type, but no rule that returns that type. What you want is

exp: BOOLEAN

在野牛,并返回布尔,不是真话,因为布尔字符串Flex的规则。您还需要

in Bison, and Flex rules that return BOOLEAN, not TRUE, for the boolean strings. You will also want

%token BOOLEAN

%%

在您的野牛文件的开头。

at the beginning of your Bison file.

看看这个网页,其中显示其他人的有关弹性和野牛问题右侧的链接。

Take a look at the links on the right side of this page which show other people's questions about flex and bison.

您的评论我有麻烦,因为C没有一个boolean类型是不正确的,误导人进入给你关于C的类型无关的意见。

Your comment "I'm having trouble because C does not have a boolean type" is incorrect and has misled people into giving you irrelevant advice about C's types.

这篇关于模拟布尔在野牛用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 21:04