json.ebnf (1083B)
1 // JSON — RFC 8259, written the way you actually would. 2 // 3 // The lexer encodes the real JSON string and number productions; the parser is 4 // the natural recursive value grammar. The only concessions to LL(1) are the 5 // `members? / elements?` optionals and the `*`-list tails — both of which are 6 // the standard way to write a comma-separated list and are LL(1)-clean here. 7 %lex { 8 %skip WS = [ \t\r\n] [ \t\r\n]*; 9 10 // string = quote ( unescaped | '\' escape )* quote 11 STRING = "\"" ( [^"\\] | "\\" ( ["\\/bfnrt] | "u" [0-9A-Fa-f] [0-9A-Fa-f] [0-9A-Fa-f] [0-9A-Fa-f] ) )* "\""; 12 13 // number = -? int frac? exp? (no leading zeros, per the spec) 14 NUMBER = "-"? ( "0" | [1-9] [0-9]* ) ( "." [0-9]+ )? ( [eE] [+\-]? [0-9]+ )?; 15 } 16 17 %token TRUE = "true"; 18 %token FALSE = "false"; 19 %token NULL = "null"; 20 21 value = object | array | STRING | NUMBER | "true" | "false" | "null"; 22 23 object = "{" members? "}"; 24 members = member member_tail*; 25 member_tail = "," member; 26 member = STRING ":" value; 27 28 array = "[" elements? "]"; 29 elements = value element_tail*; 30 element_tail = "," value;