[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lpeg.Cg, lpeg.Cb, and how to visualize what they do
- From: Sean Conner <sean@...>
- Date: Wed, 16 Aug 2023 16:56:56 -0400
It was thus said that the Great Eduardo Ochs once stated:
> On Wed, 16 Aug 2023 at 12:20, Sean Conner <sean@conman.org> wrote:
> > (...)
>
> Hi Sean,
>
> > I don't see these as different, due to the fact that
> >
> > (a * b) * c = a * (b * c)
>
> try this:
>
> foo = function (str) return setmetatable({str}, foo_mt) end
> foo_mt = {
> __tostring = function (a) return a[1] end,
> __mul = function (a,b) return foo(format("(%s%s)", a[1], b[1])) end,
> }
> = (foo"a" * foo"b") * foo"c" --> "((ab)c)"
> = foo"a" * (foo"b" * foo"c") --> "(a(bc))"
>
> The associativity of the multiplication in Lua is not a "fact".
Couter example:
local lpeg = require "lpeg"
local function HEREt(tag)
return lpeg.Cmt(lpeg.P"",function(subject,pos)
io.stderr:write(string.format("%s: %s\n",tag,subject:sub(pos,pos+40)))
return pos
end)
end
local a = HEREt"a" * lpeg.C"a"
local b = HEREt"b" * lpeg.C"b"
local c = HEREt"c" * lpeg.C"c"
local p1 = (a * b) * c
local p2 = a * (b * c)
print("p1",p1:match"abc")
print("p2",p2:match"abc")
The two expressions parse the same way, with the same results (and yes,
you can remove the HEREt() calls to avoid Cmt() and still get the same
result).
> > These are expressions ... what's a "subpattern"? Again,
> >
> > (a * b) * c = a * (b * c)
>
> Is that a honest question?
Yes, it was. Thank you for the explanation.
-spc
- References:
- lpeg.Cg, lpeg.Cb, and how to visualize what they do, Eduardo Ochs
- Re: lpeg.Cg, lpeg.Cb, and how to visualize what they do, Roberto Ierusalimschy
- Re: lpeg.Cg, lpeg.Cb, and how to visualize what they do, Eduardo Ochs
- Re: lpeg.Cg, lpeg.Cb, and how to visualize what they do, Sean Conner
- Re: lpeg.Cg, lpeg.Cb, and how to visualize what they do, Eduardo Ochs