|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://angg.twu.net/LUA/binops-re.lua
-- http://angg.twu.net/LUA/binops-re.lua.html
-- (find-angg "LUA/binops-re.lua")
-- 2014apr28
--
-- (find-lua51manualw3m "")
-- (find-books "__comp/__comp.el" "ierusalimschy")
-- (find-pil2page 8 "Contents")
-- (find-pil2text 8 "Contents")
-- (find-angg "LUA/precedenceparser.lua")
binops = {
["*"] = {size=20, leftsize=21, rightsize=19, fmt="%s*%s"},
["+"] = {size=30, leftsize=31, rightsize=29, fmt="%s+%s"},
["^"] = {size=10, leftsize=11, rightsize= 9, fmt="%s^%s"},
}
ExprNum = Class {
type = "ExprNum",
__tostring = function (o) return o:ptos() end,
__index = {
ptos = function (o) return o[1] end,
pntos = function (o) return o[1] end,
rpntos = function (o) return o[1] end,
},
}
ExprBin = Class {
type = "ExprBin",
__tostring = function (o) return o:ptos() end,
__index = {
binops = binops,
ptos = function (o, size)
local op = o.binops[o.op]
local leftstr = o[1]:ptos(op.leftsize)
local rightstr = o[2]:ptos(op.rightsize)
local str = string.format(op.fmt, leftstr, rightstr)
if size and op.size > size then str = "("..str..")" end
return str
end,
pntos = function (o)
return o.op.." "..o[1]:pntos().." "..o[2]:pntos()
end,
rpntos = function (o)
return o[1]:rpntos().." "..o[2]:rpntos().." "..o.op
end,
},
-- class methods:
fromLeft = function (list) -- "2-3-4-5" is "((2-3)-4)-5"
local a = list[1]
for i=3,#list,2 do
local op,b = list[i-1], list[i]
a = ExprBin {op=op, a, b}
end
return a
end,
fromRight = function (list) -- "2^3^4^5" is "2^(3^(4^5))"
local b = list[#list]
for i=#list-2,1,-2 do
local a,op = list[i], list[i+1]
b = ExprBin {op=op, a, b}
end
return b
end,
}
N = function (n) return ExprNum {n} end
B = function (op, a, b) return ExprBin {op=op, a, b} end
-- e2345 = B("*", B("+", N(2), N(3)), B("^", N(4), N(5)))
-- print(e2345)
-- print(e2345:pntos())
-- print(e2345:rpntos())
-- (find-es "lua5" "lpeg-re")
-- file:///home/edrx/usrc/lpeg-0.12/re.html
userocks()
require "re"
pat = re.compile([[
E30 <- {| E20 ( _ { [-+] } _ E20 )* |} -> fromLeft
E20 <- {| E10 ( _ { [*/] } _ E10 )* |} -> fromLeft
E10 <- {| E0 ( _ { '^' } _ E0 )* |} -> fromRight
E0 <- N / '(' _ E30 _ ')'
N <- { [0-9]+ } -> N
_ <- ' '*
]], {
N = function (s) return ExprNum {s} end,
fromLeft = ExprBin.fromLeft,
fromRight = ExprBin.fromRight,
})
print(pat:match "9+3*4^5")
print(pat:match "(9+3)*4^5")
print(pat:match "(9 +3 )* 4 ^5")
print(pat:match "9 +3* 4 ^5")
--[===[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "binops-re.lua"
paat = re.compile ([[
o <- {| word ( _ word )* |} -> concat
word <- [%S]+ -> "<%0>"
_ <- { ' '+ }
]], { concat = table.concat })
print(paat:match "abc de")
PP(paat:match "abc de fg ")
macros = {
-- (find-angg "LUA/lua50init.lua" "split")
oneof = function (str)
local quote = function (s) return "'"..s.."'" end
return mapconcat(quote, split(str), " / ")
end,
binprec = function (f, abops)
local a, b, ops = abops:match "%s*(%S+)%s+(%S+)%s+(.*)"
return string.format("%s <- {| %s ( _ { %s } _ %s )* |} -> %s",
a, b, macros.oneof(ops), b, f)
end,
fromLeft = function (abops) return macros.binprec("fromLeft", abops) end,
fromRight = function (abops) return macros.binprec("fromRight", abops) end,
}
LR_to_re = function (li)
local lr, newe, olde, op1, ops =
li:match "%s*([LR]):%s+(%S+)%s+(%S+)%s+(%S+)%s+(.*)"
local newops = "'"..op1.."' "..ops:gsub("(%S+)", "/ '%0'")
local s = string.format("%s <- {| %s ( _ { %s } _ %s )* |} -> %s",
newe, olde, newops, olde, ({L="fromLeft", R="fromRight"})[lr])
return s
end
= LR_to_re " L: e20 e10 + := / "
ee = {N"3", "+", N"4", "+", N"5", "+", N"6"}
-- eee = fromLeft(ee)
PP(ee)
-- PP(eee)
-- print(eee)
eee = fromRight(ee)
print(eee)
gabgrammar = [[
macro fromRight ( e e ^ )
macro fromLeft ( e e * / )
macro fromLeft ( e e + - )
macro fromLeft ( e e < <= = >= > != )
macro fromLeft ( e e & )
macro fromLeft ( e e or )
macro fromLeft ( e e -> )
macro nonAssoc ( e e <- )
macro fromRight ( e e := )
elist0 <- {| ( e ( _ ',' _ e )* )? |}
elist1 <- {| e ( _ ',' _ e )* |}
elist2 <- {| e ( _ ',' _ e )+ |}
parenish <- '(' _ e _ ')' /
'(' _ elist2 _ ')' -> tuple /
'{' _ elist0 _ '}' -> explicitSet /
'{' _ elist1 _ '|' _ elist1 _ '}' -> suchThatSet
faish <- macro oneof ( Fa Ex \ ) e '.' e
falta: function application
]]
--]===]
-- Local Variables:
-- coding: raw-text-unix
-- End: