-
Notifications
You must be signed in to change notification settings - Fork 4
/
generator.py
54 lines (39 loc) · 1.59 KB
/
generator.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
from parsing import *
import samples
def get_grammar():
return samples.get_sample_1()
def describe_grammar(gr):
return '\n'.join([
'Indexed grammar rules (%d in total):' % len(gr.productions),
str(gr) + '\n',
'Grammar non-terminals (%d in total):' % len(gr.nonterms),
'\n'.join('\t' + str(s) for s in gr.nonterms) + '\n',
'Grammar terminals (%d in total):' % len(gr.terminals),
'\n'.join('\t' + str(s) for s in gr.terminals)
])
def describe_parsing_table(table):
conflict_status = table.get_conflict_status()
def conflict_status_str(state_id):
has_sr_conflict = (conflict_status[state_id] == lalr_one.STATUS_SR_CONFLICT)
status_str = ('shift-reduce' if has_sr_conflict else 'reduce-reduce')
return 'State %d has a %s conflict' % (state_id, status_str)
return ''.join([
'PARSING TABLE SUMMARY\n',
'Is the given grammar LALR(1)? %s\n' % ('Yes' if table.is_lalr_one() else 'No'),
''.join(conflict_status_str(sid) + '\n' for sid in range(table.n_states)
if conflict_status[sid] != lalr_one.STATUS_OK) + '\n',
table.stringify()
])
def main():
print('Working on it...')
gr = get_grammar()
table = lalr_one.ParsingTable(gr)
print("I'm done.")
output_filename = 'parsing-table'
with open(output_filename + '.txt', 'w') as textfile:
textfile.write(describe_grammar(gr))
textfile.write('\n\n')
textfile.write(describe_parsing_table(table))
table.save_to_csv(output_filename + '.csv')
if __name__ == "__main__":
main()