【问题标题】:Transform (cond ? then : else) to ifthenelse(cond,then,else) with lpeg使用 lpeg 将 (cond ? then : else) 转换为 ifthenelse(cond,then,else)
【发布时间】:2012-03-08 23:37:21
【问题描述】:

我正在尝试使用lpeg lua 解析器将'a?(b?c:d):e' 之类的字符串转换为另一个字符串'ifthenelse(a,ifthenelse(b,c,d),e)'。我正在慢慢学习如何使用 lpeg,但我仍然找不到合适的解决方案来执行此操作。有什么想法吗?

这是我到目前为止所做的。

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"))

【问题讨论】:

  • 一开始我不喜欢你的缩写(S、P、R、...、V),后来我意识到它实际上使语法规则更容易阅读!

标签: lua lpeg


【解决方案1】:

我改变了一点语法,但我认为还是一样:

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

没有捕获的语法:

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")
}

带有捕获的语法:

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")
}

捕获替换:

  • "(" 和 ")" 带空字符串(lpeg.P"(" / ""lpeg.P")" / "")
  • 第一个变量匹配“ifthenelse(variable”(lpeg.Cs(lpeg.V"E") / "ifthenelse(%1"))
  • “?”和 ":" 和 ","(lpeg.P"?" / ","lpeg.P":" / ","
  • 与“变量”匹配的最后一个变量 (lpeg.Cs(lpeg.V"E") / "%1)")

一些随机测试(以 cmets 输出):

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))

希望你能从这里继续。

【讨论】:

  • 虽然我在现实生活示例中使用了威廉提供的解决方案,但我接受您的回答,因为 1)它有效 2)您详细说明了您的思维方式。再次感谢。
【解决方案2】:

这是William Ahern在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"))

【讨论】:

    猜你喜欢
    • 2019-10-22
    • 1970-01-01
    • 2019-04-15
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    相关资源
    最近更新 更多