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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
package Tree;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(input stmts stmt automakerule makerule conditional ifblock
optionalelse optionalrhs optionalcomments lhs rhs commentlist primaries
optionlist traverse printgraph);
# Grammar Rule : (1) input => stmts
# Create a node having child as stmts.
sub input($)
{
my ( $val ) = @_;
my %node = (name => input, childs => [ $val ]);
return \%node;
}
# Grammar Rule : (1) stmts=> stmt '\n'
# Creates a node having a child as stmt
# (2) stmts=> stmts stmt '\n'
# Creates a node having a child as stmt. Insert the created node into
# the childs array of the stmts(First Argument).
sub stmts($$;$)
{
my ( $val1, $val2, $val3) = @_;
if($val3 == undef)
{
my %node=(name=>stmts,childs=>[$val1]);
return \%node;
}
else
{
push @{$val1->{childs}},$val2;
return $val1;
}
}
# Grammar Rule : (1) stmt => automakerule
# (2) stmt => makerule
# (3) stmt => commentlist
# (4) stmt => conditional
# Create a node with corresponding child node.
sub stmt($)
{
my ( $val1) = @_;
my %node = ( name => stmt , childs => [ $val1 ]);
return \%node;
}
# Grammar Rule : (1) automakerule => lhs '=' optionalrhs optionalcomments
# Create a node for automake rule.
sub automakerule($$$$)
{
my ( $val1, $val2, $val3, $val4 ) = @_;
my %node = (name => automakerule, childs => [ $val1,$val3 ]);
push @{ $node{ childs }}, $val4 if $val4;
return \%node;
}
# Grammar Rule : (1) makerule => value ':' rhs
# Create a node for make rule.
sub makerule($$$)
{
my ( $val1, $val2, $val3 ) = @_;
my %node = (name => makerule, childs => [ $val1,$val3 ]);
return \%node;
}
# Grammar Rule : (1) optionalrhs =>
# Create an empty node.
# (2) optionalrhs => rhs
# Create a node with rhs as child.
sub optionalrhs(;$)
{
my ( $val ) = @_;
my %node = ( name => optionalrhs );
if( $val == undef )
{
$node{ empty } = 1;
}
else
{
$node{ childs } = [ $val ];
}
return \%node;
}
# Grammar Rule : (1) optionalcomments =>
# Create an empty node.
# (2) optionalcomments => commentlist
# Create a node with commentlist as child.
sub optionalcomments(;$)
{
my ( $val ) = @_;
my %node = ( name => optionalcomments );
if( $val == undef )
{
$node{ empty } = 1;
}
else
{
$node{ childs } = [ $val ];
}
return \%node;
}
# Grammar Rule : (1) conditional => ifblock optionalelse endif
# Create a node for conditional statement.
sub conditional($$$)
{
my ( $val1, $val2, $val3 ) = @_;
my %node = ( name => conditional, childs => [ $val1, $val2]);
return \%node;
}
# Grammar Rule : (1) ifblock => if value newline automakerule newline
# Create a node for if block.
sub ifblock($$$$$)
{
my ( $val1, $val2, $val3, $val4, $val5) = @_;
my %node = ( name => ifblock, condition => $val2 -> [1], childs => [$val4]);
return \%node;
}
# Grammar Rule : (1) optionalelse =>
# Create an empty node.
# (2) optionalelse => else newline automakerule newline
# Create a node with child as automakerule.
sub optionalelse(;$$$$)
{
my ( $val1, $val2, $val3, $val4 ) = @_;
my %node = ( name => optionalelse );
if( $val1 == undef )
{
$node{ empty } = 1;
}
else
{
$node{ childs } = [ $val3 ];
}
return \%node;
}
# Grammar Rule : (1) lhs => optionlist primaries
# Create a node for left hand side of variable defination consisting of
# option list and primary.
# (2) lhs => value
# Create a node for left hand side of variable defination having a simple
# variable defination.
sub lhs($;$)
{
my ( $val1, $val2 ) = @_;
my %node = ( name => lhs);
if( $val2 == undef )
{
$node{ value } = $val1 -> [1];
}
else
{
$node{ childs } = [ $val1, $val2 ];
}
return \%node;
}
# Grammar Rule : (1) rhs => rhsval
# Creates a node having rhsval as its value.
# (2) rhs => rhs rhsval
# Inserts rhsval into the array pointed by value key in rhs.
sub rhs($;$)
{
my ( $val1, $val2 ) = @_;
if($val2 == undef)
{
my %node = ( name => rhs, value => [$val1 -> [1]]);
return \%node;
}
else
{
push @{ $val1 -> { value }}, $val2 -> [1];
return $val1;
}
}
# Grammar Rule : (1) commentlist => comment
# Creates a node having comment as its value.
# (2) commentlist => commentlist comment
# Inserts comment into the array pointed by value key in commentlist.
sub commentlist($;$)
{
my ( $val1, $val2 ) = @_;
if( $val2 == undef )
{
my %node = ( name => commentlist, value => [ $val1 -> [1]]);
return \%node;
}
else
{
push @{ $val1 -> {value}} , $val2 -> [1];
return $val1;
}
}
# Grammar Rule : (1) primaries : PROGRAMS
# (2) primaries : LIBRARIES
# (3) primaries : LTLIBRARIES
# (4) primaries : LISP
# (5) primaries : PYTHON
# (6) primaries : JAVA
# (7) primaries : SCRIPTS
# (8) primaries : DATA
# (9) primaries : HEADERS
# (10) primaries : MASN
# (11) primaries : TEXINFOS
# (12) primaries : value
# Creates a node corresponding to the given primary.
sub primaries($)
{
my ( $val ) = @_;
my %node = ( name => primaries );
if( $val -> [0] eq 'value')
{
$node{value}= $val -> [1];
}
else
{
$node{value}= $val;
}
return \%node;
}
# Grammar Rule : (1) optionlist : value '_'
# Create a node having data value in val key.
# (2) optionlist : optionlist value '_'
# Add the data value to val key in the node pointed by optionlist(First Argument).
sub optionlist($$;$)
{
my ( $val1, $val2, $val3 ) = @_;
if($val3 == undef)
{
my %node = (name => optionlist, val => [$val1 -> [1]]);
return \%node;
}
else
{
push @{$val1 -> {val}},$val2 -> [1];
return $val1;
}
}
# printgraph(Hash)
# prints the AST to Standard Output by traversing the tree starting at node pointed by hash.
sub printgraph($)
{
print "graph graphname {\n";
my ( $ref ) = @_;
print "0 [label=\"Root\"];";
traverse( $ref, 0);
print "}\n";
}
#Stores the next id to be alloted to new node.
my $id=0;
# traverse(Hash, Parent Id)
# Traverses the tree recursively. Prints the information about the current node to Standard Output. Call all its child with Parent Id equal to current Node Id.
sub traverse($$)
{
my ( $ref,$parent ) = @_;
my %node = %$ref;
return if $node{empty};
$id++;
my $curr_id = $id;
print "$parent--$id;\n";
my $label = "";
@keys = sort grep {!/^childs/} keys %node;
foreach $key ( @keys )
{
$label .= $key."=>";
if(ref( $node{ $key }) eq 'ARRAY')
{
$label .= join(" ",@{$node{$key}})."\n";
}
else
{
$label .= $node{$key}." ";
}
}
print "$curr_id [label=\"$label\"];";
if( $node{childs} )
{
my $val1 = $node{childs};
foreach $child (@$val1)
{
traverse($child,$curr_id);
}
}
}
1;
|