1

I'm trying to define a simple grammar to parse expressions like

s=1,b=2

or

s=1,b=[1,2,3]

The grammar is (not full):

grammar KeyVal;
program : exprs EOF ;
exprs : expr (',' expr)* ;
expr  :  KEY '=' VALUE  ;         // match key=value

KEY : [a-zA-Z]+ ;             // match lower-case identifiers

VALUE : NUMBER | LIST ;

LIST : '[' NUMBER (',' NUMBER)* ']';



NUMBER   : INTEGER   ;

INTEGER    : DECIMAL_INTEGER    ;


DECIMAL_INTEGER   : NON_ZERO_DIGIT DIGIT* | '0'+
;

fragment NON_ZERO_DIGIT   : [1-9]    ;
/// digit          ::=  "0"..."9"
fragment DIGIT     : [0-9]    ;

WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

Java program which uses generated classes:

    String s = " s=1,a=2";
    KeyValLexer lexer = new KeyValLexer(CharStreams.fromString(s));
    CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
    KeyValParser parser = new KeyValParser(commonTokenStream);
    ProgramContext tree = parser.program();

provides error

line 1:8 mismatched input '<EOF>' expecting KEY

How can i avoid this error?

qmor
  • 568
  • 6
  • 20
  • Do you have a full working example, with pom.xml file? Also did you try without EOF like explained here: https://stackoverflow.com/questions/17844248/when-is-eof-needed-in-antlr-4 – pdem Sep 03 '18 at 12:26
  • 1
    The same error line 1:23 mismatched input '' expecting KEY. It looks works, because tree contains tokens, but this error message confusing me. – qmor Sep 03 '18 at 12:37
  • 1
    Has changed String s = "s = 12,b=12,c=54,n=[45]"; and System.out.println(tree.toStringTree()); for now its prints ([] ([6] ([8 6] s = 12) , ([10 6] b = 12) , ([10 6] c = 54) , ([10 6] n = [45]))) – qmor Sep 03 '18 at 12:38
  • The grammar you posted does not produce any errors on the input `" s=1,a=2"`. I suspect the problem lies in the parts of the code that you left out. – sepp2k Sep 03 '18 at 13:53
  • 1
    you're right. In fact (and it's strange) it's only gives error in console during debug (i'm using eclipse), not in "Run" configuration – qmor Sep 03 '18 at 16:22
  • @qmor The behaviour of a Java program should not change in Debug mode unless there's like a timing-related issue (which does not seem applicable here). Does your debug configuration perhaps run a different class? Are there any other notable differences between your run configuration and your debug configuration? – sepp2k Sep 03 '18 at 17:26

1 Answers1

0

LIST should be a parser rule. Something like this should do it:

grammar KeyVal;

program
 : exprs EOF
 ;

exprs
 : expr ( ',' expr )*
 ;

expr
 :  KEY '=' value
 ;

value
 : DECIMAL_INTEGER
 | list
 ;

list
 : '[' value ( ',' value )* ']'
 ;

KEY
 : [a-zA-Z]+
 ;

DECIMAL_INTEGER
 : [1-9] [0-9]*
 | '0'+
 ;

WS
 : [ \t\r\n]+ -> skip
 ;
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288