Clone this wiki locally
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.
strings.d. Parses double-quoted strings. An example of how to do escaping and looking for an end marker.
csv.d. Comma-separated values, in files.
These are intermediate-size grammars. They have about 20 - 100 rules.
c.d. A simple C grammar, most probably full of bugs. I'll change some rule structure as time permit (for example,
JumpStatementis too wide for my taste). Btw, that's about 50 rules.
Grammars that have hundreds of rules:
- dgrammar.d. 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 is a bit slow to load while linking (as in, 1-2 secs). It's probably buggy, though I corrected dozens of errors.
To Be Added:
The XML grammar (xml2.d) also contains a DTD grammar. The goal would be to get the following chain : DTD -> XML validator -> Validated XML. I think Pegged can be used to create a compile-time static validator: using the type system to accept only documents following the given DTD.
writefformat grammar: a nice example of a small DSL.