-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsamples.py
189 lines (171 loc) · 5.1 KB
/
samples.py
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
from parsing.grammar import *
def get_sample_1():
# From http://web.cs.dal.ca/~sjackson/lalr1.html
return Grammar([
NonTerminal('N', [
"V '=' E", "E"
]),
NonTerminal('E', [
"V"
]),
NonTerminal('V', [
"'x'", "'*' E"
])
])
def get_sample_2():
# From Dragonbook, page 271, example 4.61
return Grammar([
NonTerminal('S', [
"L '=' R", "R"
]),
NonTerminal('L', [
"'*' R", "ID"
]),
NonTerminal('R', [
"L"
])
])
def get_sample_3():
# From Dragonbook, page 263, grammar 4.55 below example 4.54
return Grammar([
NonTerminal('S', [
"C C"
]),
NonTerminal('C', [
"'c' C", "'d'"
])
])
def get_sample_4():
# From Dragonbook, page 267, example 4.58
return Grammar([
NonTerminal('S', [
"'a' A 'd'", "'b' B 'd'", "'a' B 'e'", "'b' A 'e'"
]),
NonTerminal('A', [
"'c'"
]),
NonTerminal('B', [
"'c'"
])
])
def get_sample_5():
# Random grammar with a moderate amount of states
return Grammar([
NonTerminal('p', [
"tit ss"
]),
NonTerminal('tit', [
"TITLE TEXT '\\n'"
]),
NonTerminal('ss', [
"s ss", "s"
]),
NonTerminal('s', [
"NOTE LEFT OF TEXT ':' TEXT '\\n'",
"TEXT '->' TEXT ':' TEXT '\\n'",
"LOOP TEXT '\\n' ss END '\\n'",
"LOOP TEXT '\\n' END '\\n'",
"ALT TEXT '\\n' ss ELSE '\\n' ss END '\\n'",
"ALT TEXT '\\n' ss END '\\n'"
])
])
def get_sample_6():
# Sample ambiguous grammar for Alex Aiken's COOL programming language
return Grammar([
NonTerminal('program', [
"class_list"
]),
NonTerminal('class_list', [
"class", "class_list class"
]),
NonTerminal('class', [
"CLASS TYPEID '{' opt_feature_list '}' ';'",
"CLASS TYPEID INHERITS TYPEID '{' opt_feature_list '}' ';'"
]),
NonTerminal('feature', [
"OBJECTID '(' opt_formal_list ')' ':' TYPEID '{' expr '}' ';'",
"OBJECTID ':' TYPEID ASSIGN expr ';'",
"OBJECTID ':' TYPEID ';'"
]),
NonTerminal('feature_list', [
"feature", "feature_list feature"
]),
NonTerminal('opt_feature_list', [
"feature_list", ""
]),
NonTerminal('formal', [
"OBJECTID ':' TYPEID"
]),
NonTerminal('formal_list', [
"formal",
"formal_list ',' formal"
]),
NonTerminal('opt_formal_list', [
"formal_list", ""
]),
NonTerminal('expr', [
"BOOL_CONST", "STR_CONST", "INT_CONST", "OBJECTID", "'(' expr ')'",
"NOT expr", "expr '=' expr", "expr LE expr", "expr '<' expr", "'~' expr",
"expr '/' expr", "expr '*' expr", "expr '-' expr", "expr '+' expr", "ISVOID expr",
"NEW TYPEID", "CASE expr OF branch_list ESAC", "'{' block_expr_list '}'",
"WHILE expr LOOP expr POOL", "IF expr THEN expr ELSE expr FI",
"OBJECTID '(' opt_dispatch_expr_list ')'",
"expr '.' OBJECTID '(' opt_dispatch_expr_list ')'",
"expr '@' TYPEID '.' OBJECTID '(' opt_dispatch_expr_list ')'",
"OBJECTID ASSIGN expr", "LET let_expr_tail"
]),
NonTerminal('branch', [
"OBJECTID ':' TYPEID DARROW expr ';'"
]),
NonTerminal('branch_list', [
"branch", "branch_list branch"
]),
NonTerminal('block_expr_list', [
"expr ';'", "block_expr_list expr ';'"
]),
NonTerminal('dispatch_expr_list', [
"expr", "dispatch_expr_list ',' expr"
]),
NonTerminal('opt_dispatch_expr_list', [
"dispatch_expr_list", ""
]),
NonTerminal('let_expr_tail', [
"OBJECTID ':' TYPEID IN expr",
"OBJECTID ':' TYPEID ASSIGN expr IN expr",
"OBJECTID ':' TYPEID ',' let_expr_tail",
"OBJECTID ':' TYPEID ASSIGN expr ',' let_expr_tail"
])
])
def get_sample_7():
return Grammar([
NonTerminal('S', [
"'a' B S", "'a' 'a'", "'a'"
]),
NonTerminal('B', [
"'a'"
])
])
def get_sample_8():
return Grammar([
NonTerminal('S', [
"'b' A 'b'", "'b' B 'a'"
]),
NonTerminal('A', [
"'a' S", "C B"
]),
NonTerminal('B', [
"'b'", "B 'c'"
]),
NonTerminal('C', [
"'c'", "'c' C"
])
])
def get_sample_9():
return Grammar([
NonTerminal('S', [
"T 'a' T"
]),
NonTerminal('T', [
"", "'b' 'b' T"
])
])