-
Notifications
You must be signed in to change notification settings - Fork 25
/
termgen.awk
executable file
·76 lines (61 loc) · 1.08 KB
/
termgen.awk
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
#!/opt/local/bin/gawk -f
# Generate a set of random terms
function get_arity(limit, res)
{
res = 0;
while((rand() < limit) && (res < 3))
{
limit = limit/2;
res++;
}
return res;
}
function get_depth_limit_arity(limit, depth)
{
if(rand() < 2/(depth+1))
{
return get_arity(limit);
}
return 0;
}
function randint(n)
{
return int(n * rand());
}
function get_symbol(arity)
{
return "f" arity randint(4-arity);
}
function gen_term(limit, depth, pred, arity, res, i)
{
arity = get_depth_limit_arity(limit, depth);
res = get_symbol(arity);
if(arity!=0)
{
pred = "(";
for(i=0; i<arity; i++)
{
# print depth ":" i "-" res;
res = res pred gen_term(limit, depth+1);
pred = ",";
}
res = res ")";
}
return res;
}
BEGIN{
if(!ARGV[2])
{
print "Usage: termgen.awk <number> <limit>" > "/dev/stderr";
exit 1;
}
limit = ARGV[2];
number = ARGV[1];
ARGV[1] = "";
ARGV[2] = "";
srand();
for(i=0; i<number; i++)
{
print gen_term(limit,0);
}
}