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 adjust name rule:

    name : string | id; 
  • you need move id rule after zero , nonzero. since numbers match id lexer rule, they'll assigned token type according first rule appearing in grammar. want first rule zero or nonzero, it's id. (since of numbers resulting in id tokens, , id not allowed value, syntax errors.)


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -