1 # Copyright (C) 2005-2008, The Perl Foundation.
5 TGE - A tree grammar engine.
9 # define a grammar leaf.tg
10 transform min (Leaf) :language('PIR') {
11 $P1 = getattribute node, "value"
18 load_bytecode 'TGE.pir'
20 # Compile a grammar from the source grammar file
22 compiler = new 'TGE::Compiler'
25 grammar = compiler.'compile'(source)
28 # ... the tree to be manipulated
30 # Apply the grammar to the tree
32 TGI = grammar.apply(tree)
34 # Return the result of a particular rule on a particular tree
36 result = TGI.get('min')
42 TGE is a tool for transforming trees. Think of it as a good
43 old-fashioned substitution, but instead of taking and returning strings,
44 it takes and returns trees.
46 TGE is most heavily used in the compiler tools suite, where it
47 transforms the trees output by PGE into abstract syntax trees.
49 TGE has both a procedural interface and a syntax interface. The syntax
50 interface is easiest for humans to use when constructing grammars by
51 hand. The procedural interface is preferable for computer generated
54 This is the syntax for tree grammar rules:
56 transform name (pattern) {
60 The I<name> is the name of the transform rule. The I<pattern> is the type of
61 node this particular transform applies to. You can have multiple transforms
62 with the same name, as long as they match different patterns. The I<action> is
63 a block of code that executes the transform. You can specify what language the
64 action code is written in with the C<:language> modifier. At the moment, the
65 only valid language is PIR. Within the block, two parameters are supplied for
66 you: C<node> is the current node considered, and C<tree> is the top-level node
69 The C<:applyto> modifier says which node the transform applies to.
71 transform name (pattern) :applyto('childname') {
75 By default the transform applies to the current node (generally synthesized
76 attributes), but you can specify the name of a child node if the transform
77 applies to a child of the current node (generally inherited attributes).
84 # make sure we execute this sub only once
85 $P0 = get_global '$!tge_loaded'
86 unless null $P0 goto end
89 set_global '$!tge_loaded', $P0
92 load_bytecode 'PGE.pbc'
93 load_bytecode 'PGE/Util.pbc'
94 load_bytecode 'compilers/tge/TGE/Rule.pbc'
95 load_bytecode 'compilers/tge/TGE/Tree.pbc'
96 load_bytecode 'compilers/tge/TGE/Parser.pbc'
97 load_bytecode 'compilers/tge/TGE/Grammar.pbc'
98 load_bytecode 'compilers/tge/TGE/Compiler.pbc'
100 # import <die> and <line_number> rules from PGE::Util
101 $P0 = get_class ['TGE::Parser']
102 $P1 = get_hll_global ['PGE::Util'], 'die'
103 $P0.'add_method'('die', $P1)
104 $P1 = get_hll_global ['PGE::Util'], 'line_number'
105 $P0.'add_method'('line_number', $P1)
115 Allison Randal <allison@perl.org>
123 # vim: expandtab shiftwidth=4 ft=pir: