-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.py
78 lines (55 loc) · 1.48 KB
/
grammar.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
# coding: utf-8
"""
A Simple context-free grammar implementation.
"""
class Terminal:
def __init__(self, symbol):
self._symbol = symbol
@property
def symbol(self):
return self._symbol
@symbol.setter
def symbol(self, sym):
if sym.islower():
self._symbol = sym
else:
raise ValueError("Terminals are meant to be lower cased.")
def __unicode__(self):
return self._symbol
class Variable:
def __init__(self, symbol):
self._symbol = symbol
@property
def symbol(self):
return self._symbol
@symbol.setter
def symbol(self, sym):
if sym.isupper():
self._symbol = sym
else:
raise ValueError("Variables are meant to be upper cased.")
def __unicode__(self):
return self._symbol
class Rule:
def __init__(self, variable, chain):
self._variable = variable
self.chains = chain
@property
def variable(self):
return self._variable
@variable.setter
def variable(self, v):
if isinstance(v, Variable):
self._variable = v
else:
raise ValueError('It must be a variable.')
def leftmost_derivation(self):
pass
def rightmost_derivation(self):
pass
class Grammar:
def __init__(self, rules, variables, terminals, e):
self.rules = rules
self.variables = variables
self.terminals = terminals
self.e = e