lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


LPeg is perfectly capable of accomplishing something like this. Documentation is scarce, unfortunately. I'm using it myself to create a programming language that greatly benefits from the grammar construction flexibility it allows.

If you haven't already, I heartily recommend reading Roberto's paper. Unlike most academic papers, it is clear, easy-to-understand, and helpful:

http://www.inf.puc-rio.br/~roberto/docs/peg.pdf

Some examples:

http://lua-users.org/wiki/LpegRecipes

Another tutorial:

http://www.wra1th.plus.com/lua/peg/PEG.html

You surely have seen the reference page, but:

http://www.inf.puc-rio.br/~roberto/lpeg/

The talk, noted on that page, is also helpful:

http://vimeo.com/1485123

If you've never created a parser before, it's going to be hard. Something like flex/bison (i.e. GNU lex/yacc) has a lot more documentation, but as tools they're a lot more awkward and kludgey. LPeg, like Lua, is elegant. There may be more documentation for flex and bison, but you'll also be learning a lot of annoying configuration stuff and weird quirks that LPeg doesn't have or need. For example, there's no need for separate tokenization step with LPeg, and thus no need to try to get a tokenization program and a parser generator coordinating.

One of the nice things about PEGs as compared to context-free grammars is that the ambiguity resolution is extremely clear from the syntax.

Gotchas:

 - Patterns are instantiated every time you create one, so if you should create non-terminals for things that are repeated a lot, like whitespace.

 - Error messages can be a little cryptic, but they're also rare.

On Nov 28, 2012 8:43 PM, "Chris Babcock" <cbabcock@asciiking.com> wrote:
I have an existing DSL (Diplomacy orders) for which I need to
implement a parser. The language specification consists of a syntax
document and a vocabulary document:

http://aqmn.asciiking.com/data/syntax
http://aqmn.asciiking.com/data/map

I need to write a parser that applies the vocabulary to the syntax and
turns an order stream into an object. There are multiple vocabulary
documents written in a similar format. I think what I need to write
may actually be a parser generator. What I need to know, since I will
be spending many hours on this with little prior background, is LPeg
the right tool for the job? If so, are there beginner documents or
some exercises in writing a trivial parser that might provide me with
a gentler learning curve than the official docs and the academic
papers?

Of course if someone on the list happens to be a fan of the Diplomacy
board game, I'd welcome a little hands on assistance. :-)

And thank you, Steve Donovan, for the wiki article:

http://lua-users.org/wiki/LpegTutorial

Chris
--
Preference will be shown to scoring systems that emphasize solo victories.
    - Diplomacy World Cup Charter, section 7