简体   繁体   中英

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. 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'

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).


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); in Java i get the error. Any help is appreciated!

Edit:

My Parse hierachy looks like the following:

Foo = mpsga(LT);

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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