Skip to content
PhilippeSigaud edited this page Apr 9, 2012 · 21 revisions

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:

Simple 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.

  • parametrized.d. Examples of parameterized rules, as described on the Parametrized Rules page.

  • csv.d. Comma-separated values, in files.

  • json.d. The standard JSON grammar, taken from json.org. Only 12 rules!

  • PEG.d the basic PEG grammar, as described by Ford.

  • PEGGED.d the somewhat extended PEG grammar used by Pegged itself, as described in Extended PEG Syntax.

Medium-size Grammars:

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.

  • c.d. A simple C grammar, most probably full of bugs. I'll change some rule structure as time permit (for example, JumpStatement is too wide for my taste). Btw, that's about 50 rules.

  • 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.

Large grammars:

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 does not compile any more (it used to with the first Github commits) :-( I'll have to dismantle the beast and test it part by part. But I'll do the C grammar first.

To Be Added:

  • 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 used 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?

Clone this wiki locally