I want to parse this with ANTLR4:
; this is a comment
dummykey=dummy string 100 #!; this is a comment
firstname=peter mike
name=mc miller
This is the grammar:
grammar KEYVALUE;
COMMENT: ';' .*? ('r'? 'n' | EOF) -> skip;
file: line* EOF;
line: Space* keyValuePair;
keyValuePair:
key EQ value
{
String k = $key.text;
String v = $value.text;
System.out.println("nkey : `" + k + "`");
System.out.println("value : `" + v + "`");
};
key: AlphaNum+;
value: valueChar+ EOL;
valueChar: Char;
EOL: LineBreak | EOF;
EQ: '=';
LineBreak: 'r'? 'n' | 'r';
Space: ' ' | 't' | 'f';
AlphaNum: [a-zA-Z0-9];
Char: .;
When i start it with: more test.txt |grun parser.KEYVALUE file -gui
I get these error messages and there are no values.
key : dummykey
value : “
line 2:14 mismatched input ‘ ‘ expecting {‘=’, AlphaNum}
line 2:21 mismatched input ‘ ‘ expecting {‘=’, AlphaNum}
line 2:26 mismatched input ‘ ‘ expecting {‘=’, AlphaNum}
line 2:27 extraneous input ‘#’ expecting {Space, AlphaNum}
line 3:10 mismatched input ‘p’ expecting Char
key : firstname
value : “
line 3:15 mismatched input ‘ ‘ expecting {‘=’, AlphaNum}
line 3:20 mismatched input ‘rn’ expecting {‘=’, AlphaNum}
line 4:5 mismatched input ‘m’ expecting Char
key : name
value : “
line 4:7 mismatched input ‘ ‘ expecting {‘=’, AlphaNum}
line 4:14 mismatched input ” expecting {‘=’, AlphaNum}
2