Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://anggtwu.net/LUA/ParseTree1.lua.html -- http://anggtwu.net/LUA/ParseTree1.lua -- (find-angg "LUA/ParseTree1.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- A "parse tree" is something like this: -- (c2m231introp 5 "gramatica-fig") -- (c2m231introa "gramatica-fig") -- -- (defun pt () (interactive) (find-angg "LUA/ParseTree1.lua")) -- (defun a6 () (interactive) (find-angg "LUA/Ast6.lua")) -- (defun tt () (interactive) (find-angg "LUA/TikzTrees1.lua")) -- (find-angg "LUA/ELpeg1.lua" "Gram") -- -- «.b-and-e» (to "b-and-e") -- «.subj» (to "subj") -- «.cot» (to "cot") -- «.NTify» (to "NTify") -- «.grammar» (to "grammar") -- «.totex» (to "totex") --require "TikzTrees1" -- (find-angg "LUA/TikzTrees1.lua") --require "Capts1" -- (find-angg "LUA/Capts1.lua") require "ELpeg1" -- (find-angg "LUA/ELpeg1.lua") require "Co1" -- (find-angg "LUA/Co1.lua") -- «b-and-e» (to ".b-and-e") -- (find-angg "LUA/ELpeg1.lua" "Gram" "mt_VAST =") Gram.__index.mt_VAST = function (gr) return { __newindex = function (v,name,pat) pat = Cp():Cg"b" * pat * Cp():Cg"e" gr:set(name, Cast(name,pat)) end, } end -- «subj» (to ".subj") -- (find-angg "LUA/ELpeg1.lua" "Gram" "cm0 =") Gram.__index.cm0 = function (gr, top, newsubj, pos) subj = newsubj if type(pos) == "string" then pos = subj:match(pos) end return gr:compile(top):match(subj, pos) end -- «cot» (to ".cot") -- (find-angg "LUA/Capts1.lua" "totex") co0 = Co.new(" %_{}", "\\^") cot = co0:translator() -- «NTify» (to ".NTify") isterminal = function (o) return otype(o) == "AST" and #o == 1 and type(o[1]) == "string" end NTify = function (o) if isterminal(o) then local tag = AST {[0]="<>", o[0]} local text = AST {[0]="co", o[1]} local und = AST {[0]="und", text, tag} return und else local tag = AST {[0]="<>", o[0]} local b,e = o.b,o.e local subs = AST {[0]="."} local pos = o.b local mkco = function (pos1,pos2) return AST {[0]="co", subj:sub(pos1,pos2-1)} end local addco = function (pos1,pos2) table.insert(subs, mkco(pos1,pos2)) end if #o == 0 then error("#o == 0 not implemented") else for i=1,#o do local thisb = o[i].b if pos < thisb then addco(pos,thisb) end table.insert(subs, NTify(o[i])) pos = o[i].e end if pos < e then addco(pos,e) end end local und = AST {[0]="und", subs, tag} return und end end -- «grammar» (to ".grammar") gr,V,VA,VE,PE = Gram.new() _ = S(" ")^0 VA.num = (R"09"^1):C() VA.op = (S"+-"):C() VA.sum = V.num *(_*V.op*_*V.num)^0 VA.Stmt = V.Id *_*C("=")*_* V.Expr *_*";" + "{" *_* V.StmtList *_*"}" + C("if") *_* "(" *_* V.Expr *_* ")" *_* V.Stmt VA.StmtList = V.Stmt * (_*V.Stmt)^0 V.Expr0 = V.Id + V.Num VA.Expr = V.Expr0*(_*V.Optr*_*V.Expr0)^0 VA.Id = C"x" + C"y" VA.Num = C(R"09") VA.Optr = C(S">+") -- «totex» (to ".totex") usepackages = [=[ % \usepackage{tikz} % \usetikzlibrary{shapes.geometric} ]=] middletexbody = Dang.from [=[ \scalebox{<<scale>>}{% <<texbody>> } ]=] defs = [=[ % (find-LATEX "2023-2-C2-intro.tex" "gramatica-fig") \def\ColorRed#1{{\color{red}{#1}}} \def\NT#1{\langle\textsf{#1}\rangle} \def\T#1{\ColorRed{\tt#1}} \def\T#1{\mathstrut \ColorRed{\tt#1}} \def\und#1#2{\underbrace{#1}_{\textstyle#2}} ]=] fmts = VTable {} fmts["co"] = "\\T{<cot(o[1])>}" fmts["und"] = "\\und{<1>}{<2>}" fmts["."] = "<mapconcat(totex, o)>" fmts["<>"] = "\\NT{<o[1]>}" --[[ ** (find-angg "LUA/Show2.lua" "texbody") ** (find-code-show2 "~/LATEX/Show2.tex") * (code-show2 "~/LATEX/Show2.tex") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "ParseTree1.lua" o = gr:cm0("Stmt", "if (x>9) { x=0; y=y+1;}") o = gr:cm0("num", "42+3 + 200") o = gr:cm0("sum", "42+3 + 200") o = gr:cm0("sum", "42 + 3") = o = o.b = o.e = otype(o) = isterminal(o) = isterminal(o[1]) = NTify(o[1]) = NTify(o) = NTify(o):show {em=1} = totex(NTify(o)) = Show.log = Show.bigstr * (etv) 2gT6 --]] -- Local Variables: -- coding: utf-8-unix -- End: