annotate python/yacc.py @ 396:fb3c1f029b30

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