简体   繁体   English

Flex和野牛中的逻辑表达式语法

[英]Logical expression Grammar in Flex and bison

I am writing a simple Grammar for logical expression in flex and bison. 我正在为flex和bison编写逻辑表达式的简单语法。 The expression is a postfix expression. 该表达式是一个后缀表达式。 This is my yacc code. 这是我的yacc代码。

float_logic_expr:
                float_logic_expr
                | float_logic_exprs
                ;

float_logic_exprs:
                float_reln_expr
            | float_reln_expr float_reln_expr PP_AND                    
            | float_reln_expr float_reln_expr PP_OR                 
            ;

string_logic_expr:
              string_reln_expr
            | string_reln_expr string_reln_expr PP_AND              
            | string_reln_expr string_reln_expr PP_OR               
            ;

The above rule works fine for the below statement. 上面的规则适用于下面的语句。

if #a 40 >  #b 20 == && then

But if i give one more condition to the logical expression, it fails. 但是,如果我再给逻辑表达式一个条件,它将失败。

if #a 40 >  #b 20 == && #b 30 == && then

Parse error. 解析错误。

What should I change the grammar to work for multiple relational expression? 我应如何更改语法以使其适用于多个关系表达?

It's doing what you told it to do, no? 它正在执行您指示的操作,不是吗?

float_logic_exprs
            : float_reln_expr
            | float_reln_expr float_reln_expr PP_AND                    
            | float_reln_expr float_reln_expr PP_OR                 
            ;

That says that a logic expression is a single relation expression or two (exactly) relation expressions followed by an AND or OR token. 也就是说,逻辑表达式是单个关系表达式或两个(精确)关系表达式,后跟ANDOR标记。 But the example you provide shows that you expect a logic expression to be able to take another logic expression as an argument, not just a relation expression. 但是您提供的示例表明,您期望逻辑表达式能够将另一个逻辑表达式作为参数,而不仅仅是关系表达式。

Also, this is pointless: 另外,这是没有意义的:

float_logic_expr:
                float_logic_expr
                | float_logic_exprs
                ;

Since the production float_logic_expr: float_logic_expr cannot ever be useful (and it must trigger some kind of bison warning). 由于生产float_logic_expr: float_logic_expr永远不会有用(它必须触发某种野牛警告)。

All that should suggest that you want: 所有这些都建议您要:

float_logic_expr
            : float_reln_expr
            | float_logic_expr float_logic_expr PP_AND                    
            | float_logic_expr float_logic_expr PP_OR                 
            ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM