lua-users home
lua-l archive

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


You need to use a grammar:

----
local lpeg = require "listlpeg"

local p = lpeg.P{
  "patt",
  _one_ = lpeg.C(lpeg.L(lpeg.L"one")),
  patt = (lpeg.V"one" + lpeg.L(lpeg.V"patt") + lpeg.P(1))^0
}

print(p:match{ "something", { "something", { "one" } } }[1]) -- one
----

The "listre" syntax is easier on the eyes:

----
local re = require "listre"
local p2 = re.compile[[
  patt <- (<<one>> / { <patt> } / .)*
  one <- { "one" }
]]
----

Fetch and compile listlpeg again to run this example, the verifier had a bug where it would detect a spurious left recursion in patt. test/list.lua has more interesting search examples.

--
Fabio Mascarenhas


On Sat, Mar 27, 2010 at 5:05 PM, Wesley Smith <wesley.hoke@gmail.com> wrote:
I'm testing out some ideas using lpeg-list and was wondering how one
could express a recursive search through nested tables.  Using lpeg to
search through a string is pretty straightforward using

(patt + P(1))^0

This works equally well for lists, but doesn't work at all for nested
lists.  For example, I was trying to see how to generalize this
pattern:

local patt = L"one" * L(L"one" * L(L"one"))

It matches this:

local code = {"one", {"one", {"one"}, }, }



Now let's say I have:

local code = {"something", {"something", {"one"}, }, }

How can I express a recursive search through nested tables to find the
list containing "one"?  My first attempt was:

(patt + L( (patt + P(1))^1 ) + P(1))^0

This only goes one level deep and I need it to exhaustively recurse.
Any ideas on how to do this?

thanks,
wes