-
Notifications
You must be signed in to change notification settings - Fork 0
/
imp_parser.py
166 lines (136 loc) · 5.17 KB
/
imp_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Copyright (c) 2011, Jay Conrod.
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Jay Conrod nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL JAY CONROD BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from imp_lexer import *
from combinators import *
from imp_ast import *
from functools import reduce
# Basic parsers
def keyword(kw):
return Reserved(kw, RESERVED)
num = Tag(INT) ^ (lambda i: int(i))
id = Tag(ID)
# Top level parser
def imp_parse(tokens):
ast = parser()(tokens, 0)
return ast
def parser():
return Phrase(stmt_list())
# Statements
def stmt_list():
separator = keyword(';') ^ (lambda x: lambda l, r: CompoundStatement(l, r))
return Exp(stmt(), separator)
def stmt():
return assign_stmt() | \
if_stmt() | \
while_stmt()
def print_stmt():
pass
def assign_stmt():
def process(parsed):
((name, _), exp) = parsed
return AssignStatement(name, exp)
return id + keyword('=') + aexp() ^ process
def if_stmt():
def process(parsed):
(((((_, condition), _), true_stmt), false_parsed), _) = parsed
if false_parsed:
(_, false_stmt) = false_parsed
else:
false_stmt = None
return IfStatement(condition, true_stmt, false_stmt)
return keyword('если') + bexp() + \
keyword('{') + Lazy(stmt_list) + \
Opt(keyword('иначе') + Lazy(stmt_list)) + \
keyword('}') ^ process
def while_stmt():
def process(parsed):
((((_, condition), _), body), _) = parsed
return WhileStatement(condition, body)
return keyword('пока') + bexp() + \
keyword('{') + Lazy(stmt_list) + \
keyword('}') ^ process
# Boolean expressions
def bexp():
return precedence(bexp_term(),
bexp_precedence_levels,
process_logic)
def bexp_term():
return bexp_not() | \
bexp_relop() | \
bexp_group()
def bexp_not():
return keyword('не') + Lazy(bexp_term) ^ (lambda parsed: NotBexp(parsed[1]))
def bexp_relop():
relops = ['<', '<=', '>', '>=', '==', '!=']
return aexp() + any_operator_in_list(relops) + aexp() ^ process_relop
def bexp_group():
return keyword('(') + Lazy(bexp) + keyword(')') ^ process_group
# Arithmetic expressions
def aexp():
return precedence(aexp_term(),
aexp_precedence_levels,
process_binop)
def aexp_term():
return aexp_value() | aexp_group()
def aexp_group():
return keyword('(') + Lazy(aexp) + keyword(')') ^ process_group
def aexp_value():
return (num ^ (lambda i: IntAexp(i))) | \
(id ^ (lambda v: VarAexp(v)))
# An IMP-specific combinator for binary operator expressions (aexp and bexp)
def precedence(value_parser, precedence_levels, combine):
def op_parser(precedence_level):
return any_operator_in_list(precedence_level) ^ combine
parser = value_parser * op_parser(precedence_levels[0])
for precedence_level in precedence_levels[1:]:
parser = parser * op_parser(precedence_level)
return parser
# Miscellaneous functions for binary and relational operators
def process_binop(op):
return lambda l, r: BinopAexp(op, l, r)
def process_relop(parsed):
((left, op), right) = parsed
return RelopBexp(op, left, right)
def process_logic(op):
if op == 'and':
return lambda l, r: AndBexp(l, r)
elif op == 'or':
return lambda l, r: OrBexp(l, r)
else:
raise RuntimeError('unknown logic operator: ' + op)
def process_group(parsed):
((_, p), _) = parsed
return p
def any_operator_in_list(ops):
op_parsers = [keyword(op) for op in ops]
parser = reduce(lambda l, r: l | r, op_parsers)
return parser
# Operator keywords and precedence levels
aexp_precedence_levels = [
['*', '/'],
['+', '-'],
]
bexp_precedence_levels = [
['и'],
['или'],
]