-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsintax.py
236 lines (174 loc) · 5.68 KB
/
sintax.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
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
234
235
236
import ply.yacc as yacc
from lexer import MyLexer
class Sintax():
tokens = MyLexer.tokens
def __init__(self):
l =[]
def p_program(p):
'''Program : Declaration FunctionTotal Main'''
def p_functionTotal(p):
'''FunctionTotal : Function
| RFunction'''
def p_main(p):
'''Main : MAIN LPAREN RPAREN FBlock'''
def p_declaration(p):
'''Declaration : Primitive Identifier List SEMI
| empty'''
def p_list(p):
'''List : Array
| empty'''
def p_array(p):
'''Array : LBRACKET INTEGER RBRACKET Matrix'''
def p_matrix(p):
'''Matrix : LBRACKET INTEGER RBRACKET
| empty'''
def p_function(p):
'''Function : VOID Identifier LPAREN ParamList RPAREN FBlock'''
def p_rFunction(p):
'''RFunction : Primitive Identifier LPAREN ParamList RPAREN RFBlock'''
def p_block(p):
'''Block : LBRACE Instruction RBRACE'''
def p_fBlock(p):
'''FBlock : LBRACE Declaration Instruction RBRACE'''
def p_rFBlock(p):
'''RFBlock : LBRACE Declaration Instruction RETURN SuperExpr SEMI RBRACE'''
def p_loop(p):
'''Loop : LOOP LPAREN SuperExpr RPAREN Block'''
def p_conditional(p):
'''Conditional : IF LPAREN SuperExpr RPAREN Block Else'''
def p_else(p):
'''Else : ELSE Block
| empty'''
def p_superExpr(p):
'''SuperExpr : Expression LogicalOp'''
def p_logicalOp(p):
'''LogicalOp : OR SuperExpr
| AND SuperExpr
| empty'''
def p_expression(p):
'''Expression : Expr Comparison'''
def p_comparison(p):
'''Comparison : GREATHAN Expr
| LESSTHAN Expr
| DIFFERENT Expr
| TWOEQUAL Expr
| GREATEQUAL Expr
| LESSEQUAL Expr
| empty'''
def p_expr(p):
'''Expr : Term Op1'''
def p_op1(p):
'''Op1 : PLUS Expr
| MINUS Expr
| empty'''
def p_term(p):
'''Term : Factor Op2'''
def p_op2(p):
'''Op2 : TIMES Term
| DIVIDE Term
| empty'''
def p_factor(p):
'''Factor : LPAREN SuperExpr RPAREN
| Op3 Constant'''
def p_op3(p):
'''Op3 : PLUS
| MINUS
| empty'''
def p_instruccion(p):
'''Instruction : Loop SEMI
| Assign SEMI
| Call SEMI
| Brush SEMI
| Read SEMI
| Print SEMI
| PenDown SEMI
| PenUp SEMI
| Home SEMI
| Forward SEMI
| Rotate SEMI
| Color SEMI
| Circle SEMI
| Arc SEMI
| Square SEMI'''
def p_assign(p):
'''Assign : Identifier EQUAL Assignation'''
def p_assignation(p):
'''Assignation : SuperExpr
| STRING
| Call'''
def p_call(p):
'''Call : Identifier LPAREN Parameters RPAREN'''
def p_read(p):
'''Read : READ LPAREN Type COMMA Identifier RPAREN'''
def p_type(p):
'''Type : Primitive
| STRING'''
def p_print(p):
'''Print : PRINT LPAREN Parameters RPAREN'''
def p_parameters(p):
'''Parameters : Params
| empty'''
def p_params(p):
'''Params : SuperExpr Params1
| STRING Params1'''
def p_params_1(p):
'''Params1 : COMMA Params
| empty'''
def p_brush(p):
'''Brush : BRUSH LPAREN Color COMMA SuperExpr RPAREN'''
def p_color(p):
'''Color : COLOR LPAREN SuperExpr COMMA SuperExpr COMMA SuperExpr RPAREN'''
def p_penDown(p):
'''PenDown : PD LPAREN RPAREN'''
def p_penUp(p):
'''PenUp : PU LPAREN RPAREN'''
def p_home(p):
'''Home : HOME LPAREN RPAREN'''
def p_forward(p):
'''Forward : FD LPAREN SuperExpr RPAREN'''
def p_rotate(p):
'''Rotate : RT LPAREN SuperExpr RPAREN'''
def p_circle(p):
'''Circle : CIRCLE LPAREN SuperExpr RPAREN'''
def p_arc(p):
'''Arc : ARC LPAREN SuperExpr COMMA SuperExpr RPAREN'''
def p_square(p):
'''Square : SQUARE LPAREN SuperExpr RPAREN'''
def p_paramList(p):
'''ParamList : Param ParamList1'''
def p_paramList_1(p):
'''ParamList1 : COMMA ParamList
| empty'''
def p_param(p):
'''Param : Primitive ListP Identifier'''
def p_ListP(p):
'''ListP : ArrayP ArrayP'''
def p_arrayp(p):
'''ArrayP : LBRACE RBRACE
| empty'''
def p_primitive(p):
'''Primitive : INT
| FLOAT
| BOOL
| CHAR'''
def p_identifier(p):
'''Identifier : ID'''
def p_constant(p):
'''Constant : INTEGER
| FLOAT
| Identifier'''
def p_empty(p):
'''empty : '''
pass
# Error rule for syntax errors
def p_error(p):
try:
print "Syntax error at line {0} col {1}, unexpected '{2}'".format(p.lineno, p.lexpos, p.value)
except:
pass
# Build the parser
parser = yacc.yacc()
with open(raw_input('filename > '), 'r') as f:
result = parser.parse(f.read())
print result
#print MyLexer.listOfTokens