lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

> I am having some trouble trying to learn more about pattern matching and
> when to use it over regular expressions.
   PM and REs are different in application. Where REs apply to text /
tokenizing, PMs apply to data structures.
For example, a rule like this (broken up over several lines to
emphasize the structure):
{ { "Tree",
       left={ "Tree", left=V"LL", val=V"VL", right=V"LR" },
       right={ "Tree", left=V"RL", val=V"VR", right=V"RR" }
     rotated  -- pass captured vars to rotated(cs) and return
transformed version
could be used to test for a binary tree whose nodes are no longer in
the proper order. In one expression, it extracts the pieces of the
tree structure (LL = left's left, VR = value of right, etc.), passes a
table with all the captures to is_out_of_order, and if so, it returns
a rotated version of the tree. The code to do this sort of
transformation as a series of switch or if / else if / else statements
would be quite a bit longer, while this actually looks like a template
for the (hypothetical) tree structure. You could similarly work with
XML trees (as manifested by lxp.lom), parse trees / ASTs, etc.

(Reading about pattern matching in Erlang may be confusing because
Erlang also uses pattern-matching for some string operations.)

> I work with scientific instruments and right now I am trying to build a
> disassembler. I am assuming that all of the commands that an instrument will
> respond to are encoded in it's firmware. It's my goal to extract these, that
> way I(and others) could control instrumentation without access to it's
> expensive proprietary software.
> Does Tamale for disassembly sound plausible?
   Perhaps, but lexing/parsing tools may be more appropriate - tamale
is better suited to working with tables than raw byte streams. Either
way, though, you could use a collection of templates (a grammar or a
rule table) to match chunks of date and capture addresses, constants,