# LPeg self-referential pattern

I am trying to create a self-referential pattern in lua/lpeg

``````local group = lpeg.P(sep1 * (1 - sep2)^0 * group^0 * sep2)
``````

notice that group refers to itself. This is not possible since group is undefined. lpeg.V does not work outside a grammar definition. Is there any way to solve this chicken and egg problem?

• What you need is a grammar, implemented as a Lua table. I don't have time right now to write a complete working example, but it would be very useful for future reference if you can post such a working example as an answer. – Bruno Le Floch Mar 18 '12 at 6:45
• lpeg says there is no way without the grammar so there probably is little hope. – Uiy Mar 18 '12 at 7:41

The obvious problem is that in the definition

``````local group = lpeg.P(sep1 * (1 - sep2)^0 * group^0 * sep2)
``````

`group` is not known yet. You have to resort to grammars for that.

``````lpeg.P{
"group",
group = lpeg.P(sep1 * (1-sep2)^0 * lpeg.V"group"^0 * sep2
}
``````

(untested). This works because the reference to group is now a string, and therefore can be undefined at the point of definition. Once the table is complete, it has to be an entry of it.

• Could the downvoter please explain why it is voted down? I'd like to improve my answers and any comment would help me. – topskip Mar 18 '12 at 13:42
• Well, You've said nothing that I haven't said in my original post. I was looking for a possible way to do it outside a grammar. There should be no programmatic reason why one can't do it outside grammars besides the fact that lpeg wasn't designed to do so. For example, why couldn't we have an lpeg.VF("group") where VF is used in pattern definitions analogous to lpeg.V in group definitions. I was basically hoping for some trick for lpeg modification and not a rehash of what I said. The reason I down voted was because the "answer" wasn't an answer to my question. – Uiy Mar 21 '12 at 12:00
• My question was "How can one use [non-table] self-referential patterns in lpeg" and now "How can one use self-referential patterns". You know I mean non-table by simply reading my post. The reason why it won't work, directly at least, is because lua tries to evaluate each variable before in the definition of the pattern before the lvalue is created. No other way is possible. BUT lpeg.V works by just using a string of the variable we want to use. lpeg deals with the recursive nature under the hood. Should be no different outside of tables. – Uiy Mar 21 '12 at 12:06
• e.g., `local group = lpeg.P(sep1 * (1 - sep2)^0 * lpeg.V("group")^0 * sep2)` should work but doesn't. After all whats the difference between defining group as I just did and doing `p = {"group" = lpeg.P(sep1 * (1 - sep2)^0 * lpeg.V("group")^0 * sep2)}` except in one case lpeg.V has to look up a variable(shouldn't a problem in lua?) and the other case lpeg just has to look up a key in a table(easier of course). – Uiy Mar 21 '12 at 12:09
• I guess the biggest problem to variables is they maybe local and hence go out of scope which would lose the grammar. Maybe that is why lpeg.V only works in tables in the first place? Anyways, tables are the way to do it as thy are just as easy if not easier and allow for the recursion(I just didn't know that at the time I wrote the question ;)) – Uiy Mar 21 '12 at 12:33