annotate python/yacc.py @ 318:e84047f29c78

Add burg and yacc initial attempts
author Windel Bouwman
date Tue, 31 Dec 2013 12:38:15 +0100
parents
children 8d07a4254f04
rev   line source
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
2
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
3 """ Parser generator utility """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
4
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
5 import argparse
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
6 import re
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
7 import sys
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
8 import datetime
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
9 from pyyacc import Grammar, print_grammar
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
10
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
11
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
12 class XaccLexer:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
13 def __init__(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
14 pass
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
15
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
16 def feed(self, txt):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
17 # Create a regular expression for the lexing part:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
18 tok_spec = [
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
19 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
20 ('STRING', r"'[^']*'"),
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
21 ('BRACEDCODE', r"\{[^\}]*\}"),
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
22 ('OTHER', r'[:;\|]'),
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
23 ('SKIP', r'[ ]')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
24 ]
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
25 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
26 gettok = re.compile(tok_re).match
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
27
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
28 lines = txt.split('\n')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
29
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
30 def tokenize_line(line):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
31 """ Generator that splits up a line into tokens """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
32 mo = gettok(line)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
33 pos = 0
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
34 while mo:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
35 typ = mo.lastgroup
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
36 val = mo.group(typ)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
37 if typ == 'ID':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
38 yield (typ, val)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
39 elif typ == 'STRING':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
40 typ = 'ID'
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
41 yield (typ, val[1:-1])
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
42 elif typ == 'OTHER':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
43 typ = val
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
44 yield (typ, val)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
45 elif typ == 'BRACEDCODE':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
46 yield (typ, val)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
47 elif typ == 'SKIP':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
48 pass
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
49 else:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
50 raise NotImplementedError(str(typ))
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
51 pos = mo.end()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
52 mo = gettok(line, pos)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
53 if len(line) != pos:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
54 raise ParseError('Lex fault at {}'.format(line))
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
55
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
56 def tokenize():
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
57 section = 0
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
58 for line in lines:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
59 line = line.strip()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
60 if not line:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
61 continue # Skip empty lines
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
62 if line == '%%':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
63 section += 1
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
64 yield('%%', '%%')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
65 continue
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
66 if section == 0:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
67 if line.startswith('%tokens'):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
68 yield('%tokens', '%tokens')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
69 yield from tokenize_line(line[7:])
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
70 else:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
71 yield ('HEADER', line)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
72 elif section == 1:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
73 yield from tokenize_line(line)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
74 yield ('eof', 'eof')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
75 self.tokens = tokenize()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
76 self.token = self.tokens.__next__()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
77
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
78 def next_token(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
79 t = self.token
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
80 if t[0] != 'eof':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
81 self.token = self.tokens.__next__()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
82 #print(t)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
83 return t
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
84
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
85
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
86 class ParseError(Exception):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
87 pass
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
88
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
89
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
90 class XaccParser:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
91 """ Implements a recursive descent parser to parse grammar rules.
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
92 We could have made an generated parser, but that would yield a chicken
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
93 egg issue.
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
94 """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
95 def __init__(self, lexer):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
96 self.lexer = lexer
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
97
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
98 @property
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
99 def Peak(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
100 """ Sneak peak to the next token in line """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
101 return self.lexer.token[0]
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
102
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
103 def next_token(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
104 """ Take the next token """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
105 return self.lexer.next_token()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
106
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
107 def consume(self, typ):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
108 """ Eat next token of type typ or raise an exception """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
109 if self.Peak == typ:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
110 return self.next_token()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
111 else:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
112 raise ParseError('Expected {}, but got {}'.format(typ, self.Peak))
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
113
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
114 def has_consumed(self, typ):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
115 """ Consume typ if possible and return true if so """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
116 if self.Peak == typ:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
117 self.consume(typ)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
118 return True
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
119 return False
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
120
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
121 def parse_grammar(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
122 """ Entry parse function into recursive descent parser """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
123 # parse header
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
124 headers = []
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
125 terminals = []
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
126 while self.Peak in ['HEADER', '%tokens']:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
127 if self.Peak == '%tokens':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
128 self.consume('%tokens')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
129 while self.Peak == 'ID':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
130 terminals.append(self.consume('ID')[1])
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
131 else:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
132 headers.append(self.consume('HEADER')[1])
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
133 self.consume('%%')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
134 self.grammar = Grammar(terminals)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
135 while self.Peak != 'eof':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
136 self.parse_rule()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
137 return self.grammar
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
138
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
139 def parse_symbol(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
140 return self.consume('ID')[1]
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
141
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
142 def parse_rhs(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
143 symbols = []
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
144 while self.Peak not in [';', 'BRACEDCODE', '|']:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
145 symbols.append(self.parse_symbol())
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
146 if self.Peak == 'BRACEDCODE':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
147 action = self.consume('BRACEDCODE')[1]
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
148 action = action[1:-1].strip()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
149 else:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
150 action = None
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
151 return symbols, action
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
152
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
153 def parse_rule(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
154 p = self.parse_symbol()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
155 self.consume(':')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
156 symbols, action = self.parse_rhs()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
157 self.grammar.add_production(p, symbols, action)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
158 while self.has_consumed('|'):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
159 symbols, action = self.parse_rhs()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
160 self.grammar.add_production(p, symbols, action)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
161 self.consume(';')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
162
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
163
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
164 class XaccGenerator:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
165 """ Generator that writes generated parser to file """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
166 def __init__(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
167 pass
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
168
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
169 def generate(self, grammar, output_file):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
170 print_grammar(grammar)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
171 self.grammar = grammar
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
172 self.action_table, self.goto_table = grammar.doGenerate()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
173 self.generate_python_script(output_file)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
174
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
175 def generate_python_script(self, f):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
176 """ Generate python script with the parser table """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
177 print('#!/usr/bin/python', file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
178 stamp = datetime.datetime.now().ctime()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
179 print('""" Automatically generated by xacc on {} """'.format(stamp), file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
180 print('from pyyacc import LRParser, Reduce, Shift, Accept, Production, Grammar', file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
181 print('from ppci import Token', file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
182 print(file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
183 print('class Parser(LRParser):', file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
184 print(' def __init__(self):', file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
185 # Generate rules:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
186 print(' self.start_symbol = "{}"'.format(self.grammar.start_symbol), file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
187 print(' self.grammar = Grammar({})'.format(self.grammar.terminals), file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
188 for rule_number, rule in enumerate(self.grammar.productions):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
189 rule.f_name = 'action_{}_{}'.format(rule.name, rule_number)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
190 print(' self.grammar.add_production("{}", {}, self.{})'.format(rule.name, rule.symbols, rule.f_name), file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
191 # Fill action table:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
192 print(' self.action_table = {}', file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
193 for state in self.action_table:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
194 action = self.action_table[state]
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
195 print(' self.action_table[{}] = {}'.format(state, action), file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
196 print('', file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
197
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
198 # Fill goto table:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
199 print(' self.goto_table = {}', file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
200 for gt in self.goto_table:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
201 to = self.goto_table[gt]
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
202 print(' self.goto_table[{}] = {}'.format(gt, to), file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
203 print('', file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
204
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
205 # Generate a function for each action:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
206 for rule in self.grammar.productions:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
207 M = len(rule.symbols)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
208 args = ', '.join('arg{}'.format(n + 1) for n in range(M))
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
209 print(' def {}(self, {}):'.format(rule.f_name, args), file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
210 if rule.f == None:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
211 semantics = 'pass'
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
212 else:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
213 semantics = str(rule.f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
214 if semantics.strip() == '':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
215 semantics = 'pass'
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
216 for n in range(M):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
217 semantics = semantics.replace('${}'.format(n + 1), 'arg{}'.format(n + 1))
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
218 print(' {}'.format(semantics), file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
219 print('', file=f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
220
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
221
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
222 def main():
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
223 # Parse arguments:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
224 parser = argparse.ArgumentParser(description='xacc compiler compiler')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
225 parser.add_argument('source', type=argparse.FileType('r'), \
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
226 help='the parser specification')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
227 parser.add_argument('-o', '--output', type=argparse.FileType('w'), \
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
228 default=sys.stdout)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
229 args = parser.parse_args()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
230 src = args.source.read()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
231 args.source.close()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
232
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
233 # Construction of generator parts:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
234 lexer = XaccLexer()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
235 parser = XaccParser(lexer)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
236 generator = XaccGenerator()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
237
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
238 # Sequence source through the generator parts:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
239 lexer.feed(src)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
240 grammar = parser.parse_grammar()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
241 generator.generate(grammar, args.output)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
242 args.output.close()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
243
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
244
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
245 if __name__ == '__main__':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
246 main()