GCCPY:
[official-gcc.git] / gcc / python / py-parser.y
blob98e432d2c665560ec7983522cfe5bdee0b8991e5
1 %{
2 /* This file is part of GCC.
4 GCC is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 3, or (at your option) any later
7 version.
9 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
18 /* Grammar largely bassed on
19 * - http://docs.python.org/release/2.5.2/ref/grammar.txt
22 #include "gpython.h"
24 #if !defined(YYLLOC_DEFAULT)
25 # define YYLLOC_DEFAULT(Current, Rhs, N) \
26 do \
27 if (N) \
28 { \
29 (Current).line = YYRHSLOC(Rhs, 1).line; \
30 (Current).column = YYRHSLOC(Rhs, 1).column; \
31 } \
32 else \
33 { \
34 (Current).line = YYRHSLOC(Rhs, 0).line; \
35 (Current).column = YYRHSLOC(Rhs, 0).column; \
36 } \
37 while (0)
38 #endif
40 #define YYDEBUG 1
42 static vec<gpydot, va_gc> * gpy_symbol_stack;
43 extern int yylineno;
45 extern int yylex (void);
46 extern void yyerror (const char *);
49 %union {
50 char * string;
51 long int integer;
52 gpy_dot_tree_t * symbol;
53 opcode_t opcode;
56 %debug
57 %locations
59 %error-verbose
60 %start declarations
62 %token CLASS "class"
63 %token DEF "def"
64 %token BREAK "break"
65 %token CONTINUE "continue"
66 %token RETURN "return"
67 %token FOR "for"
68 %token WHILE "while"
69 %token IN "in"
70 %token PRINT "print"
72 %token EXCEPT "except"
73 %token FINALLY "finally"
74 %token TRY "try"
76 %token AS "as"
77 %token ASSERT "assert"
78 %token DEL "del"
79 %token EXEC "exec"
80 %token FROM "from"
81 %token GLOBAL "global"
82 %token IMPORT "import"
83 %token IS "is"
84 %token LAMBDA "lambda"
85 %token PASS "pass"
86 %token RAISE "raise"
87 %token WITH "with"
88 %token YIELD "yield"
90 %token IF "if"
91 %token ELIF "elif"
92 %token ELSE "else"
94 %token OR "or"
95 %token AND "and"
96 %token NOT "not"
98 %token V_TRUE "True"
99 %token V_FALSE "False"
101 %token NEWLINE
102 %token INDENT
103 %token DEDENT
105 %token EQUAL_EQUAL
106 %token NOT_EQUAL
107 %token LESS
108 %token GREATER
109 %token LESS_EQUAL
110 %token GREATER_EQUAL
112 %token NONE
113 %token<string> IDENTIFIER
114 %token<string> STRING
115 %token<integer> INTEGER
116 %token<decimal> DOUBLE
118 %type<symbol> statement
119 %type<symbol> compound_stmt
120 %type<symbol> stmt_list
121 %type<symbol> simple_stmt
122 %type<symbol> expression_stmt
123 %type<symbol> target_list
124 %type<symbol> target
125 %type<symbol> funcdef
126 %type<symbol> classdef
127 %type<symbol> suite
128 %type<symbol> suite_statement_list
129 %type<symbol> indent_stmt
130 %type<symbol> literal
131 %type<symbol> atom
132 %type<symbol> primary
133 %type<symbol> expression
134 %type<symbol> call
135 %type<symbol> decl
136 %type<symbol> argument_list
137 %type<symbol> argument_list_stmt
138 %type<symbol> parameter_list
139 %type<symbol> parameter_list_stmt
140 %type<symbol> print_stmt
141 %type<symbol> attributeref
142 %type<symbol> ident
143 %type<symbol> while_stmt
144 %type<symbol> for_stmt
145 %type<symbol> ifblock
146 %type<symbol> elifstmt
147 %type<symbol> elsestmt
148 %type<symbol> elif_list
149 %type<symbol> elifblock
150 %type<symbol> if_stmt
151 %type<symbol> elseblock
152 %type<symbol> list_display
153 %type<symbol> enclosure
154 %type<symbol> return_stmt
155 %type<symbol> import_stmt
156 %type<symbol> slicing
157 %type<symbol> simple_slicing
159 %type<symbol> funcname
160 %type<symbol> classname
162 %left '='
163 %left '-' '+'
164 %left '*' '/'
165 %left EQUAL_EQUAL
166 %left LESS LESS_EQUAL
167 %left GREATER GREATER_EQUAL
168 %right '^'
169 %nonassoc UMINUS
173 declarations: /* epsilon */
174 | declarations decl
176 if ($2 != NULL)
177 dot_pass_manager_ProcessDecl ($2);
181 decl: NEWLINE
182 { $$ = NULL; }
183 | statement
186 while_stmt: WHILE expression ':' suite
187 { $$ = dot_build_decl2 (D_STRUCT_WHILE, $2, $4); }
189 ifblock: IF expression ':' suite
190 { $$ = dot_build_decl2 (D_STRUCT_IF, $2, $4); }
192 elifstmt: ELIF expression ':' suite
193 { $$ = dot_build_decl2 (D_STRUCT_ELIF, $2, $4); }
195 elsestmt: ELSE ':' suite
196 { $$ = dot_build_decl1 (D_STRUCT_ELSE, $3); }
199 elseblock:
200 { $$ = NULL; }
201 | elsestmt
204 elif_list: elif_list elifstmt
206 DOT_CHAIN($1) = $2;
207 $$ = $2;
209 | elifstmt
211 vec_safe_push (gpy_symbol_stack, $1);
212 $$ = $1;
216 elifblock:
217 { $$ = NULL; }
218 | elif_list
219 { $$ = gpy_symbol_stack->pop (); }
222 if_stmt: ifblock elifblock elseblock
223 { $$ = dot_build_conditional_struct ($1, $2, $3); }
226 compound_stmt: funcdef
227 | classdef
228 | while_stmt
229 | if_stmt
230 | for_stmt
233 for_stmt: FOR ident IN expression ':' suite
234 { $$ = dot_build_for ($2, $4, $6); }
237 classdef: CLASS classname ':' suite
239 gpy_dot_tree_t *dot = dot_build_class_decl ($2, $4);
240 $$ = dot;
244 classname: IDENTIFIER
245 { $$ = dot_build_identifier ($1); }
248 funcname: IDENTIFIER
249 { $$ = dot_build_identifier ($1); }
252 parameter_list_stmt:
253 { $$ = NULL; }
254 | parameter_list
255 { $$ = gpy_symbol_stack->pop (); }
258 parameter_list: parameter_list ',' ident
260 DOT_CHAIN($1) = $3;
261 $$ = $3;
263 | ident
265 vec_safe_push (gpy_symbol_stack, $1);
266 $$ = $1;
270 funcdef: DEF funcname '(' parameter_list_stmt ')' ':' suite
272 gpy_dot_tree_t *dot = dot_build_func_decl ($2, $4, $7);
273 $$ = dot;
277 suite: stmt_list NEWLINE
278 | NEWLINE INDENT suite_statement_list DEDENT
280 $$ = gpy_symbol_stack->pop ();
284 suite_statement_list: suite_statement_list indent_stmt
286 DOT_CHAIN($1) = $2;
287 $$ = $2;
289 | indent_stmt
291 vec_safe_push (gpy_symbol_stack, $1);
292 $$ = $1;
296 indent_stmt: statement
299 statement: stmt_list NEWLINE
300 | compound_stmt
303 stmt_list: simple_stmt
306 simple_stmt: expression
307 | print_stmt
308 | return_stmt
309 | import_stmt
312 import_stmt: IMPORT IDENTIFIER
314 $$ = dot_build_decl1 (D_KEY_IMPORT,
315 dot_build_identifier ($2));
319 return_stmt: RETURN expression
321 $$ = dot_build_decl1 (D_KEY_RETURN, $2);
323 | RETURN
325 $$ = dot_build_decl1 (D_KEY_RETURN, NULL);
329 argument_list_stmt:
330 { $$ = NULL; }
331 | argument_list
332 { $$ = gpy_symbol_stack->pop (); }
335 argument_list: argument_list ',' expression
337 DOT_CHAIN($1) = $3;
338 $$ = $3;
340 | expression
342 vec_safe_push ( gpy_symbol_stack, $1);
343 $$ = $1;
347 print_stmt: PRINT argument_list_stmt
349 gpy_dot_tree_t *dot = dot_build_decl1 (D_PRINT_STMT, $2);
350 $$ = dot;
354 expression: expression_stmt
357 target_list: target
360 target: IDENTIFIER
362 gpy_dot_tree_t *dot = dot_build_identifier ($1);
363 $$ = dot;
365 | slicing
366 | attributeref
369 ident: IDENTIFIER
370 { $$ = dot_build_identifier ($1); }
373 attributeref: primary '.' ident
375 $$ = dot_build_decl2 (D_ATTRIB_REF, $1, $3);
379 expression_stmt: target_list '=' expression_stmt
380 { $$ = dot_build_decl2 (D_MODIFY_EXPR, $1, $3); }
381 | expression_stmt '+' expression_stmt
382 { $$ = dot_build_decl2 (D_ADD_EXPR, $1, $3); }
383 | expression_stmt '-' expression_stmt
384 { $$ = dot_build_decl2 (D_MINUS_EXPR, $1, $3); }
385 | expression_stmt '*' expression_stmt
386 { $$ = dot_build_decl2 (D_MULT_EXPR, $1, $3); }
387 | expression_stmt LESS expression_stmt
388 { $$ = dot_build_decl2 (D_LESS_EXPR, $1, $3); }
389 | expression_stmt GREATER expression_stmt
390 { $$ = dot_build_decl2 (D_GREATER_EXPR, $1, $3); }
391 | expression_stmt EQUAL_EQUAL expression_stmt
392 { $$ = dot_build_decl2 (D_EQ_EQ_EXPR, $1, $3); }
393 | expression_stmt NOT_EQUAL expression_stmt
394 { $$ = dot_build_decl2 (D_NOT_EQ_EXPR, $1, $3); }
395 | '(' expression_stmt ')'
396 { $$ = $2; }
397 | primary
400 literal: INTEGER
402 gpy_dot_tree_t *dot = dot_build_integer ($1);
403 $$ = dot;
405 | STRING
407 gpy_dot_tree_t *dot = dot_build_string ($1);
408 $$ = dot;
412 atom: ident
413 | literal
414 | enclosure
417 call: primary '(' argument_list_stmt ')'
419 gpy_dot_tree_t *dot = dot_build_decl2 (D_CALL_EXPR, $1, $3);
420 $$ = dot;
424 list_display: '[' argument_list_stmt ']'
426 $$ = dot_build_decl1 (D_T_LIST, $2);
430 enclosure: list_display
433 slicing: simple_slicing
436 simple_slicing: primary '[' expression ']'
438 $$ = dot_build_decl2 (D_SLICE, $1, $3);
442 primary: atom
443 | call
444 | attributeref
445 | slicing
450 void yyerror (const char *msg)
452 fatal_error ("%s at line %i\n", msg, yylineno);