如何获取规则中的所有逻辑运算符令牌

How to get all logical operator token in a rule

我在获取规则中的所有操作员令牌时遇到问题。例如,如果我的输入是 (states = failed) and (states1 = nominal) or (states2 = nominal),那么我想得到 "and"/ "or"。我已经有一个语法可以解析我的输入,但是像 'and''or' 这样的词是我语法中的关键字.这样它们就可以出现在解析树中,但它们不匹配规则。

我想通过Listener的方式来完成,但是不知道如何获取这些token。 我的词法分析器文件:

lexer grammar TransitionLexer;

BOOLEAN: 'true' | 'false';
IF: 'if';
THEN: 'then';
ELSE: 'else';

NAME: (ALPHA | CHINESE | '_')(ALPHA | CHINESE | '_'|DIGIT)*;

ALPHA: [a-zA-Z];
CHINESE: [\u4e00-\u9fa5];

NUMBER: INT | REAL;
INT: DIGIT+
    |'(-'DIGIT+')';
REAL: DIGIT+ ('.' DIGIT+)?
    | '(-' DIGIT+ ('.' DIGIT+)? ')';
fragment DIGIT: [0-9];

OPCOMPARE: '='|'>='|'<='|'!='|'>'|'<';
WS: [ \t\n\r]+ ->skip;
SL_COMMENT:  '/*' .*? '*/' ->skip;

我的语法文件:

grammar TransitionCondition;
import TransitionLexer;
@parser::header{
    import java.util.*;
}
@parser:: members{
    private List<String> keywords = new ArrayList<String>();

    public boolean isKeyWord(){
        return keywords.contains(getCurrentToken().getText());
    }

    public List<String> getKeywords(){
        return keywords;
    }
}

condition : stat+ EOF;
stat : expr;

expr: pair (('and' | 'or') pair)*
    | '(' pair ')';

pair: '(' var OPCOMPARE value ')'   # keyValuePair
      | booleanExpr # booleanPair
      | BOOLEAN         # plainBooleanPair
      ;

var: localStates    # localVar
     | globalStates # globalVar
     | connector        # connectorVar
     ;
localStates: NAME;
globalStates: 'Top' ('.' brick)+ '.' NAME;
connector: brick '.' NAME;

value: {isKeyWord()}? userDefinedValue
         |basicValue
         ;

userDefinedValue: NAME;
basicValue: arithmeticExpr | booleanExpr;

booleanExpr: booleanExpr op=('and' | 'or') booleanExpr
                       | BOOLEAN
                       | relationExpr
                       | 'not' booleanExpr
                       | '(' booleanExpr ')'
                       ;
relationExpr: arithmeticExpr
                      | arithmeticExpr OPCOMPARE arithmeticExpr
                      ;
arithmeticExpr: arithmeticExpr op=('*'|'/') arithmeticExpr
                           | arithmeticExpr op=('+'|'-') arithmeticExpr
                           | 'min' '(' arithmeticExpr (',' arithmeticExpr)* ')'
                           | 'max' '(' arithmeticExpr (',' arithmeticExpr)* ')'
                           | globalStates
                           | connector
                           | localStates
                           | NUMBER
                           | '(' arithmeticExpr ')'
                           ;
brick: NAME;

我的输入文件 t.expr 内容为:(states = failed) and (states1 = nominal) or (states2 = nominal)

I get the tree in Command line using 'grun'.

如果您 label 您的解析器规则 expr:

expr
 : pair (operators+=('and' | 'or') pair)* #logicalExpr
 | '(' pair ')'                           #parensExpr
 ;

您的(生成的)侦听器 class 将包含这些方法:

void enter_logicalExpr(TransitionConditionParser.LogicalExprContext ctx);

void enter_parensExpr(TransitionConditionParser.ParensExprContext ctx);

enter_logicalExpr 中,您可以从上下文中找到 java.util.List 中的 and/or 标记:ctx.operators.