-
Notifications
You must be signed in to change notification settings - Fork 66
Grammar Examples
To show how to create a grammar with its rules and semantic actions, and to see what Pegged can manage, I added an /examples/
directory containing, as of this writing, the following grammars:
These grammars have about a dozen rules (say, from 5 to 20 rules):
-
arithmetic.d. Parses arithmetic expression, like
1*(2+ x/3) - ( x * y * y -4)
. -
numbers.d. Parses different kind of numbers: integral, floating point, hexadecimal.
-
csv.d. Comma-separated values, in files. TODO : add unit tests.
-
json.d. The standard JSON grammar, taken from [http://json.org]. Only 12 rules!
These are intermediate grammars. They have about 20 - 100 rules.
-
markdown.d. The markdown language grammar. The PEG file comes from this Github project. You can find the official description of Markdown syntax here. TODO: add unit tests.
-
oberon2.d. The Oberon-2 programming language grammar. This grammar was written by Bjoern Lietz-Spendig. You can fin the Oberon-2 description here.
-
xml.d. OK, I'm cheating, it's a 5-rules long XML grammar, but it show how to use semantic actions to validate an input. See Semantic Actions. The entire XML grammar is described here and it's an 80-rules-long grammar, that's why I placed it in the medium-size category (as a way to motivate me to write it in Pegged).
-
constraints.d. Do not use yet!. This was written with an early version of Pegged and the D grammar. That was the first 'real-life' test for Pegged. I used it to parse template constraints
if (A && B || isNumeric!T)
and test all the tree branches to see which ones fail and block instantiation. It then printed the different results, as a diagnostic. It may still work, I haven't tested it for a while.
I'll call large grammars that have hundreds of rules:
- dgrammar.d. DO NOT USE: it's a first pass through the http://dlang.org online spec. It's about 500 rules long! Pegged generates the module in a few seconds alright, but the resulting module do not compile any more (it used to) :-( I'll have to dismantle the beast and test it part by part. But I'll do the C grammar first.
-
A C grammar. The ISO standard gives one.
-
A more complete XML grammar (the standard grammar is an 80-rules medium-size grammar)
-
Also, a DTD grammar. The goal would be to get the following chain : DTD -> XML validator -> Validated XML. I think Pegged can be use to create a compile-time static validator: using the type system to accept only documents following the given DTD.
-
The Javascript grammar, probably (I'm not so sure about
-
printf
/writef
format grammar: a nice example of a small DSL. -
A
.INI
grammar?