1page_util_norm_peg(n) Parser generator tools page_util_norm_peg(n)
2
3
4
5______________________________________________________________________________
6
8 page_util_norm_peg - page AST normalization, PEG
9
11 package require page::util::norm_peg ?0.1?
12
13 package require snit
14
15 ::page::util::norm::peg tree
16
17_________________________________________________________________
18
20 This package provides a single utility command which takes an AST for a
21 parsing expression grammar and normalizes it in various ways. The
22 result is called a Normalized PE Grammar Tree.
23
24 Note that this package can only be used from within a plugin managed by
25 the package page::pluginmgr.
26
28 ::page::util::norm::peg tree
29 This command assumes the tree object contains for a parsing
30 expression grammar. It normalizes this tree in place. The
31 result is called a Normalized PE Grammar Tree.
32
33 The following operations are performd
34
35 [1] The data for all terminals is stored in their grand‐
36 parental nodes. The terminal nodes and their parents are
37 removed. Type information is dropped.
38
39 [2] All nodes which have exactly one child are irrelevant and
40 are removed, with the exception of the root node. The
41 immediate child of the root is irrelevant as well, and
42 removed as well.
43
44 [3] The name of the grammar is moved from the tree node it is
45 stored in to an attribute of the root node, and the tree
46 node removed.
47
48 The node keeping the start expression separate is removed
49 as irrelevant and the root node of the start expression
50 tagged with a marker attribute, and its handle saved in
51 an attribute of the root node for quick access.
52
53 [4] Nonterminal hint information is moved from nodes into
54 attributes, and the now irrelevant nodes are deleted.
55
56 Note: This transformation is dependent on the removal of
57 all nodes with exactly one child, as it removes the all
58 'Attribute' nodes already. Otherwise this transformation
59 would have to put the information into the grandparental
60 node.
61
62 The default mode given to the nonterminals is value.
63
64 Like with the global metadata definition specific infor‐
65 mation is moved out out of nodes into attributes, the now
66 irrelevant nodes are deleted, and the root nodes of all
67 definitions are tagged with marker attributes. This pro‐
68 vides us with a mapping from nonterminal names to their
69 defining nodes as well, which is saved in an attribute of
70 the root node for quick reference.
71
72 At last the range in the input covered by a definition is
73 computed. The left extent comes from the terminal for the
74 nonterminal symbol it defines. The right extent comes
75 from the rightmost child under the definition. While this
76 not an expression tree yet the location data is sound
77 already.
78
79 [5] The remaining nodes under all definitions are transformed
80 into proper expression trees. First character ranges,
81 followed by unary operations, characters, and nontermi‐
82 nals. At last the tree is flattened by the removal of
83 superfluous inner nodes.
84
85 The order matters, to shed as much nodes as possible
86 early, and to avoid unnecessary work later.
87
89 This document, will undoubtedly contain bugs and other problems.
90 Please report such in the category page of the Tcllib SF Trackers
91 [http://sourceforge.net/tracker/?group_id=12883]. Please also report
92 any ideas for enhancements you may have.
93
95 PEG, graph walking, normalization, page, parser generator, text pro‐
96 cessing, tree walking
97
99 Copyright (c) 2007 Andreas Kupries <andreas_kupries@users.sourceforge.net>
100
101
102
103
104page 1.0 page_util_norm_peg(n)