-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
175 lines (135 loc) · 4.39 KB
/
parser.py
File metadata and controls
175 lines (135 loc) · 4.39 KB
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
167
168
169
170
171
172
173
174
175
from sly import Parser
from code_generator import CodeGenerator
from definitions.compiler_exception import CompilerException
from lexer import CompilerLexer
class CompilerParser(Parser):
def __init__(self, generator):
if isinstance(generator, CodeGenerator):
self.generator = generator
tokens = CompilerLexer.tokens
precedence = (
('left', ADDITION, SUBTRACTION),
('left', MULTIPLICATION, DIVIDE),
)
'''
program
'''
@_('DECLARE declarations BEGIN commands END')
def program(self, p):
self.generator.end_program()
@_('BEGIN commands END')
def program(self, p):
self.generator.end_program()
'''
declarations
'''
@_('declarations COMMA PIDENTIFIER')
def declarations(self, p):
pass
@_('declarations COMMA PIDENTIFIER LEFTBRACKET NUMBER COLON NUMBER RIGHTBRACKET')
def declarations(self, p):
self.generator.check_array_declaration(p[2], p[4], p[6], p.lineno)
@_('PIDENTIFIER')
def declarations(self, p):
pass
@_('PIDENTIFIER LEFTBRACKET NUMBER COLON NUMBER RIGHTBRACKET')
def declarations(self, p):
self.generator.check_array_declaration(p[0], p[2], p[4], p.lineno)
'''
commands
'''
@_('commands command',
'command')
def commands(self, p):
pass
'''
command
'''
@_('identifier ASSIGN expression SEMICOLON')
def command(self, p):
self.generator.c_assign(p[0], p.lineno)
@_('IF condition begin_if THEN commands ELSE begin_else_if commands ENDIF')
def command(self, p):
self.generator.c_if()
@_('')
def begin_if(self, p):
self.generator.c_begin_if()
@_('')
def begin_else_if(self, p):
self.generator.c_if_else()
@_('IF condition begin_if THEN commands ENDIF')
def command(self, p):
self.generator.c_if()
@_('WHILE begin_while condition DO commands ENDWHILE')
def command(self, p):
self.generator.c_exit_while()
@_('REPEAT begin_while commands UNTIL condition SEMICOLON')
def command(self, p):
self.generator.c_exit_repeat()
@_('')
def begin_while(self, p):
self.generator.c_while()
@_('FOR PIDENTIFIER FROM value TO value DO begin_for_to commands ENDFOR')
def command(self, p):
self.generator.c_exit_for_to()
@_('')
def begin_for_to(self, p):
#print("FOR {} FROM {} TO {}".format(p[-6], p[-4], p[-2]))
self.generator.c_for_to(p[-6], p[-4], p[-2], 0)
@_('FOR PIDENTIFIER FROM value DOWNTO value DO begin_for_down_to commands ENDFOR')
def command(self, p):
self.generator.c_exit_for_down_to()
@_('')
def begin_for_down_to(self, p):
self.generator.c_for_down_to(p[-6], p[-4], p[-2], 0)
@_('READ identifier SEMICOLON')
def command(self, p):
self.generator.c_read(p[1], p.lineno)
@_('WRITE value SEMICOLON')
def command(self, p):
self.generator.c_write(p[1], p.lineno)
'''
expression
'''
@_('value')
def expression(self, p):
self.generator.e_value(p.value, 0)
@_('value ADDITION value',
'value SUBTRACTION value',
'value MULTIPLICATION value',
'value DIVIDE value',
'value MODULO value')
def expression(self, p):
self.generator.e_operation(p[0], p[2], p[1], p.lineno)
@_('value EQUAL value',
'value NOTEQUAL value',
'value LOWERTHAN value',
'value GREATERTHAN value',
'value LOWEREQUALTHAN value',
'value GREATEREQUALTHAN value')
def condition(self, p):
self.generator.conditions(p[0], p[2], p[1], p.lineno)
'''
value
'''
@_('NUMBER')
def value(self, p):
return p.NUMBER
@_('identifier')
def value(self, p):
return p.identifier
'''
identifier
'''
@_('PIDENTIFIER')
def identifier(self, p):
self.generator.check_identifier_reference(p[0], p.lineno)
return p.PIDENTIFIER
@_('PIDENTIFIER LEFTBRACKET PIDENTIFIER RIGHTBRACKET')
def identifier(self, p):
return self.generator.check_array_reference_by_identifier(p[0], p[2], p.lineno)
@_('PIDENTIFIER LEFTBRACKET NUMBER RIGHTBRACKET')
def identifier(self, p):
return self.generator.check_array_reference_by_number(p[0], p[2], p.lineno)
def error(self, token):
raise CompilerException("Syntax error in grammar", token.lineno)