4

我正在使用 Anlr 3.5 从以下包含的语法生成解析器和词法分析器。该语法用于读取字符串,以便将它们转换为对象图以供以后评估。但是,当我尝试使用unaryExpression子句时遇到问题,这样做会引发类转换异常报告:

无法将“operandExpression_return”类型的对象转换为“unaryExpression_return”类型。

导致这种情况的一个示例输入是

([p1 eq \"p1Value\"]) and [p2 eq \"p2Value\"] or [p3 > \"p3\"\"Val\"\"ue\"]

它似乎是显式触发异常的括号(unaryExpression)。运行相同的语句减去括号似乎可以正确解析,括号内的子表达式也是如此。

我可以通过异常清楚地看到使用了错误的类型。我只是不理解为什么解析器会选择它,也不知道语法需要什么更正来避免这个错误。

值得注意的是,表达式子句的两个分支确实与每个 binaryOperator 分支都有多个匹配的替代方案,但据我了解,这应该没问题,只会导致效率较低的贪婪解析器,而不会导致我看到的错误。

使用的语法:

grammar QueryExpressionGrammar;

options {
    language=CSharp3;
    TokenLabelType=CommonToken;
    output=AST;
    ASTLabelType=CommonTree;
}

@lexer::namespace{namespace}
@parser::namespace{namespace}

@parser::members {
public static string CleanQuotedString(string input)
{
    return input == null
        ? null
        : input.Substring(1, input.Length - 2).Replace("\"\"", "\"");
}
}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

public parse returns [IQueryExpression value]
    : exp=expression EOF {$value = $exp.value;}
    ;

expression returns [IQueryExpression value]
    : lhs=operandExpression { $value = $lhs.value; } ( op=binaryOperator rhs=expression {$value = new BinaryOperationQueryExpression($lhs.value, $op.value, $rhs.value);} )*
    | lhs=unaryExpression { $value = $lhs.value; } ( op=binaryOperator rhs=expression {$value = new BinaryOperationQueryExpression($lhs.value, $op.value, $rhs.value);} )*
    ;

binaryOperator returns [BinaryOperator value]
    : BinaryOperatorAnd {$value = BinaryOperator.And;}
    | BinaryOperatorOr {$value = BinaryOperator.Or;}
    ;

unaryExpression  returns [IQueryExpression value]
    : UnaryOperatorNot sub=expression {$value = new UnaryOperationQueryExpression(UnaryOperator.Not, $sub.value);}
    | OPEN_PAREN sub=expression CLOSE_PAREN {$value = new UnaryOperationQueryExpression(UnaryOperator.Paren, $sub.value);}
    ;

operandExpression returns [IQueryExpression value]
    : OPEN_BRACKET p=PROPERTY OperandQueryExpressionOperatorEq v=QUOTED_STRING CLOSE_BRACKET {$value = new OperandQueryExpression($p.text, OperandQueryExpressionOperator.Eq, CleanQuotedString($v.text));}
    | OPEN_BRACKET p=PROPERTY OperandQueryExpressionOperatorLt v=QUOTED_STRING CLOSE_BRACKET {$value = new OperandQueryExpression($p.text, OperandQueryExpressionOperator.Lt, CleanQuotedString($v.text));}
    | OPEN_BRACKET p=PROPERTY OperandQueryExpressionOperatorLe v=QUOTED_STRING CLOSE_BRACKET {$value = new OperandQueryExpression($p.text, OperandQueryExpressionOperator.Le, CleanQuotedString($v.text));}
    | OPEN_BRACKET p=PROPERTY OperandQueryExpressionOperatorGt v=QUOTED_STRING CLOSE_BRACKET {$value = new OperandQueryExpression($p.text, OperandQueryExpressionOperator.Gt, CleanQuotedString($v.text));}
    | OPEN_BRACKET p=PROPERTY OperandQueryExpressionOperatorGe v=QUOTED_STRING CLOSE_BRACKET {$value = new OperandQueryExpression($p.text, OperandQueryExpressionOperator.Ge, CleanQuotedString($v.text));}
    | OPEN_BRACKET p=PROPERTY OperandQueryExpressionOperatorLike v=QUOTED_STRING CLOSE_BRACKET {$value = new OperandQueryExpression($p.text, OperandQueryExpressionOperator.Like, CleanQuotedString($v.text));}
    | OPEN_BRACKET p=PROPERTY OperandQueryExpressionOperatorIlike v=QUOTED_STRING CLOSE_BRACKET {$value = new OperandQueryExpression($p.text, OperandQueryExpressionOperator.Ilike, CleanQuotedString($v.text));}
    | OPEN_BRACKET p=PROPERTY NonValueBasedOperandQueryExpressionOperatorIsNull CLOSE_BRACKET {$value = new OperandQueryExpression($p.text, OperandQueryExpressionOperator.IsNull, null);}
    | OPEN_BRACKET p=PROPERTY NonValueBasedOperandQueryExpressionOperatorNotNull CLOSE_BRACKET {$value = new OperandQueryExpression($p.text, OperandQueryExpressionOperator.NotNull, null);}
    ;

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/

UnaryOperatorNot
    : 'not'
    | '!'
    ;

BinaryOperatorAnd
    : 'and'
    | '&&'
    | '&'
    ;

BinaryOperatorOr
    : 'or'
    | '||'
    | '|'
    ;

// OperandQueryExpressionOperator that uses a comparison value
OperandQueryExpressionOperatorEq
    : 'eq'
    | '=='
    | '='
    ;

OperandQueryExpressionOperatorLt
    : 'lt'
    | '<'
    ;

OperandQueryExpressionOperatorLe
    : 'le'
    | '<='
    ;

OperandQueryExpressionOperatorGt
    : 'gt'
    | '>'
    ;

OperandQueryExpressionOperatorGe
    : 'ge'
    | '>='
    ;

OperandQueryExpressionOperatorLike
    : 'like'
    ;

OperandQueryExpressionOperatorIlike
    : 'ilike'
    ;

// OperandQueryExpressionOperator that does not use a comparison value
NonValueBasedOperandQueryExpressionOperatorIsNull
    : 'null'
    | 'isnull'
    ;

NonValueBasedOperandQueryExpressionOperatorNotNull
    : 'notnull'
    ;

OPEN_BRACKET: '[';
CLOSE_BRACKET:  ']';
OPEN_PAREN: '(';
CLOSE_PAREN: ')';
PROPERTY: LETTER (ALPHA_NUMERIC | SPECIAL)*; // property definition is in line with the definition of a property definition for c#
QUOTED_STRING: QUOTE (~QUOTE | (QUOTE QUOTE))* QUOTE; // values are characters, or if they contain quotes they are escaped by double quoting and surrounding value in quotes

fragment DIGIT: ('0'..'9');
fragment LETTER: (('a'..'z')|('A'..'Z'));   
fragment ALPHA_NUMERIC: (LETTER|DIGIT);
fragment SPECIAL: ('_'|'-');    
fragment QUOTE: '\u0022';

WHITESPACE: (' ' | '\t' | '\n' | '\r'){ Skip(); }; // valid whitespace characters

私有 QueryExpressionGrammarParser.expression_return expression() 方法中解析器内的异常抛出代码片段

...
switch (alt3)
{
case 1:
    DebugEnterAlt(1);
    // QueryExpressionGrammar.g:37:4: lhs= operandExpression (op= binaryOperator rhs= expression )*
    {
    root_0 = (CommonTree)adaptor.Nil();

    DebugLocation(37, 7);
    PushFollow(Follow._operandExpression_in_expression111);
    lhs=operandExpression();
    PopFollow();

    adaptor.AddChild(root_0, lhs.Tree);
    DebugLocation(37, 26);


    /// v v v Exception throwing on line below v v v
     retval.value = (lhs!=null?((QueryExpressionGrammarParser.unaryExpression_return)lhs).value:default(IQueryExpression)); 


    DebugLocation(37, 51);
    // QueryExpressionGrammar.g:37:51: (op= binaryOperator rhs= expression )*
    try { DebugEnterSubRule(1);
...
4

1 回答 1

3

所以这是我们在评论中找到的解决方案。改变这个:

expression returns [IQueryExpression value]
    : lhs=operandExpression { $value = $lhs.value; } ( op=binaryOperator rhs=expression {$value = new BinaryOperationQueryExpression($lhs.value, $op.value, $rhs.value);} )*
    | ulhs=unaryExpression { $value = $ulhs.value; } ( op=binaryOperator rhs=expression {$value = new BinaryOperationQueryExpression($ulhs.value, $op.value, $rhs.value);} )*
    ;
于 2013-01-08T23:44:07.700 回答