Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://anggtwu.net/LUA/Ast1.lua.html
--   http://anggtwu.net/LUA/Ast1.lua
--          (find-angg "LUA/Ast1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun a  () (interactive) (find-angg "LUA/Ast1.lua"))
-- (defun a1 () (interactive) (find-angg "LUA/Ast1.lua"))
-- (defun a2 () (interactive) (find-angg "LUA/Ast2.lua"))
-- (defun c3 () (interactive) (find-angg "LUA/Caepro3.lua"))
-- (defun c4 () (interactive) (find-angg "LUA/Caepro4.lua"))
-- (defun g2 () (interactive) (find-angg "LUA/Gram2.lua"))
-- (defun g3 () (interactive) (find-angg "LUA/Gram3.lua"))

-- Machinery for converting asts to text.
-- (find-angg "LUA/Caepro3.lua" "Abbrevs")
-- (find-angg "LUA/Caepro3.lua" "Abbrevs" "fmts =")

-- expand "LATEX/<ma:yyyy_s()>-<ma:MM()>-tudo.pdf"
-- expand "LATEX/<m:yyyy_s(a)>-<m:MM(a)>-tudo.pdf"
-- expand "LATEX/<:yyyy_s>-<:MM>-tudo.pdf"
-- yyyy_s = function (ma, ...) return ma.m:yyyy_s(ma.a, ...) end

-- «.AST»		(to "AST")
-- «.AST-tests»		(to "AST-tests")
-- «.E»			(to "E")
-- «.E-tests»		(to "E-tests")
-- «.ToTeX»		(to "ToTeX")
-- «.ToTeX-tests»	(to "ToTeX-tests")


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

lpeg.ptmatch = function (pat, str) PP(pat:Ct():match(str)) end
lpeg.Copyto  = function (pat, tag) return pat:Cg(tag) * Cb(tag) end



--     _    ____ _____ 
--    / \  / ___|_   _|
--   / _ \ \___ \ | |  
--  / ___ \ ___) || |  
-- /_/   \_\____/ |_|  
--                     
-- «AST»  (to ".AST")
-- Based on: (find-angg "LUA/Gram2.lua" "AST")
--
alttags = {eq="=", plus="+", minus="-", Mul="*",
           div="/", Div="//", pow="^",
           paren="()", plic="'"}

AST = Class {
  type = "AST",
  ify  = function (o)
      if type(o) == "table" and o[0] and alttags[o[0]] then
        o[0] = alttags[o[0]]
      end
      return AST(o)
    end,
  __tostring = function (o) return tostring(SynTree.from(o)) end,
  __index = {
  },
}

-- «AST-tests»  (to ".AST-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Ast1.lua"
= AST.ify {[0]="Mul", 2, 3}

--]]



--  _____ 
-- | ____|
-- |  _|  
-- | |___ 
-- |_____|
--        
-- «E»  (to ".E")

entries = {}
E = setmetatable({}, {
    __call     = function (_,name) return entries[name] end,
    __index    = function (_,name) return entries[name] end,
    __newindex = function (_,name,o) entries[name] = o end,
  })

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

-- «E-tests»  (to ".E-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Ast1.lua"
L = Code.L

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

--]]



-- (find-angg "LUA/Gram3.lua" "expr")


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

foldplic = function (A)
    local o = A[1]
    for i=2,#A do o = plic(o) end
    return o
  end
V.exprplic  = Ct(V.exprbasic * (_*Cs"'")^0) / foldplic

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Ast1.lua"
-- alttags = {}
= 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"]

--]]



--  _____   _____   __  __
-- |_   _|_|_   _|__\ \/ /
--   | |/ _ \| |/ _ \\  / 
--   | | (_) | |  __//  \ 
--   |_|\___/|_|\___/_/\_\
--                        
-- «ToTeX»  (to ".ToTeX")

AST.__index.totex  = function (o) return totex(o) end
AST.__index.tt     = function (o) return ToTeX.new(o) end

totex = function (o)
    if type(o) == "string" then return o end
    if type(o) == "number" then return tostring(o) end
    return ToTeX.fromast(o)
  end

ToTeX = Class {
  type = "ToTeX",
  new  = function (o) return ToTeX {o=o} end,
  from = function (o)
      if type(o) == "string" then return o end
      if type(o) == "number" then return tostring(o) end
      return ToTeX.new(o):asttotex()
    end,
  fromast = function (o) return ToTeX.from(o) end,
  __index = {
    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 = "<tt.funs[o[1]] or o[1]>",
      },
    funs = {
        ln  = "\\ln ",
        sin = "\\sin ",
      },
    --
    eval = function (tt,fmt,a,b,c)
        return L("tt,o,a,b,c => "..fmt)(tt, tt.o, a, b, c)
      end,
    getn      = function (tt, n) return tt:eval("o[a+0]", n) end,
    totexn    = function (tt, n) return tt:eval("totex(o[a+0])", n) end,
    tostringn = function (tt, n) return tt:eval("tostring(o[a+0])", n) end,
    subst     = function (tt, fmt)
        local isnum  = function (s) return s:match"^[0-9]+$" end
        local totexn = function (n) return tt:totexn(n) end
        local ev = function (s) return tt:eval(s) end
        local f = function (s) return isnum(s) and totexn(s) or ev(s) end
	return (fmt:gsub("<(.-)>", f))
      end,
    --
    tag      = function (tt) return tt.o[0] end,
    fmt      = function (tt) return tt:tag() and tt.fmts[tt:tag()] end,
    asttotex = function (tt)
        if not tt:tag() then return "[notag]" end
        if not tt:fmt() then return "[no fmt: "..tt:tag().."]" end
	return tt:subst(tt:fmt())
      end,
  },
}

-- «ToTeX-tests»  (to ".ToTeX-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Ast1.lua"
L = Code.L
o = E["(ln x)^3 // 5 + sin x"]
o = E["(f(x)g(x))' = f'(x)g(x) + f(x)g'(x)"]
= o
= o:totex()

= E["(ln x)"]
= E["(ln x)"]:tt()
= E["(ln x)"]:tt():tostringn(1)
= E["(ln x)"]:tt():asttotex()
= totex(23)
= E["(ln x)"]:totex()
= E["x"]:totex()
= E["x"]:totex()
= E["x"]:totex0():fmt()
= E["x"]:totex0():format()
= E["x"]:totex0():eval("mytostring(o)")
= E["x"]:totex0():eval("o[0]")
= E["x"]:totex0():eval("o[1]")
= E["x"]:totex0():eval("o[a]",    1)
= E["x"]:totex0():eval("o[a]",   "1")
= E["x"]:totex0():eval("o[a+0]", "1")

= fun"x":totex0()

--]==]






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