ANTLR4 : mismatched input -
i match input of form ::
commit a1b2c3 author: michael <michael@test.com> commit d3g4 author: david <david@test.com> here grammar have written:
grammar commit; file : commitinfo+; commitinfo : commitdesc authordesc; commitdesc : 'commit' commithash newline; authordesc : 'author:' authorname '<' email '>' newline; commithash : [a-z0-9]+; authorname : [a-za-z]+; email : [a-za-z0-9.@]+; newline : '\r'?'\n'; whitespace : [ \t]->skip; the problem above parser that, above input matches perfectly. when input changes :
commit c1d2 author: michael <michael@test.com> it throws error :
line 2:8 mismatched input 'michael' expecting authorname.
when print tokens, seems string 'michael' gets matched token commithash instead of authorname.
how fix above case?
antlr4 matches lexer rules according sequence in have been written.
'michael' gets matched rule commithash : [a-z0-9]+ ; appears before rule authorname , hence having error.
i can think of following options resolve issue facing :
you can use
'mode'feature in antlr : in antlr 4, 1 lexer mode active @ time, , longestnon-fragment lexer rulein mode rule determine token created. grammar includes default mode, lexer rules active , hence 'michael' gets matchedcommithashlength of token matched samecommithash,authornamecommithashappears beforeauthornamein grammar.you can alter lexical rules interchanging way in appear in grammar. assuming
commithashrule has numeral matched it. putauthornamebeforecommithashin following way :grammar commit; ... authorname : [a-za-z]+; commithash : [a-z0-9]+; ...
note: feel lexer rules not crisply written. sure commithash rule should [a-z0-9]+; mean token 'abhdks' matched commithash rule. that's different issue altogether.
Comments
Post a Comment