Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
35 lines (30 sloc) 1.35 KB
greg is a re-entrant peg/leg, with some bug fixes.
the most comprehensive example of greg usage is in
nagaqueen, an ooc grammar, used in rock, an ooc
compiler written in ooc.
peg/leg is copyright (c) 2007 by Ian Piumarta
released under an MIT license. as is greg.
This fork of greg supports the memoization of
failed rule application attempts. For a language
with more elaborated syntax and operators, a PEG
parser can spend exponentially long time to try
the same rule application at the same input
location. The typical solution for this problem
is to assume that a grammar rule always returns
the same result (or fail) at the same location and
remember the value (or failure), effectively in
the 2D table of (location, rule_id) -> value.
An interesting feature that Ian's leg has is that it
defers the semantic action execution until the
system "commits" a successfull parse. (That is
why putting printf() in a failed choice in a rule
does not show any debug log, BTW.) Because of
this, we cannot remember the successful result; as
it is calculated later while attempting a lot of
reduandant failures. So, instead, this fork only
remembers the fact that the rule failed; next time
it trys the same rule at the location, it
short-cuts the work and fails.