lua-users home
lua-l archive

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

> After watching the talk about LPeg I was inspired to use it to
> describe the grammar of my DSL.
> I now have a complete PEG grammar (LPEG.RE) of about 300 lines, but
> can't find a good pointer on how to generate an AST or parse tree with
> callbacks from it.
> Other PEG implementations do offer this functionality (e.g.
> [...]
> Do I have to do this for every non-terminal I specified? Is there
> a better way to do this?

There is no automatic way to generate an AST in LPeg; you have to do it
"manually". But there are several ways to do it. One way is what you are
doing. Another possiblility is like this. Assume an original grammar
segment like here:

  S <- A B C
    |  E e F

You may rewrite it like this:

  S <- ( {:tag: ''->'R1':} A B C ) -> {}
     | ( {:tag: ''->'R2':} E {e} F ) -> {}

These captures will generate the entire AST for you. Each inner node
is a table with a 'tag' field (which describes the production that
generated that node) and fields [1], [2], etc. with the node sibblings.

You may also play with re.lua and add another rule to ease your job. For
instance, you might write only

  S <- ( A B C ) @R1
     | ( E {e} F ) @R2

and add a rule for 'Suffix' in re.lua to treat '@name' as 'create a
capture table with a tag with the given name'.

Yet another option is to follow your style, but to play with the __index
metamehtod of the table that you pass to re.compile so that you do not
need to define one function for each production; the metamethod creates
the function for you.

-- Roberto