-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEBNF programining language .py
More file actions
56 lines (42 loc) · 1.7 KB
/
Copy pathEBNF programining language .py
File metadata and controls
56 lines (42 loc) · 1.7 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
import re
def parse_array_declaration(desc):
# Define the regular expressions for parsing
array_definition = re.compile(r'(?P<array_name>[A-Z][A-Z0-9]*)\s*:\s*array\s*\[\s*(?P<dimensions>.+?)\s*\]\s*of\s*(?P<type>integer|char|real|bool)\s*;')
bounds = re.compile(r'(?P<lower_bound>\d+)\s*\.\.\s*(?P<upper_bound>\d+)')
# Mach the array declaration pattern
match = array_definition.match(desc)
if not match:
return None
array_name = match.group('array_name')
type_name = match.group('type')
dimensions_str = match.group('dimensions')
# Parse dimensions
dimensions = []
for dimension_str in re.split(r'\s*,\s*', dimensions_str):
bounds_match = bounds.match(dimension_str)
if not bounds_match:
return None
lower_bound = bounds_match.group('lower_bound')
upper_bound = bounds_match.group('upper_bound')
dimensions.append([lower_bound, upper_bound])
# Determine if the array is one-dimensional or multidimensional
dimenCount = len(dimensions)
array_name = "One-dimensional array" if dimenCount == 1 else "Multidimensional array"
# Compile-time descriptor fields
descriptor = [
array_name ,
type_name ,
"Integer", # Index type field
str(dimenCount),
dimensions,
"1000" # Address field
]
return descriptor
# Get array declaration input from the user
array_declaration = input("Enter the array declaration: ")
# Parse and print the compile-time descriptor
descriptor = parse_array_declaration(array_declaration)
if descriptor:
print(descriptor)
else:
print("Invalid array declaration.")