[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Simple peg examples
- From: William Ahern <william@...>
- Date: Tue, 12 Jun 2012 14:04:41 -0700
On Tue, Jun 12, 2012 at 01:13:21PM +0200, steve donovan wrote:
> On Mon, Jun 11, 2012 at 4:32 AM, Glenn Edgar <firstname.lastname@example.org> wrote:
> > I was trying to use peg module. I could not figure it out. Are there
> > simple complete programs.
> I have just added a tutorial to the Wiki:
Part of the problem with PEGs, I think, is that people approach PEGs like
souped-up regular expressions. The problem is compounded the more experience
one has with regexes.
I was tweaking the LPeg Leg grammar. The author did a fine job of
translating the left-recursive elements to right-recursive. Except the final
solution used a nasty hack for prefixexp / Name disambigutation. The code
uses a global variable and a match-time assertion to make sure the correct
choice is followed.
The proper, pure PEG solution is a negative lookahead assertion on the set
of keywords in the grammar. And so that's what I ended up doing; and in fact
the entire grammar could be made much simpler once you exclude the keywords
from the set of allowable identifiers, although in this case I didn't
refactor things much.
Almost every single PEG and LPeg example I've seen treats lookahead
assertions as something equivalent to the ^ operator in Perl regexes; a
simple way to invert a character set. But lookahead assertions are much more
powerful than that, and in fact critical to using PEGs properly for complex
grammars. But experience with regular expressions is often a hindrance to
seeing the proper solution, even when it's staring you right in the face.
Not sure how to concisely describe the utility of lookahead assertions in a
tutorial format, though.