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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/pcpparse.rb', line 8
def initialize
@pycplus_parser = Parser.new("Pycplus parser") do
token(/#[^#]*#/)
token(/\n/)
token(/\s+/)
token(/if/) {:if}
token(/while/) {:while}
token(/def/) {:def}
token(/return/) {:return}
token(/false/) {|m| m}
token(/true/) {|m| m}
token(/>=/) {|m| m}
token(/<=/) {|m| m}
token(/==/) {|m| m}
token(/!=/) {|m| m}
token(/\+=/) {|m| m}
token(/\-=/) {|m| m}
token(/&&/) {|m| m}
token(/\|\|/) {|m| m}
token(/\d+\.\d+/) {|m| m.to_f}
token(/\d+/) {|m| m.to_i}
token(/\w+/) {|m| m}
token(/./) {|m| m}
start :program do
match(:statements) {|m| ProgramNode.new(m)}
end
rule :statements do
match(:statements, :statement) {|a,b| a << b}
match(:statement) {|a| [a]}
end
rule :statement do
match(:assignment, ';')
match(:function_call, ';')
match(:function_def)
match(:return_statement, ';')
match(:if_statement)
match(:while_statement)
match(:expression_statement)
end
rule :assignment do
match(:identifier, :assignment_OP, :logical_expr) {|a, b, c| AssignmentNode.new(a,b,c)}
match(:identifier, :assignment_OP, :array) {|a, b, c| AssignmentNode.new(a,b,c)}
end
rule :assignment_OP do
match('=') {|a| a}
match('+=') {|a| a}
match('-=') {|a| a}
end
rule :function_def do
match(:def, :identifier, '(', :parameters , ')', :block) {|_, a, _, b, _, c| FunctiondefNode.new(a,c,b)}
match(:def, :identifier, '(', ')', :block) {|_, a, _, _, b| FunctiondefNode.new(a,b)}
end
rule :parameters do
match(:parameters,',' ,:parameter) {|a ,_ , b| a << b}
match(:parameter) {|a| [a]}
end
rule :parameter do
match(:identifier)
end
rule :block do
match('{', :statements, '}') {|_,a,_| BlockNode.new(a)}
match('{', '}') {|_,_| BlockNode.new()}
end
rule :function_call do
match(:identifier,'(', :arguments, ')') {|a, _, b, _| FunctioncallNode.new(a,nil,b)}
match(:identifier,'(', ')') {|a, _, _| FunctioncallNode.new(a)}
match(:function_call,'.',:identifier ,'(',')') {|a, _, b, _, _| FunctioncallNode.new(b, a)}
match(:function_call,'.',:identifier ,'(',:arguments,')') {|a, _, b, _, c, _| FunctioncallNode.new(b,a,c)}
match(:object,'.',:identifier ,'(',')') {|a, _, b, _, _| FunctioncallNode.new(b, a)}
match(:object,'.',:identifier ,'(',:arguments,')') {|a, _, b, _, c, _| FunctioncallNode.new(b,a,c)}
end
rule :object do
match(:array)
match(:atom)
end
rule :arguments do
match(:arguments, ',', :argument) {|a ,_ , b| a << b}
match(:argument) {|a| [a]}
end
rule :argument do
match(:logical_expr)
match(:array)
end
rule :return_statement do
match(:return, :logical_expr) {|_, a| ReturnNode.new(a)}
match(:return, :array) {|_, a| ReturnNode.new(a)}
end
rule :while_statement do
match(:while, '(', :logical_expr, ')', :block) {|_,_,a,_,b| WhileNode.new(a,b)}
end
rule :if_statement do
match(:if, '(', :logical_expr, ')', :block) {|_,_,a,_,b| IfNode.new(a,b)}
end
rule :expression_statement do
match(:logical_expr, ';')
end
rule :logical_expr do
match(:logical_expr, :logical_OP, :comparsion_expr) {|a,b,c| LogicalNode.new(a,b,c)}
match(:comparsion_expr)
end
rule :logical_OP do
match('&&') {|a| a}
match('||') {|a| a}
end
rule :comparsion_expr do
match(:comparsion_expr, :comparsion_OP, :addition_expr) {|a,b,c| ComparisonNode.new(a,b,c)}
match(:addition_expr)
end
rule :comparsion_OP do
match('<') {|a| a}
match('>') {|a| a}
match('<=') {|a| a}
match('>=') {|a| a}
match('==') {|a| a}
match('!=') {|a| a}
end
rule :addition_expr do
match(:addition_expr, :addition_OP, :multiplication_expr) {|a, b, c| ArithmeticNode.new(a,b,c)}
match(:multiplication_expr)
end
rule :addition_OP do
match('+') {|a| a}
match('-') {|a| a}
end
rule :multiplication_expr do
match(:multiplication_expr, :multiplication_OP, :power_expr) {|a, b, c| ArithmeticNode.new(a,b,c)}
match(:power_expr)
end
rule :multiplication_OP do
match('%') {|a| a}
match('*') {|a| a}
match('/') {|_| :fdiv}
end
rule :power_expr do
match(:unary_expr, :power_OP, :power_expr) {|a, b, c| ArithmeticNode.new(a,b,c)}
match(:unary_expr)
end
rule :power_OP do
match('^') {|_| '**'}
end
rule :unary_expr do
match(:unary_OP, :unary_expr) {|a, b| UnaryNode.new(a,b)}
match(:binary_operand)
end
rule :unary_OP do
match('!') {|a| a}
match('-') {|a| a}
match('+') {|a| a}
end
rule :binary_operand do
match(:function_call)
match(:bool)
match(:digit)
match(:identifier)
match('(', :logical_expr, ')') {|_, a, _| a}
end
rule :array do
match('[', :elements , ']') {|_, a, _| ArrayNode.new(a)}
match('[', ']') {|_,_| ArrayNode.new()}
end
rule :elements do
match(:elements, ',', :element) {|a ,_ , b| a << b}
match(:element) {|a| [a]}
end
rule :element do
match(:logical_expr)
match(:array)
end
rule :atom do
match(:bool)
match(:digit)
match(:identifier)
end
rule :bool do
match('true') {|a| BoolNode.new(true)}
match('false') {|a| BoolNode.new(false)}
end
rule :digit do
match(Float) {|a| DigitNode.new(a)}
match(Integer) {|a| DigitNode.new(a)}
end
rule :identifier do
match(/[_a-zA-Z]+\w*/) {|a| IdentifierNode.new(a.to_sym)}
end
end
end
|