1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
lex start
{
ignore /[\t\n ]+/
literal '^', '|', '-', ',', ':', '!', '?', '.'
literal '(', ')', '{', '}', '*', '&', '+'
literal '--', ':>', ':>>', '<:', '->', '**'
token word /[a-zA-Z_][a-zA-Z0-9_]*/
token uint /[0-9]+/
}
def start [expression]
def expression [term expression_op*]
def expression_op
['|' term]
| ['&' term]
| ['-' term]
| ['--' term]
def term [factor_rep term_rest]
# This list is done manually to get shortest match.
def term_rest
[]
| [term_op term_rest]
def term_op
[factor_rep]
| ['.' factor_rep]
| [':>' factor_rep]
| [':>>' factor_rep]
| ['<:' factor_rep]
def factor_rep
[factor_neg factor_rep_op*]
def factor_rep_op
['*']
| ['**']
| ['?']
| ['+']
| ['{' factor_rep_num '}']
| ['{' ',' factor_rep_num '}']
| ['{' factor_rep_num ',' '}']
| ['{' factor_rep_num ',' factor_rep_num '}']
def factor_rep_num [uint]
def factor_neg
['!' factor_neg]
| ['^' factor_neg]
| [factor]
def factor
[alphabet_num]
| [word]
| ['(' expression ')']
def alphabet_num
[uint]
parse SP: start[stdin]
S: start = SP.tree
#
# Top-Down, Left-Right
#
int do_topdown_leftright( T: ref any )
{
for C:any in child(T) {
yield C
do_topdown_leftright( C )
}
}
iter topdown_leftright( T: ref any )
{
do_topdown_leftright( T )
}
#
# Bottom-Up, Left-Right
#
int do_bottomup_leftright( T: ref any )
{
for C:any in child(T) {
do_bottomup_leftright( C )
yield C
}
}
iter bottomup_leftright( T: ref any )
{
do_bottomup_leftright( T )
}
#
# Top-Down, Right-Left
#
int do_topdown_rightleft( T: ref any )
{
for C:any in rev_child(T) {
yield C
do_topdown_rightleft( C )
}
}
iter topdown_rightleft( T: ref any )
{
do_topdown_rightleft( T )
}
#
# Bottom-Up, Right-Left
#
int do_bottomup_rightleft( T: ref any )
{
for C:any in rev_child(T) {
do_bottomup_rightleft( C )
yield C
}
}
iter bottomup_rightleft( T: ref any )
{
do_bottomup_rightleft( T )
}
#
# Testing
#
print( 'bottomup_leftright\n' )
for T1: any in bottomup_leftright( S )
{
print( ^T1 '\n' )
}
print( 'bottomup_rightleft\n' )
for T2: any in bottomup_rightleft( S )
{
print( ^T2 '\n' )
}
print( 'topdown_leftright\n' )
for T3: any in topdown_leftright( S )
{
print( ^T3 '\n' )
}
print( 'topdown_rightleft\n' )
for T4: any in topdown_rightleft( S )
{
print( ^T4 '\n' )
}
|