annotate python/yacc.py @ 383:173e20a47fda

Added linker description loader
author Windel Bouwman
date Sun, 27 Apr 2014 17:40:39 +0200
parents d2ddfe134c48
children fb3c1f029b30
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
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
3 """
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 323
diff changeset
4 Parser generator utility. This script can generate a python script from a
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
5 grammar description.
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
6
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
7 Invoke the script on a grammar specification file:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
8
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
9 .. code::
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
10
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
11 $ ./yacc.py test.x -o test_parser.py
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
12
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
13 And use the generated parser by deriving a user class:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
14
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
15
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
16 .. code::
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
17
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
18 import test_parser
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
19 class MyParser(test_parser.Parser):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
20 pass
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
21 p = MyParser()
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
22 p.parse()
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
23
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
24
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
25 Alternatively you can load the parser on the fly:
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
26
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
27 .. code::
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
28
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
29 import yacc
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
30 parser_mod = yacc.load_as_module('mygrammar.x')
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
31 class MyParser(parser_mod.Parser):
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
32 pass
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
33 p = MyParser()
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
34 p.parse()
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
35
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
36 """
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
37
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
38 import argparse
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
39 import re
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
40 import sys
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
41 import datetime
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
42 import types
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
43 import io
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
44 import logging
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 323
diff changeset
45 from pyyacc import Grammar
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
46 from baselex import BaseLexer
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
47 from ppci import Token
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
48
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
49
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
50 class XaccLexer(BaseLexer):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
51 def __init__(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
52 tok_spec = [
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
53 ('ID', r'[A-Za-z][A-Za-z\d_]*', lambda typ, val: (typ, val)),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
54 ('STRING', r"'[^']*'", lambda typ, val: ('ID', val[1:-1])),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
55 ('BRACEDCODE', r"\{[^\}]*\}", lambda typ, val: (typ, val)),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
56 ('OTHER', r'[:;\|]', lambda typ, val: (val, val)),
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
57 ('SKIP', r'[ ]', None)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
58 ]
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
59 super().__init__(tok_spec)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
60
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
61 def tokenize(self, txt):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
62 lines = txt.split('\n')
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
63 section = 0
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
64 for line in lines:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
65 line = line.strip()
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
66 if not line:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
67 continue # Skip empty lines
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
68 if line == '%%':
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
69 section += 1
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
70 yield Token('%%', '%%')
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
71 continue
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
72 if section == 0:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
73 if line.startswith('%tokens'):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
74 yield Token('%tokens', '%tokens')
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
75 for tk in super().tokenize(line[7:]):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
76 yield tk
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
77 else:
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
78 yield Token('HEADER', line)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
79 elif section == 1:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
80 for tk in super().tokenize(line):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
81 yield tk
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
82
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
83
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
84 class ParseError(Exception):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
85 pass
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
86
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
87
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
88 class XaccParser:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
89 """ Implements a recursive descent parser to parse grammar rules.
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
90 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
91 egg issue.
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
92 """
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
93 def __init__(self):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
94 pass
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
95
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
96 def prepare_peak(self, lexer):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
97 self.lexer = lexer
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
98 self.look_ahead = self.lexer.next_token()
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
99
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
100 @property
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
101 def Peak(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
102 """ Sneak peak to the next token in line """
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
103 return self.look_ahead.typ
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
104
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
105 def next_token(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
106 """ Take the next token """
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
107 token = self.look_ahead
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
108 self.look_ahead = self.lexer.next_token()
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
109 return token
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
110
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
111 def consume(self, typ):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
112 """ Eat next token of type typ or raise an exception """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
113 if self.Peak == typ:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
114 return self.next_token()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
115 else:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
116 raise ParseError('Expected {}, but got {}'.format(typ, self.Peak))
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
117
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
118 def has_consumed(self, typ):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
119 """ Consume typ if possible and return true if so """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
120 if self.Peak == typ:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
121 self.consume(typ)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
122 return True
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
123 return False
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
124
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
125 def parse_grammar(self, lexer):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
126 """ Entry parse function into recursive descent parser """
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
127 self.prepare_peak(lexer)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
128 # parse header
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
129 self.headers = []
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
130 terminals = []
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
131 while self.Peak in ['HEADER', '%tokens']:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
132 if self.Peak == '%tokens':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
133 self.consume('%tokens')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
134 while self.Peak == 'ID':
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
135 terminals.append(self.consume('ID').val)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
136 else:
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
137 self.headers.append(self.consume('HEADER').val)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
138 self.consume('%%')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
139 self.grammar = Grammar(terminals)
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
140 while self.Peak != 'EOF':
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
141 self.parse_rule()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
142 return self.grammar
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
143
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
144 def parse_symbol(self):
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
145 return self.consume('ID').val
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
146
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
147 def parse_rhs(self):
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
148 """ Parse the right hand side of a rule definition """
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
149 symbols = []
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
150 while self.Peak not in [';', 'BRACEDCODE', '|']:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
151 symbols.append(self.parse_symbol())
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
152 if self.Peak == 'BRACEDCODE':
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
153 action = self.consume('BRACEDCODE').val
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
154 action = action[1:-1].strip()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
155 else:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
156 action = None
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
157 return symbols, action
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
158
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
159 def parse_rule(self):
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
160 """ Parse a rule definition """
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
161 p = self.parse_symbol()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
162 self.consume(':')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
163 symbols, action = self.parse_rhs()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
164 self.grammar.add_production(p, symbols, action)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
165 while self.has_consumed('|'):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
166 symbols, action = self.parse_rhs()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
167 self.grammar.add_production(p, symbols, action)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
168 self.consume(';')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
169
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
170
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
171 class XaccGenerator:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
172 """ Generator that writes generated parser to file """
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
173 def __init__(self):
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
174 self.logger = logging.getLogger('yacc')
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
175
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
176 def generate(self, grammar, headers, output_file):
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
177 self.output_file = output_file
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
178 self.grammar = grammar
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
179 self.headers = headers
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
180 self.logger.info('Generating parser for grammar {}'.format(grammar))
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 334
diff changeset
181 self.action_table, self.goto_table = grammar.generate_tables()
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
182 self.generate_python_script()
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
183
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
184 def print(self, *args):
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
185 """ Print helper function that prints to output file """
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
186 print(*args, file=self.output_file)
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
187
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
188 def generate_python_script(self):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
189 """ Generate python script with the parser table """
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
190 self.print('#!/usr/bin/python')
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
191 stamp = datetime.datetime.now().ctime()
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
192 self.print('""" Automatically generated by xacc on {} """'.format(stamp))
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
193 self.print('from pyyacc import LRParser, Reduce, Shift, Accept, Production, Grammar')
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
194 self.print('from ppci import Token')
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
195 self.print('')
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
196 for h in self.headers:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
197 print(h, file=output_file)
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
198 self.print('')
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
199 self.print('class Parser(LRParser):')
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
200 self.print(' def __init__(self):')
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
201 # Generate rules:
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
202 self.print(' self.start_symbol = "{}"'.format(self.grammar.start_symbol))
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
203 self.print(' self.grammar = Grammar({})'.format(self.grammar.terminals))
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
204 for rule_number, rule in enumerate(self.grammar.productions):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
205 rule.f_name = 'action_{}_{}'.format(rule.name, rule_number)
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
206 self.print(' self.grammar.add_production("{}", {}, self.{})'.format(rule.name, rule.symbols, rule.f_name))
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
207 # Fill action table:
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
208 self.print(' self.action_table = {}')
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
209 for state in self.action_table:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
210 action = self.action_table[state]
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
211 self.print(' self.action_table[{}] = {}'.format(state, action))
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
212 self.print('')
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
213
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
214 # Fill goto table:
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
215 self.print(' self.goto_table = {}')
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 323
diff changeset
216 for state_number in self.goto_table:
6f4753202b9a Added more recipes
Windel Bouwman
parents: 323
diff changeset
217 to = self.goto_table[state_number]
6f4753202b9a Added more recipes
Windel Bouwman
parents: 323
diff changeset
218 self.print(' self.goto_table[{}] = {}'.format(state_number, to))
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
219 self.print('')
318
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 # Generate a function for each action:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
222 for rule in self.grammar.productions:
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 323
diff changeset
223 num_symbols = len(rule.symbols)
6f4753202b9a Added more recipes
Windel Bouwman
parents: 323
diff changeset
224 args = ', '.join('arg{}'.format(n + 1) for n in range(num_symbols))
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
225 self.print(' def {}(self, {}):'.format(rule.f_name, args))
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
226 if rule.f == None:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
227 semantics = 'pass'
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
228 else:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
229 semantics = str(rule.f)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
230 if semantics.strip() == '':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
231 semantics = 'pass'
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 323
diff changeset
232 for n in range(num_symbols):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
233 semantics = semantics.replace('${}'.format(n + 1), 'arg{}'.format(n + 1))
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
234 self.print(' {}'.format(semantics))
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
235 self.print('')
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
236
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
237
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
238 def make_argument_parser():
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
239 # Parse arguments:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
240 parser = argparse.ArgumentParser(description='xacc compiler compiler')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
241 parser.add_argument('source', type=argparse.FileType('r'), \
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
242 help='the parser specification')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
243 parser.add_argument('-o', '--output', type=argparse.FileType('w'), \
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
244 default=sys.stdout)
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
245 return parser
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
246
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
247
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
248 def load_as_module(filename):
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
249 """ Load a parser spec file, generate LR tables and create module """
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
250 ob = io.StringIO()
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
251 args = argparse.Namespace(source=open(filename), output=ob)
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
252 main(args)
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
253
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
254 parser_mod = types.ModuleType('generated_parser')
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
255 exec(ob.getvalue(), parser_mod.__dict__)
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
256 return parser_mod
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
257
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 340
diff changeset
258
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
259 def main(args):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
260 src = args.source.read()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
261 args.source.close()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
262
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
263 # Construction of generator parts:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
264 lexer = XaccLexer()
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
265 parser = XaccParser()
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
266 generator = XaccGenerator()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
267
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
268 # Sequence source through the generator parts:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
269 lexer.feed(src)
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 368
diff changeset
270 grammar = parser.parse_grammar(lexer)
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
271 generator.generate(grammar, parser.headers, args.output)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
272
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
273
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
274 if __name__ == '__main__':
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
275 args = make_argument_parser().parse_args()
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
276 main(args)