Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://anggtwu.net/LUA/Ast4.lua.html
--   http://anggtwu.net/LUA/Ast4.lua
--          (find-angg "LUA/Ast4.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun a3 () (interactive) (find-angg "LUA/Ast4.lua"))
-- (defun a4 () (interactive) (find-angg "LUA/Ast4.lua"))
--        (code-etv "~/LATEX/")
--   (find-code-etv "~/LATEX/")

require "Ast3"   -- (find-angg "LUA/Ast3.lua")

AST.alttags = {
  eq="=", plus="+", minus="-", Mul="*",
  div="/", Div="//", pow="^",
  paren="()", plic="'"
}

fun   = function (name) return AST {[0]="fun", name} end
var   = function (name) return AST {[0]="var", name} end
num   = function (str)  return AST {[0]="num", str}  end
bin   = function (a, op, b) return AST {[0]=op, a, b} end
unary = function    (op, a) return AST {[0]=op, a}    end
paren = function (a)    return unary("()", a) end
plic  = function (a)    return unary("'", a) end
ap    = function (a, b) return bin(a, "ap", b) end

eq    = function (a, b) return bin(a, "=", b) end
plus  = function (a, b) return bin(a, "+", b) end
minus = function (a, b) return bin(a, "-", b) end
mul   = function (a, b) return bin(a, "mul", b) end
Mul   = function (a, b) return bin(a, "*",   b) end
div   = function (a, b) return bin(a, "/",   b) end
Div   = function (a, b) return bin(a, "//",  b) end
pow   = function (a, b) return bin(a, "^",   b) end

E[    "x"                ] = var"x"
E[ "ln x"                ] = ap(fun"ln", E"x")
E["(ln x)"               ] = paren(E"ln x")
E[       "3"             ] = num("3")
E["(ln x)^3"             ] = pow(E"(ln x)", E"3")
E["(ln x)^3"             ] = pow(E"(ln x)", E"3")
E[           "5"         ] = num("5")
E[                "sin x"] = ap(fun"sin", E"x")
E[            "5 + sin x"] = plus(E"5", E"sin x")
E["(ln x)^3 // 5 + sin x"] = Div(E"(ln x)^3", E"5 + sin x")

E[ "f"                                 ] = fun"f"
E[  "(x)"                              ] = paren(E"x")
E[ "f(x)"                              ] = ap(E"f", E"(x)")
E[     "g"                             ] = fun"g"
E[     "g(x)"                          ] = ap(E"g", E"(x)")
E[ "f(x)g(x)"                          ] = mul(E"f(x)", E"g(x)")
E["(f(x)g(x))"                         ] = paren(E"f(x)g(x)")
E["(f(x)g(x))'"                        ] = plic(E"(f(x)g(x))")
E[              "f'"                   ] = fun"f'"
E[              "f'(x)"                ] = ap(E"f'", E"(x)")
E[              "f'(x)g(x)"            ] = mul(E"f'(x)", E"g(x)")
E[                              "g'"   ] = fun"g'"
E[                              "g'(x)"] = ap(E"g'", E"(x)")
E[                          "f(x)g'(x)"] = mul(E"f(x)", E"g'(x)")
E[              "f'(x)g(x) + f(x)g'(x)"] = plus(E"f'(x)g(x)", E"f(x)g'(x)")
E["(f(x)g(x))' = f'(x)g(x) + f(x)g'(x)"] = eq(E"(f(x)g(x))'", E"f'(x)g(x) + f(x)g'(x)")

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Ast4.lua"
= E["(f(x)g(x))' = f'(x)g(x) + f(x)g'(x)"]
= E["(ln x)^3 // 5 + sin x"]

--]]

foldplic = function (A)
    local o = A[1]
    for i=2,#A do o = plic(o) end
    return o
  end

gr,V,VA,VE,PE = Gram.new()
grcm = function (...) print(gr:cm(...)) end
_ = S(" ")^0

VA.num   = Cs(R"09"^1)
VA.var   = anyof "x"
VA.fun   = anyof "f' f g' g ln sin"
VA.ap    = V.fun *_* V.exprbasic

VA.paren = "("*_* V.expr *_*")"
V.exprbasic = V.paren + V.ap + V.num + V.fun + V.var
V.exprplic  = assocpost(V.exprbasic, Cs"'"     )
V.exprpow   = assocr(V.exprplic,     Cs"^"     )
V.exprmul   = assocl(V.exprpow,      Cc"mul"   )
V.exprMul   = assocl(V.exprmul,      Cs"*"     )
V.exprplus  = assocl(V.exprMul,      Cs"+"     )
V.exprDiv   = assocl(V.exprplus,     Cs"//"    )
V.expreq    = assocl(V.exprDiv,      Cs"="     )
V.expr      = V.expreq

V.exprplic  = Ct(V.exprbasic * (_*Cs"'")^0) / foldplic

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Ast4.lua"

= grcm("num", "234")
= grcm("expr", "234")
= grcm("expr", "f'")
= grcm("expr", "f'(x)")
= grcm("expr", "f'(x)")
= grcm("expr", "f'(x) + 5")
= grcm("expr", "f'(x) + 5^6 // 20")
= grcm("expr", "(ln x)^3 // 5 + sin x")
= grcm("expr", "(f(x)g(x))' = f'(x)g(x) + f(x)g'(x)")

= E["(f(x)g(x))' = f'(x)g(x) + f(x)g'(x)"]
= E["(ln x)^3 // 5 + sin x"]

--]]


totex00 = ToText0 {
  fmts = {
    ["()"] = "(<1>)",
    ["//"] = "\\frac{<1>}{<2>}",
    ["^"]  = "{<1>}^{<2>}",
    ["+"]  = "<1> + <2>",
    ["="]  = "<1> = <2>",
    ap  = "<1> <2>",
    mul = "<1> <2>",
    Mul = "<1> \\cdot <2>",
    ["'"] = "<1>'",
    --
    num = "<1>",
    var = "<1>",
    fun = "<tt0.funs[o[1]] or o[1]>",
  },
  funs = {
    ln  = "\\ln ",
    sin = "\\sin ",
  },
}

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Ast4.lua"
=   gr:cm ("expr", "(ln x)^3 // 5 + sin x")
o = gr:cm0("expr", "(ln x)^3 // 5 + sin x")
= o
= totex(o)
= o:show("4 dd")
= Show.log
* (etv)

-- (find-angg "LUA/Show1.lua" "string.show-tests")

--]]







-- Local Variables:
-- coding:  utf-8-unix
-- End: