gpt4 book ai didi

c# - C# 中 antlr 语法/解析器的无效转换问题

转载 作者:行者123 更新时间:2023-11-30 12:12:05 27 4
gpt4 key购买 nike

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

Unable to cast object of type 'operandExpression_return' to type '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

解析器中的异常抛出代码片段在私有(private) 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);
...

最佳答案

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

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);} )*
;

关于c# - C# 中 antlr 语法/解析器的无效转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14225200/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com