2

I'm trying to convert a string like 'a?(b?c:d):e' to another string 'ifthenelse(a,ifthenelse(b,c,d),e)' using the lpeg lua parser. I'm slowly learning how to use lpeg but still I can't find a suitable solution to do this with captures. Any ideas?

Here is what I did so far.

local lpeg = require("lpeg")

local S, P, R = lpeg.S, lpeg.P, lpeg.R
local C, Cc, Ct = lpeg.C, lpeg.Cc, lpeg.Ct
local Cf, Cg, Cs = lpeg.Cf, lpeg.Cg, lpeg.Cs
local V = lpeg.V

local thenop = P("?")
local elseop = P(":")
local openpar = P("(")
local closepar = P(")")
local digit = R("09")
local letter = R("az") + R("AZ")

local parser = 
   P({
    "F",
    F = V("E") * (thenop * V("E") * elseop * V("E"))^0,
    E = (letter + digit)^1 + (openpar * V("F") * closepar)
 }) -- * -1 -- Is it needed?
print(lpeg.match(parser,"a?(b?c:d):e"))
print(lpeg.match(parser,"a"))
1
  • At first I didn't like your abbreviations (S, P, R, ..., V), then I realized it actually made the grammar rules easier to read!
    – Stomp
    Dec 6, 2012 at 13:24

2 Answers 2

2
+50

I changed a bit the grammar, but still the same I think:

S = E "?" E ":" E | E
E = "(" S ")" | T
T = (L | D) +
L = [a-z] | [A-Z]
D = [0-9]

The grammar without captures:

local grammar = lpeg.P{
  "S";
  S = (lpeg.V"E" * lpeg.P"?" * lpeg.V"E" * lpeg.P":" * lpeg.V"E") + lpeg.V"E",
  E = (lpeg.P"(" * lpeg.V"S" * lpeg.P")") + lpeg.V"T",
  T = (lpeg.V"L" + lpeg.V"D")^1,
  L = lpeg.R("az") + lpeg.R("AZ"),
  D = lpeg.R("09")
}

The grammar with captures:

local grammar2 = lpeg.P{
  "S";
  S = (lpeg.Cs(lpeg.V"E") / "ifthenelse(%1") * (lpeg.P"?" / ",") * lpeg.V"E" * (lpeg.P":" / ",") * (lpeg.Cs(lpeg.V"E") / "%1)") + lpeg.V"E",
  E = (lpeg.P"(" / "") * lpeg.V"S" * (lpeg.P")" / "") + lpeg.V"T",
  T = (lpeg.V"L" + lpeg.V"D")^1,
  L = lpeg.R("az") + lpeg.R("AZ"),
  D = lpeg.R("09")
}

Captures replace:

  • "(" and ")" with empty string (lpeg.P"(" / "" and lpeg.P")" / ""))
  • First variable match with "ifthenelse(variable" (lpeg.Cs(lpeg.V"E") / "ifthenelse(%1"))
  • "?" and ":" with "," (lpeg.P"?" / "," and lpeg.P":" / ",")
  • Last variable match with "variable)" (lpeg.Cs(lpeg.V"E") / "%1)")

Some random tests (output in comments):

print( lpeg.match( lpeg.Cs(grammar2), "a") )
-- a
print( lpeg.match( lpeg.Cs(grammar2), "a?b:c") )
-- ifthenelse(a,b,c)
print( lpeg.match( lpeg.Cs(grammar2), "a?(i?j:k):c") )
-- ifthenelse(a,ifthenelse(i,j,k),c)
print( lpeg.match( lpeg.Cs(grammar2), "(a?(i?j:(x?y:z)):b)?c:(u?v:w)") )
-- ifthenelse(ifthenelse(a,ifthenelse(i,j,ifthenelse(x,y,z)),b),c,ifthenelse(u,v,w))

I hope you can continue from here.

1
  • While I use in my real life example the solution provided by William, I accept your answer because 1) it works 2) you detailed your way of thinking. Thanks again.
    – cjorssen
    Feb 21, 2012 at 20:44
2

Here is another solution to the problem given by William Ahern on the lua-list.

local lpeg = require("lpeg")

lpeg.locale(lpeg)

local function tr(a, b, c)
   if not b then
       return a
   else
       return string.format("ifthenelse(%s,%s,%s)", a, b, c)
   end
end

local var = lpeg.C(lpeg.alpha * (lpeg.alnum^0))

local E, G = lpeg.V"E", lpeg.V"G"

local grammar = lpeg.P{ "E",
   E = ((var + G) * (lpeg.P"?" * E * lpeg.P":" * E)^-1) / tr,
   G = lpeg.P"(" * E * lpeg.P")",
}

print(lpeg.match(grammar, "a?(b?c:d):e"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.