如何在Flex中定义布尔值 [英] How to define a boolean in Flex
问题描述
我正在为玩具Java语言(Decaf)构建编译器,但在定义布尔值时遇到了麻烦.当我尝试分析布尔值时,无论我是否写错,它总是返回false.
弹性代码:
I am building a compiler for a toy Java language (Decaf) and I am having trouble with defining a bool. When I try to analyze a boolean, it always returns false, whether or not I wrote false.
Flex code:
true|false {
yylval.boolConstant = yytext;
return T_BoolConstant;
}
输入代码:
Input code:
bool x = true;
bool y = false;
输出:
Output:
true T_BoolConstant (value = false)
false T_BoolConstant (value = false)
我尝试在SO上进行搜索,但这是我能得到的最接近的正确答案:
谢谢!
EDIT :输出被编码在一个单独的c文件中,该文件打印在测试代码中找到的字符串,它是什么类型的值(在这种情况下,它是boolean或BooleanConstant),然后是保存的令牌.抱歉造成任何混乱.
I tried searching on SO but this was the closest I could get to a proper answer:
Simulating Booleans in Bison with C
Thank you!
EDIT: The output is coded in a separate c file that prints the string found in the test code, what kind of value it is (in this case it is a boolean or BooleanConstant) and then the value of the token that was saved. Sorry for any confusion.
推荐答案
我相信您需要将字符串"true"
和"false"
转换为布尔常量true
和false
.否则,您只是存储字符串.
I believe you need to convert the strings "true"
and "false"
into the boolean constants true
and false
. Otherwise you're just storing strings.
true {
yylval.boolConstant = true;
return T_BoolConstant;
}
false {
yylval.boolConstant = false;
return T_BoolConstant;
}
这篇关于如何在Flex中定义布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!