PEG Library

Revision as of 15:35, 11 August 2009 by Sandrod (Talk | contribs)

This page describes the Parsing Expression Library implementation for Eiffel. Information about PEGs can be found here [1].

Basic classes

All the parsers inherit from PEG_ABSTRACT_PEG which defines the common functionalities. The parsers are the same as in the definition of Wikipedia with the additional classes like whitespace support.

The parsers are combined to a object hierarchy which defines the grammar. A string can then be parsed via the the feature parser.parse_string ("Some source") on the root object.

Internal DSL

Objects can be combined via features, but the easier way is to use the defined operators. For instance if we want to define the simple grammar 'a' 'b' 'c'* we will simply write: a + b + (-c) Where a, b, c are already defined as character parsers parsing the right character (PEG_CHARACTER). The '+' operator concatenates the parsers to a sequence (PEG_SEQUENCE), weil the prefix operator '-' wraps c into a one or more parser (PEG_ONE_OR_MORE). All the operators are:

  • binary '+': Sequence concatenation
  • binary '|': Choice concatenation
  • prefix '+': wraps one or more
  • prefix '-': wraps zero or more


Additionally there is the operator '|+' which acts like the binary '+' operator. In contrast to it, it inserts an whitespace* parser between the two operands. As it is often needed it makes sense to define it as an operator.

Building a domain model