-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths.py
79 lines (70 loc) · 1.96 KB
/
s.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
# Hard-coded FIRST sets
first_sets = {
'E': {'(', 'i', 'n'},
'E\'': {'+', 'e'},
'T': {'(', 'i', 'n'},
'T\'': {'*', 'e'},
'F': {'(', 'i', 'n'},
}
# Hard-coded FOLLOW sets
follow_sets = {
'E': {')', '$'},
'E\'': {')', '$'},
'T': {'+', ')', '$'},
'T\'': {'+', ')', '$'},
'F': {'+', ')', '$', '*'}
}
# Hard-coded LL(1) Parsing Table
parsing_table = {
'E': {
'(': ['T', 'E\''],
'i': ['T', 'E\''],
'n': ['T', 'E\''],
},
'E\'': {
'+': ['+', 'T', 'E\''],
')': ['e'],
'$': ['e'],
},
'T': {
'(': ['F', 'T\''],
'i': ['F', 'T\''],
'n': ['F', 'T\''],
},
'T\'': {
'*': ['*', 'F', 'T\''],
'+': ['e'],
')': ['e'],
'$': ['e'],
},
'F': {
'(': ['(', 'E', ')'],
'i': ['i'],
'n': ['n'],
}
}
# Function to display the tables in a 2D structure
def display_tables_2d():
# Display FIRST sets
print("FIRST Sets (2D Structure):")
first_table = [["Non-terminal", "FIRST"]]
for non_terminal, first_set in first_sets.items():
first_table.append([non_terminal, ', '.join(first_set)])
for row in first_table:
print(f"{row[0]:<10} | {row[1]}")
print("\nFOLLOW Sets (2D Structure):")
follow_table = [["Non-terminal", "FOLLOW"]]
for non_terminal, follow_set in follow_sets.items():
follow_table.append([non_terminal, ', '.join(follow_set)])
for row in follow_table:
print(f"{row[0]:<10} | {row[1]}")
print("\nParsing Table (2D Structure):")
parsing_table_rows = [["Non-terminal", "Terminal", "Production"]]
for lhs, productions in parsing_table.items():
for terminal, production in productions.items():
parsing_table_rows.append([lhs, terminal, ' '.join(production)])
for row in parsing_table_rows:
print(f"{row[0]:<10} | {row[1]:<10} | {row[2]}")
# Example usage
if __name__ == "__main__":
display_tables_2d()