Fabrique

A build language for complex systems

 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
yaccparse.py
1 #!/usr/bin/env python
2 
3 import argparse
4 import collections
5 import re
6 
7 
8 args = argparse.ArgumentParser()
9 args.add_argument('input')
10 
11 args = args.parse_args()
12 
13 
14 separators_reached = 0
15 
16 symbol = re.compile('[_A-Za-z]+:')
17 first_production = re.compile('([^|][^{]*)({.*)?')
18 
19 
20 symbols = []
21 current_symbol = None
22 productions = []
23 in_code = False
24 
25 for line in open(args.input):
26  if line.strip() == '%%':
27  separators_reached += 1
28  continue
29 
30  if separators_reached != 1:
31  continue
32 
33  line = line.strip()
34 
35  if symbol.match(line):
36  if current_symbol:
37  symbols.append((current_symbol, productions))
38  productions = []
39 
40  current_symbol = line[:-1]
41  continue
42 
43  if in_code:
44  in_code = '}' not in line
45  continue
46 
47  match = first_production.match(line)
48  if match:
49  (variables, code) = match.groups()
50 
51  variables = [ v for v in variables.split() if v != ';' ]
52  if len(variables) > 0:
53  assert len(productions) == 0
54  productions = [ variables ]
55 
56  print('%s -> %s' % (current_symbol, productions))
57 
58  if code and '}' not in code:
59  in_code = True
60 
61  continue
62 
63 
64  print(line)
65 
66 print(symbols)