pegc

pegc
Login

pegc

Welcome to the pegc fossil repository

pegc is an experimental toolkit for writing PEG parsers in C using something similar to functional composition, conceptually similar to C++ parsing toolkits like Boost.Spirit, PEGTL, and parsepp. While several C++ implementations exist for this model, it is currently (September 2008) believed that pegc is the the first C library of its kind :).

Author: Stephan Beal (http://wanderinghorse.net/home/stephan)

License: The pegc code itself is released into the Public Domain. However, pegc uses some hashtable code which has an MIT license.

Overview

pegc attempts to implement a model of parser which has become quite popular in C++, but to do so within the limitations of C (e.g. lack of type safety in many places, and no safe casts).

The basic idea is that one defines a grammar as a list of Rule objects. A grammar starts with a top rule, and that rule may then delegate all parsing, as it sees fit, to other rules. The result of a parse is either 'true' (the top-most rule matches) or false (the top-most rule fails). It is roughly modelled off of recursive descent parsers, and follows most of those conventions. For example, a parsing rule which does not match (i.e. it returns false) must not consume input. Most rules which do match, on the other hand, do consume (there are several exceptions to that rule, though).

In C++ we would build the parser using templates (at least that's how i'd do it). In C we don't have that option, so we build up little objects which contain a Rule function and some data for that function. Those rules can then be processed in an RD fashion.

The peg/leg project is similar to pegc but solves the problem from the exact opposite direction - it uses a custom PEG grammar as input to a code generator, whereas pegc is not a generator (but could be used to implement one).

My theory is that once the basic set of rules are in place, it will be relatively easy to implement a self-hosted code generator which can read a lex/yacc/lemon-like grammar and generate pegc-based parsers. That is, a PEGC-parsed grammar which in turn generates PEGC-based parsers.

Features

Requirements

Current Status

pegc is very much beta software, but the core parsing features are all in place and "seem to work", so it's mostly just a matter of refinement now. "Refinement" is a friendly way of saying, "i change the public API a lot as i experiment with different techniques."

News

Download

See this page for instructions.

TODOs