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
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
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
24 #if !defined(YYLLOC_DEFAULT)
25 # define YYLLOC_DEFAULT(Current, Rhs, N) \
29 (Current
).line
= YYRHSLOC
(Rhs
, 1).line
; \
30 (Current
).column
= YYRHSLOC
(Rhs
, 1).column
; \
34 (Current
).line
= YYRHSLOC
(Rhs
, 0).line
; \
35 (Current
).column
= YYRHSLOC
(Rhs
, 0).column
; \
42 static vec
<gpydot
, va_gc
> * gpy_symbol_stack
;
45 extern
int yylex (void);
46 extern
void yyerror (const char *);
52 gpy_dot_tree_t
* symbol
;
65 %token CONTINUE
"continue"
66 %token RETURN
"return"
72 %token EXCEPT
"except"
73 %token FINALLY
"finally"
77 %token ASSERT
"assert"
81 %token GLOBAL
"global"
82 %token IMPORT
"import"
84 %token LAMBDA
"lambda"
99 %token V_FALSE
"False"
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
125 %type
<symbol
> funcdef
126 %type
<symbol
> classdef
128 %type
<symbol
> suite_statement_list
129 %type
<symbol
> indent_stmt
130 %type
<symbol
> literal
132 %type
<symbol
> primary
133 %type
<symbol
> expression
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
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
166 %left LESS LESS_EQUAL
167 %left GREATER GREATER_EQUAL
173 declarations: /* epsilon */
177 dot_pass_manager_ProcessDecl
($2);
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); }
204 elif_list: elif_list elifstmt
211 vec_safe_push
(gpy_symbol_stack
, $1);
219 { $$
= gpy_symbol_stack
->pop
(); }
222 if_stmt: ifblock elifblock elseblock
223 { $$
= dot_build_conditional_struct
($1, $2, $3); }
226 compound_stmt: funcdef
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);
244 classname: IDENTIFIER
245 { $$
= dot_build_identifier
($1); }
249 { $$
= dot_build_identifier
($1); }
255 { $$
= gpy_symbol_stack
->pop
(); }
258 parameter_list: parameter_list
',' ident
265 vec_safe_push
(gpy_symbol_stack
, $1);
270 funcdef: DEF funcname
'(' parameter_list_stmt
')' ':' suite
272 gpy_dot_tree_t
*dot
= dot_build_func_decl
($2, $4, $7);
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
291 vec_safe_push
(gpy_symbol_stack
, $1);
296 indent_stmt: statement
299 statement: stmt_list NEWLINE
303 stmt_list: simple_stmt
306 simple_stmt: expression
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);
325 $$
= dot_build_decl1
(D_KEY_RETURN
, NULL
);
332 { $$
= gpy_symbol_stack
->pop
(); }
335 argument_list: argument_list
',' expression
342 vec_safe_push
( gpy_symbol_stack
, $1);
347 print_stmt: PRINT argument_list_stmt
349 gpy_dot_tree_t
*dot
= dot_build_decl1
(D_PRINT_STMT
, $2);
354 expression: expression_stmt
362 gpy_dot_tree_t
*dot
= dot_build_identifier
($1);
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
')'
402 gpy_dot_tree_t
*dot
= dot_build_integer
($1);
407 gpy_dot_tree_t
*dot
= dot_build_string
($1);
417 call: primary
'(' argument_list_stmt
')'
419 gpy_dot_tree_t
*dot
= dot_build_decl2
(D_CALL_EXPR
, $1, $3);
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);
450 void yyerror (const char *msg
)
452 fatal_error
("%s at line %i\n", msg
, yylineno
);