-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindromes.gap
114 lines (100 loc) · 2.38 KB
/
palindromes.gap
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
import "ext_palindrome.hh"
type Rope = extern
signature sig_palindrome(alphabet, answer) {
answer match(alphabet, answer, alphabet);
answer cadd(alphabet, answer);
answer end(void);
answer skipl(alphabet, answer);
answer skipr(answer, alphabet);
choice [answer] h([answer]);
}
algebra alg_enum auto enum;
algebra alg_count auto count;
algebra alg_tikz auto tikz;
algebra alg_pretty implements sig_palindrome(alphabet = char, answer = Rope) {
Rope match(alphabet a, Rope x, alphabet b) {
Rope res;
append(res, a);
append(res, x);
append(res, b);
return res;
}
Rope cadd(alphabet a, Rope l) {
Rope res;
append(res, '_');
append(res, l);
return res;
}
Rope end(void) {
Rope res;
return res;
}
Rope skipl(alphabet a, Rope x) {
Rope res;
append(res, '>');
append(res, x);
return res;
}
Rope skipr(Rope x, alphabet b) {
Rope res;
append(res, x);
append(res, '<');
return res;
}
choice [Rope] h([Rope] candList) {
return candList;
}
}
algebra alg_palScore implements sig_palindrome(alphabet = char, answer = int) {
int match(alphabet a, int x, alphabet b) {
return x + 1;
}
int cadd(alphabet a, int l) {
return l;
}
int end(void) {
return 0;
}
int skipl(alphabet a, int x) {
return x;
}
int skipr(int x, alphabet b) {
return x;
}
choice [int] h([int] candList) {
return list(maximum(candList));
}
}
grammar gra_palindrome uses sig_palindrome(axiom = palin) {
palin = match(CHAR, palin, CHAR) with samechar
| loop
# h;
loop = cadd(CHAR, loop)
| end(EMPTY)
# h;
}
grammar gra_palloc uses sig_palindrome(axiom = skip_l) {
skip_l = skipl(CHAR, skip_l)
| skip_r
# h;
skip_r = skipr(skip_r, CHAR)
| palin
# h;
palin = match(CHAR, palin, CHAR) with samechar
| loop
# h;
loop = cadd(CHAR, loop)
| end(EMPTY)
# h;
}
/*
example inputs: abbabaabba
*/
instance enum = gra_palindrome(alg_enum);
instance loc = gra_palloc(alg_palScore * alg_pretty);
instance palin_pp = gra_palindrome(alg_pretty);
instance palloc_pp = gra_palloc(alg_pretty);
instance palin_count = gra_palindrome(alg_count);
instance palloc_count = gra_palloc(alg_count);
instance palin_scorecount = gra_palindrome(alg_palScore * alg_count);
instance palloc_scorecount = gra_palloc(alg_palScore * alg_count);