antlr4 - Grammar for a JSON-like language -
i trying devise grammar json-like language. main differences property names need not double quoted (they can though), , numbers integers (no floating point numbers).
this 1 example:
{ "property1": "string value", property2: 321, arr: [1,2,3] }
this (attempt at) grammar:
grammar command; command: object; object: '{' pair (',' pair)* '}' ; pair: name ':' value ; name : '"' id '"' | id ; value : string | integer | object | array | bool ; array: '[' value (',' value)* ']' ; string: string ; integer : 0 | nonzero ; bool : 'true' | 'false' ; id : [a-za-z0-9_]+ ; string: '"' (esc | .)*? '"' ; fragment esc: '\\"' | '\\\\' ; zero: '0' ; nonzero: '-'? [1-9] [0-9]* ; ws : [ \t\n\r]+ -> skip ;
however, trying run testrig on example input, get
line 2:2 no viable alternative @ input '"property"' line 3:10 no viable alternative @ input '321' line 4:8 no viable alternative @ input '1' line 4:10 no viable alternative @ input '2' line 4:12 no viable alternative @ input '3'
any ideas going wrong?
thanks time!
tuomas
the lexer creating single
string
token"property"
, should adjustname
rule:name : string | id;
you need move
id
rule afterzero
,nonzero
. since numbers matchid
lexer rule, they'll assigned token type according first rule appearing in grammar. want first rulezero
ornonzero
, it'sid
. (since of numbers resulting inid
tokens, ,id
not allowedvalue
, syntax errors.)
Comments
Post a Comment