Example output¶
ANTLR¶
This output is generated with the following directive:
.. syntax:autogrammar:: Json.g4
:root-rule: value
:cc-to-dash:
- grammar Json¶
JSON (JavaScript Object Notation) is a lightweight data-interchange format.
Top level
- value¶
On top level, JSON consists of a single value. That value can be either a complex structure (such as an
object
or anarray
) or a primitive type (astring
in double quotes, anumber
, ortrue
orfalse
ornull
).
Complex objects
- object¶
Object is a collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
- array¶
Array is an ordered list of values. In most languages, this is realized as vector, list, array or sequence.
'[' value ',' ']'
Primitive types
- number¶
A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.
'-' '0' [1-9] [0-9] '.' [0-9] 'e' 'E' '+' '-' [0-9]
- string¶
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.
'"' Any unicode character except " and \ '\' '"' quotation mark '\' reverse solidus '/' solidus 'b' backspace 'f' formfeed 'n' newline 'r' carriage return 't' horizontal tab 'u' 4 hexadecimal digits '"'
/**
* JSON (JavaScript Object Notation) is a lightweight data-interchange format.
*/
grammar Json;
/// **Top level**
/**
* On top level, JSON consists of a single value. That value can be either
* a complex structure (such as an `object` or an `array`) or a primitive
* type (a :syntax:r:`STRING` in double quotes, a :syntax:r:`NUMBER`,
* or ``true`` or ``false`` or ``null``).
*/
value
: object
| array
| NUMBER
| STRING
| TRUE
| FALSE
| NULL
;
/// **Complex objects**
/**
* Object is a collection of name/value pairs. In various languages,
* this is realized as an object, record, struct, dictionary,
* hash table, keyed list, or associative array.
*/
object
: '(' (STRING ':' value (',' STRING ':' value)*)? ')'
;
/**
* Array is an ordered list of values. In most languages, this is realized as
* vector, list, array or sequence.
*/
array
: '[' (value (',' value)*)? ']'
;
/// **Primitive types**
/**
* A number is very much like a C or Java number,
* except that the octal and hexadecimal formats are not used.
*/
NUMBER
: NEG? ('0' | [1-9] [0-9]*) ('.' [0-9]+)? EXPONENT?
;
//@ doc:inline
//@ doc:importance 0
fragment NEG
: '-'
;
//@ doc:inline
//@ doc:importance 0
fragment EXPONENT
: ('e' | 'E') ('+' | '-')? [0-9]+
;
/**
* A string is a sequence of zero or more Unicode characters,
* wrapped in double quotes, using backslash escapes.
* A character is represented as a single character string.
* A string is very much like a C or Java string.
*/
STRING
: '"' (SAFECODEPOINT | ESC)* '"'
;
//@ doc:inline
fragment ESC
: '\\' ESC_CONTENTS
;
//@ doc:inline
fragment ESC_CONTENTS
: '"' /** quotation mark */
| '\\' /** reverse solidus */
| '/' /** solidus */
| 'b' /** backspace */
| 'f' /** formfeed */
| 'n' /** newline */
| 'r' /** carriage return */
| 't' /** horizontal tab */
| 'u' UNICODE
;
//@ doc:name 4 hexadecimal digits
fragment UNICODE
: 'u' HEX HEX HEX HEX
;
fragment HEX
: [0-9a-fA-F]
;
//@ doc:name Any unicode character except " and \
fragment SAFECODEPOINT
: ~ ["\\\u0000-\u001F]
;
TRUE
: 'true'
;
FALSE
: 'false'
;
NULL
: 'null'
;
Bison¶
This output is generated with the following directive:
.. syntax:autogrammar:: Calc.y
:root-rule: value
:cc-to-dash:
- grammar Calc¶
Infix notation calculator.
- input¶
Grammar input.
expression ' \n '
- expression¶
A single expression.
'-' number '(' expression ')' right-assoc '^' '*' '/' '+' '-'
- number¶
A single integer number.
[1-9] [0-9] '0'
/** Infix notation calculator. */
/**
* A single integer number.
*/
//@ doc:content [1-9][0-9]* | '0'
%token NUMBER
%%
/** Grammar input. */
input
: input line
| %empty
;
//@ doc:inline
line
: expression '\n'
| '\n'
;
/** A single expression. */
expression
: expression '+' exp1
| expression '-' exp1
| exp1
;
//@ doc:inline
exp1
: exp1 '*' exp2
| exp1 '/' exp2
| exp2
;
//@ doc:inline
exp2
: '-' exp3
| exp3
;
//@ doc:inline
exp3
: exp4 /** right-assoc */ '^' exp3
| exp4
;
//@ doc:inline
exp4
: NUMBER
| '(' expression ')'
;
%%