简体   繁体   English

ANTLR 不匹配输入 'foo(some_foo)' 期望 {'foo'}

[英]ANTLR mismatched input 'foo(some_foo)' expecting {'foo'}

I'm writing a parser using ANTLR and am now at the stage of testing my parser/lexer.我正在使用 ANTLR 编写解析器,现在正处于测试解析器/词法分析器的阶段。 I stumbled over a strange bug while trying to parse basically a variable assignment.我在尝试基本上解析一个变量赋值时偶然发现了一个奇怪的错误。 (Like this) (像这样)

Foo = mpsga(LT);

I get the error : mismatched input 'line 1:6 mismatched input 'mpsga(LT)' expecting 'mpsga'我收到错误: mismatched input 'line 1:6 mismatched input 'mpsga(LT)' expecting 'mpsga'

This is especially strange for when I remove the brackets (or the argument LT ), the parser recognizes mpsga and it only misses the brackets (or the argument).这尤其奇怪,因为当我删除括号(或参数LT )时,解析器识别mpsga并且它只错过了括号(或参数)。


My Grammar looks something like this:我的语法看起来像这样:

Lexer词法分析器

lexer grammar FooLexer;

COMMENT
:
    '#' ~[\r\n]* -> channel ( HIDDEN )
;


NEWLINE
:
    (
        '\r'? '\n'
        | '\r'
    )+ -> channel ( HIDDEN )
;


EQUALSSIGN
:
    '='
;

SEMICOLON
:
    ';'
;

MPSGA_255_1
:
    'LT'
;

MPSGA
:
    'mpsga'
;

WHITESPACE
:
    (
        ' '
        | '\t'
    )+ -> channel ( HIDDEN )
;

BRACKET_OPEN
:
    '('
;

BRACKET_CLOSED
:
    ')'
;

VAR
:
    [a-zA-Z][0-9a-zA-Z_]*
;

Parser解析器

parser grammar FooParser;

options {
    tokenVocab = FooLexer;
}

stmt_block
:
    stmt_list EOF
;

stmt
:
    VAR EQUALSSIGN expr SEMICOLON NEWLINE?
;

stmt_list
:
    stmt
    | stmt_list stmt
;

expr
:
     extvar
;

extvar
:

    MPSGA BRACKET_OPEN mpsga_field BRACKET_CLOSED

;

mpsga_field
:

    MPSGA_255_1
;


When I try to parse this Foo = mpsga(LT);当我尝试解析这个Foo = mpsga(LT); in Java i get the error.在 Java 中,我收到错误消息。 Any help is appreciated!任何帮助表示赞赏!

Edit:编辑:

My Parse hierachy looks like the following:我的 Parse 层次结构如下所示:

Foo = mpsga(LT); Foo = mpsga(LT);

stmt_block
->stmt_list:1
-->stmt
--->"Foo"
--->"="
--->expr
---->extvar
----->"mpsga(LT)"
---->";"
-><EOF>

Foo = mpsga(LT; Foo = mpsga(LT;

stmt_block
->stmt_list:1
-->stmt
--->"Foo"
--->"="
--->expr
---->extvar
----->"mpsga"
----->"("
----->mpsga_field
------>"LT"
----->"<missing ')'>"
---->";"
-><EOF>

DISCLAIMER: I solved the problem.免责声明:我解决了这个问题。 For anyone experiencing the same issue: I had some Lexer rules that were ambiguous for the mpsga part.对于遇到相同问题的任何人:我有一些 Lexer 规则对于mpsga部分不明确。

这是论点:你的语法接受'foo'或'foo2'作为常量,而不是some_foo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM