-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsers_ex.py
146 lines (120 loc) · 4.09 KB
/
parsers_ex.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
#!/usr/bin/env python3
"""Code for testing."""
# %cd ..
from tools.tools import tcs.tools
# from grammar.regular.regular_grammar import RG
from grammar.cf.cf_grammar import CFG
# from grammar.cs.cs_grammar import CSG
# from grammar.general.general_grammar import GG
from parse.cyk.cyk_parser import CYK_parser
from parse.rd.recursive_descent import RD_parser
from regexpr.reg_expression import RegEx
# Definizione di grammatica context free in CNF che genera le espressioni
# regolari sull'alfabeto {a,b,c}
er_terminals = {'(', ')', '+', '*', '.'}
string_alphabet = {'a', 'b', 'c'}
regex = CFG(
terminals=er_terminals.union(string_alphabet),
non_terminals={"E", 'A', 'R', 'S', 'T', 'U', 'V', 'W', 'Y', 'Z', 'X'},
axiom="E",
productions={
'A': {('a'), ('b'), ('c'), ('A', 'R'), ('Y', 'X'), ('Z', 'X', 'E')},
'R': {('*')},
'S': {('(')},
'T': {(')')},
'U': {('+')},
'V': {('.')},
'W': {('S', 'A')},
'Y': {('W', 'U')},
'X': {('A', 'T')},
'Z': {('W', 'V')},
"E": {('A',), ()}
}
)
print(regex)
print(regex.syntax_tree)
# Definizione di un parser CYK sulla grammatica precedente
cyk = CYK_parser(regex)
simple_prods = {
'A': {'b', 'a', 'c', 'AR', 'YX', 'ZX'},
'R': {'*'},
'S': {'('},
'T': {')'},
'U': {'+'},
'V': {'.'},
'W': {'SA'},
'Y': {'WU'},
'X': {'AT'},
'Z': {'WV'},
}
er_terminals = {'(', ')', '+', '*', '.'}
string_alphabet = {'a', 'b', 'c'}
regex_simple = CFG(
terminals=er_terminals.union(string_alphabet),
non_terminals={'A', 'R', 'S', 'T', 'U', 'V', 'W', 'Y', 'Z', 'X'},
axiom='A',
productions=Tools.simple_productions(simple_prods)
)
Tools.simple_productions(simple_prods)
Tools.simple_sequence('aXbZ')
er_terminals = {'(', ')', '+', '*', '.'}
string_alphabet = {'a', 'b', 'c'}
regex = CFG(
terminals=er_terminals.union(string_alphabet),
non_terminals={'A', 'R', 'S', 'T', 'U', 'V', 'W', 'Y', 'Z', 'X'},
axiom='A',
productions={
'A': {('a'), ('b'), ('c'), ('A', 'R'), ('Y', 'X'), ('Z', 'X')},
'R': {('*')},
'S': {('(')},
'T': {(')')},
'U': {('+')},
'V': {('.')},
'W': {('S', 'A')},
'Y': {('W', 'U')},
'X': {('A', 'T')},
'Z': {('W', 'V')},
}
)
re = RegEx(alphabet={'a', 'b'},
expression=('(', '(', 'a', '.', 'b', ')', '*', ')'))
print(re)
re.syntax_tree
print(re.syntax_tree)
print(re.random_string())
re1 = RegEx(alphabet={'a', 'b'},
expression=(Tools.simple_sequence('(a+b*)*.(a.b)')))
re2 = RegEx(alphabet={'a', 'b'},
expression=(Tools.simple_sequence('(a+b*)*+(a.b)')))
re3 = RegEx(alphabet={'a', 'b'}, expression=(Tools.simple_sequence('(a.b)')))
print(re2.random_string())
terminals = {'(', ')', '+', '*', '.', 'a', 'b', 'c'}
regex1 = CFG(
terminals=terminals,
non_terminals={'A'},
axiom='A',
productions={
('A',): {('a',), ('b',), ('c',), ('(', 'A', '+', 'A', ')'),
('(', 'A', '+', 'A', ')', '*'),
('(', 'A', '.', 'A', ')'),
('(', 'A', '.', 'A', ')', '*'), ('a', '*'), ('b', '*'),
('c', '*')}
}
)
alphabet = {'io', 'tu', 'lui'}
regex2 = CFG(
terminals=alphabet.union({'(', ')', '+', '*', '.'}),
non_terminals={'S', 'A', 'B'},
axiom='S',
productions={
'S': {'A', ''},
'A': alphabet.union({('(', 'A', ')'),
('(', 'A', '+', 'A', ')'),
('(', 'A', '.', 'A', ')'),
('B', '*')}),
'B': alphabet.union({('(', 'A', '.', 'A', ')'),
('(', 'A', '+', 'A', ')'),
('(', 'A', ')')}),
}
)
rd = RD_parser(regex1)