布尔/条件表达式的ANTLR v3语法 [英] ANTLR v3 grammar for boolean/conditional expression

查看:119
本文介绍了布尔/条件表达式的ANTLR v3语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先要为诸如此类的表达式创建语法

I'm taking a first stab at creating a grammar for expressions like:

(foo = bar or (bar = "bar" and baz = 45.43)) and test = true

到目前为止,我的语法如下:

My grammar so far looks like:

grammar filter;

tokens {
    TRUE = 'true';
    FALSE = 'false';
    AND = 'and';
    OR = 'or';
    LT = '<';
    GT = '>';
    EQ = '=';
    NEQ = '!=';
    PATHSEP = '/';
    LBRACK = '[';
    RBRACK = ']';
    LPAREN = '(';
    RPAREN = ')';
}

expression : or_expression EOF;

or_expression : and_expression (OR or_expression)*;

and_expression : term (AND term)*;

term : atom ( operator atom)? | LPAREN expression RPAREN;

atom : ID | INT | FLOAT | STRING | TRUE | FALSE;

operator : LT | GT | EQ | NEQ;

INT : '0'..'9'+;
FLOAT : ('0'..'9')+ '.' ('0'..'9')*;
STRING : '"' ('a'..'z'|'A'..'Z'|'_'|' ')* '"';
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

但是在ANTLRWorks 1.4.3中,我得到了解析树:

But in ANTLRWorks 1.4.3, I get the parse tree:

但是对于我的一生,我无法弄清楚我的语法出了什么问题.这里缺少什么标记?

But for the life of me I can't figure out what is wrong with my grammar. What token is it missing here?

非常感谢.

为了阐明atom生产中的atom ( operator atom)?替代方案,我也许应该提到原子应该能够独立存在,而无需与另一个原子进行比较.例如. a or b是有效的表达式.

To clarify the atom ( operator atom)? alternative in the atom production, I should perhaps mention that atoms should be able to be free-standing without comparison to another atom. E.g. a or b is a valid expression.

推荐答案

我在这里回答自己的问题.我发现语法有两个问题.第一个很容易发现.我已将EOF放在我的顶级规则的末尾:

I'm answering to my own question here. I found two problems with my grammar. The first was easy to spot; I had put EOF at the end of my top-level rule:

expression : or_expression EOF;

EOF因此是丢失的令牌.我的解决方案是从expression规则中删除EOF,而是在其上方引入一个规则:

The EOF was thus the missing token. My solution was remove the EOF from the expression rule, and instead introduce a rule above it:

filter: expression EOF;

第二个问题是我的or_expression规则应为:

The second problem was that my or_expression rule should be:

or_expression : and_expression (OR and_expression)*;

不是

or_expression : and_expression (OR or_expression)*;

完整的更正语法为:

grammar filter;

tokens {
    TRUE = 'true';
    FALSE = 'false';
    AND = 'and';
    OR = 'or';
    LT = '<';
    GT = '>';
    EQ = '=';
    NEQ = '!=';
    PATHSEP = '/';
    LBRACK = '[';
    RBRACK = ']';
    LPAREN = '(';
    RPAREN = ')';
}

filter: expression EOF;

expression : or_expression;

or_expression : and_expression (OR and_expression)*;

and_expression : term (AND term)*;

term : atom (operator atom)? | LPAREN expression RPAREN;

atom : ID | INT | FLOAT | STRING | TRUE | FALSE;

operator : LT | GT | EQ | NEQ;

INT : '0'..'9'+;
FLOAT : ('0'..'9')+ '.' ('0'..'9')*;
STRING : '"' ('a'..'z'|'A'..'Z'|'_'|' ')* '"';
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

生成的解析树为:

这篇关于布尔/条件表达式的ANTLR v3语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆