1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef top_down_tree_matching_automata_generator_h
26 #define top_down_tree_matching_automata_generator_h
28 #include <AD/automata/acgen.h> // Aho-Corasick string matching generator
29 #include <AD/automata/treegram.h> // Tree grammars
31 //////////////////////////////////////////////////////////////////////////////
33 // The top-down tree matching algorithm\cite{tree-matching, AC}
34 // reduces the problem of tree matching to the problem of string matching
35 // a set of path strings.
37 //////////////////////////////////////////////////////////////////////////////
39 class TopDownGen
: public ACGen
{
41 TopDownGen(const TopDownGen
&); // no copy constructor
42 void operator = (const TopDownGen
&); // no assignment
46 ///////////////////////////////////////////////////////////////////////////
48 ///////////////////////////////////////////////////////////////////////////
50 typedef Super::Symbol Symbol
;
51 typedef Super::Rule Rule
;
52 typedef Super::Offset Offset
;
53 typedef Super::State State
;
54 typedef TreeGrammar::TreeProduction TreeProduction
;
57 ///////////////////////////////////////////////////////////////////////////
58 // The current tree grammar
59 ///////////////////////////////////////////////////////////////////////////
60 const TreeGrammar
* G
;
62 ///////////////////////////////////////////////////////////////////////////
63 // Private method to add a path string to the trie
64 ///////////////////////////////////////////////////////////////////////////
65 void add_path(int, const TreeTerm
, int, Symbol
[]);
69 ///////////////////////////////////////////////////////////////////////////
70 // Constructors and destructor
71 ///////////////////////////////////////////////////////////////////////////
73 TopDownGen(const TreeGrammar
&);
76 ///////////////////////////////////////////////////////////////////////////
78 ///////////////////////////////////////////////////////////////////////////
79 inline int size() const { return Super::size(); }
80 inline State
start_state() const { return 0; }
82 ///////////////////////////////////////////////////////////////////////////
83 // Compilation and code emission
84 ///////////////////////////////////////////////////////////////////////////
85 void compile (const TreeGrammar
&);
86 virtual std::ostream
& gen_code (std::ostream
&, const char name
[]) const;
87 virtual std::ostream
& print_report (std::ostream
&) const;
89 ///////////////////////////////////////////////////////////////////////////
90 // Advance one state within the tree matching automaton
91 ///////////////////////////////////////////////////////////////////////////
92 inline State
go (register State s
, register int branch
, register Symbol c
) const
93 { return Super::go( Super::go(s
,branch
), c
); }