使用解析器进行OData v4语法

时间:2014-11-29 13:25:57

标签: java parsing antlr4

我尝试使用OASIS组提供的Antlr4的OData v4语法。请参阅以下链接:https://tools.oasis-open.org/version-control/browse/wsvn/odata/trunk/spec/grammar/ANTLR/#_trunk_spec_grammar_ANTLR_

基于这些文件和Antlr v4 Maven插件,我成功生成了用于解析OData URL的类。

我尝试使用如下所述的解析器:

String expression = "http://192.168.1.1/odata/Category(1)/Products?$top=2&$orderby=name";
ANTLRInputStream in = new ANTLRInputStream(expression);

ODataParserLexer lexer = new ODataParserLexer(in);
ODataParserParser parser = new ODataParserParser(
            new CommonTokenStream(lexer));

ODataErrorListener errorListener = new ODataErrorListener();
parser.addErrorListener(errorListener);

ODataParseListener listener = new ODataParseListener();
parser.addParseListener(listener);

OdataUriContext ctx = parser.odataUri();

调用odataUri方法时,我在错误监听器中报告了以下错误:

line 1:66 mismatched input '<EOF>' expecting Protocol

这很奇怪,因为词法分析器能够获取字符串解析的标记:

"http" 
"://" 
"192.168.1.1" 
"/" 
"odata" 
"/" 
"Category" 
"(" 
"1" 
")" 
"/" 
"Products" 
"?" 
"$top" 
"=" 
"2" 
"&" 
"$orderby" 
"=" 
"name"

也许odataUri的方法不是调用解析器的方法。但在阅读了解析器语法文件之后,似乎就是这种情况。

- 于12/01编辑

我检测到规则名称存在问题:

odataUri : Protocol ColSlaSla host ( COLON port )?
       serviceRoot
       ( ODataSignal_METADATA | ODataSignal_BATCH | odataRelativeUri )? EOF;

Protocol :  

无法找到规则Protocol。如果我将其名称更新为protocol,那就更好了......

按照巴特的建议,我打印了与令牌相关的规则名称。使用Antlr4 maven插件生成,我无法获得正确的插件。对于经典一代,我有这个:

"http" 
    index = 93, ODataParserLexer.tokenNames[index] = HTTPORHTTPS
"://" 
    index = 92, ODataParserLexer.tokenNames[index] = ColSlaSla
"192.168.1.1" 
    index = 23, ODataParserLexer.tokenNames[index] = Ls32
"/" 
    index = 60, ODataParserLexer.tokenNames[index] = '/'
"odata" 
    index = 4, ODataParserLexer.tokenNames[index] = 'odata'
"/" 
    index = 60, ODataParserLexer.tokenNames[index] = '/'
"Category" 
    index = 251, ODataParserLexer.tokenNames[index] = ODATA_ID_CHAR8
"(" 
    index = 28, ODataParserLexer.tokenNames[index] = SubDelims
"1" 
    index = 25, ODataParserLexer.tokenNames[index] = DecOctet
")" 
    index = 28, ODataParserLexer.tokenNames[index] = SubDelims
"/" 
    index = 60, ODataParserLexer.tokenNames[index] = '/'
"Products" 
    index = 251, ODataParserLexer.tokenNames[index] = ODATA_ID_CHAR8
"?" 
    index = 66, ODataParserLexer.tokenNames[index] = '?'
"$top" 
    index = 128, ODataParserLexer.tokenNames[index] = ODataSignal_TOP
"=" 
    index = 28, ODataParserLexer.tokenNames[index] = SubDelims
"2" 
    index = 25, ODataParserLexer.tokenNames[index] = DecOctet
"&" 
    index = 28, ODataParserLexer.tokenNames[index] = SubDelims
"$orderby" 
    index = 126, ODataParserLexer.tokenNames[index] = ODataSignal_ORDERBY
"=" 
    index = 28, ODataParserLexer.tokenNames[index] = SubDelims
"name" 
    index = 250, ODataParserLexer.tokenNames[index] = ODATA_ID_CHAR4

令牌和相关规则似乎是正确的。

我还在解析器(parser.setTrace(true))上启用了跟踪并再次执行我的代码。我还有错误

enter   odataUri, LT(1)=<EOF>
enter   protocol, LT(1)=<EOF>
line 1:66 mismatched input '<EOF>' expecting HTTPORHTTPS
------------
Error on query : 
null
=> line 1 : mismatched input '<EOF>' expecting HTTPORHTTPS
Context : [590]
exit    protocol, LT(1)=<EOF>
exit    odataUri, LT(1)=<EOF>

非常感谢你的帮助。 亨利

1 个答案:

答案 0 :(得分:1)

指定的语法有很多不明确的匹配,需要重写以消除可能使用语义谓词或词法分析器模式的模糊匹配。对于expamle(我重写了语法启动规则):

odataUri : serviceRoot? EOF  ;

serviceRoot : Protocol host segments relative? # OnSerivceRoot ;

segments    : Segments ;

host         : (addr | regName) port?;
addr         : ColSlaSla IPv4address ;

regName      : HOST ;

port          : PortDef ;

relative : (ODataSignal_METADATA | ODataSignal_BATCH) | odataRelativeUri;

odataRelativeUri : resourcePath ( question queryOptions )?;
question : QUESTION ;


PortDef     : COLON Digits ;
Segments    : SLASH ((Unreserved | PctEncoded | SubDelims | COLON | AT_SIGN)+ SLASH)* ;
HOST         : ColSlaSla HOST_DEF ;
HOST_DEF     : (Unreserved | PctEncoded | SubDelims)+ ;
QUESTION : '?';
Protocol :  HttpOrHttpsAnyCase;
Digits  : Digit+ ;
Digit  : [0-9] ;
Alpha  : [a-zA-Z];