Repository navigation
Expand file tree
/
Copy pathJSGFGrammar.py
More file actions
171 lines (133 loc) · 4.59 KB
/
Copy pathJSGFGrammar.py
File metadata and controls
171 lines (133 loc) · 4.59 KB
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
# -*- coding: utf-8 -*-
# @copyright: MIT License
# Copyright (c) 2018 syntactic (Pastèque Ho)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# @summary: This file lays out the class structure for a JSGF Grammar
# @since: 2014/06/02
"""
This file lays out the class structure for a JSGF Grammar.
.. module:: JSGFGrammar
.. moduleauthor:: Pastèque Ho <timothyakho@gmail.com>
"""
class JSGFExpression():
pass
class Disjunction(JSGFExpression):
"""
Disjunction class stores disjuncts in a list
"""
def __init__(self, disjuncts):
self.disjuncts = disjuncts
def __str__(self):
return '( ' + ' | '.join(map(str,self.disjuncts)) + ' )'
def __repr__(self):
return str(self)
def __getitem__(self):
return self.disjuncts
class Optional(JSGFExpression):
"""
Optional class stores either a JSGFExpression, list, or string
as its optional element
"""
def __init__(self, option):
self.option = option
def __str__(self):
return ('[ ' + str(self.option) + ' ]')
def __repr__(self):
return str(self)
def __getitem__(self):
return self.option
class NonTerminal(JSGFExpression):
"""
NonTerminal class simply stores the label of the nonterminal
"""
def __init__(self, ntName):
self.name = ntName
def __str__(self):
return self.name
def __repr__(self):
return str(self)
def __getitem__(self):
return self.name
class Rule():
"""
Rule class, represents a JSGF rule, with a nonterminal name representing the
left hand side, and a list of possible expansions representing the right
hand side.
"""
def __init__(self):
"""
constructor with no args
"""
self.lhs = ''
self.rhs = []
def __init__(self, lhs):
"""
constructor with nonterminal name
"""
pass
def __init__(self, lhs, rhs):
"""
constructor with full rule definition
"""
self.lhs = lhs
self.rhs = rhs
def __str__(self):
return (str(self.lhs) + ' -> ' + str(self.rhs))
def __repr__(self):
return str(self)
class Grammar():
"""
Grammar class which contains a list for public rules and a list
for all rules.
"""
def __init__(self):
self.rules = []
self.publicRules = []
def addRule(self, rule):
"""
adds a rule to the list of rules
"""
self.rules.append(rule)
def addPublicRule(self, rule):
"""
adds a rule to the list of public rules
"""
self.publicRules.append(rule)
def getRHS(self, nt):
"""
returns rule definition
:param nt: Non-Terminal (variable) whose definition to get
"""
for rule in self.rules:
#print 'checking', nt.name, 'and', rule.lhs, type(nt.name), rule.lhs.name
if rule.lhs.name == nt.name:
return rule.rhs
raise ValueError("Rule not defined for " + str(nt))
def __getitem__(self, nt):
#rhsides = [] to do for multiple rhsides
for rule in self.rules:
if rule.lhs == nt:
return rule.rhs
else:
raise ValueError('Rule not defined for ' + str(nt))
def __str__(self):
return 'All Rules:' + str(self.rules) + '\n' + 'Public Rules:' + str(self.publicRules)
if __name__ == "__main__":
jgDisj = Disjunction(['hello', 'world'])
jgOpt = Optional(jgDisj)
jgRule = Rule("<greeting>", jgOpt)
print(jgRule)