1%{
2/*
3   +----------------------------------------------------------------------+
4   | Zend Engine                                                          |
5   +----------------------------------------------------------------------+
6   | Copyright (c) 1998-2015 Zend Technologies Ltd. (http://www.zend.com) |
7   +----------------------------------------------------------------------+
8   | This source file is subject to version 2.00 of the Zend license,     |
9   | that is bundled with this package in the file LICENSE, and is        |
10   | available through the world-wide-web at the following url:           |
11   | http://www.zend.com/license/2_00.txt.                                |
12   | If you did not receive a copy of the Zend license and are unable to  |
13   | obtain it through the world-wide-web, please send a note to          |
14   | license@zend.com so we can mail you a copy immediately.              |
15   +----------------------------------------------------------------------+
16   | Authors: Andi Gutmans <andi@zend.com>                                |
17   |          Zeev Suraski <zeev@zend.com>                                |
18   |          Nikita Popov <nikic@php.net>                                |
19   +----------------------------------------------------------------------+
20*/
21
22/* $Id$ */
23
24#include "zend_compile.h"
25#include "zend.h"
26#include "zend_list.h"
27#include "zend_globals.h"
28#include "zend_API.h"
29#include "zend_constants.h"
30#include "zend_language_scanner.h"
31
32#define YYSIZE_T size_t
33#define yytnamerr zend_yytnamerr
34static YYSIZE_T zend_yytnamerr(char*, const char*);
35
36#define YYERROR_VERBOSE
37#define YYSTYPE zend_parser_stack_elem
38
39#ifdef _MSC_VER
40#define YYMALLOC malloc
41#define YYFREE free
42#endif
43
44%}
45
46%pure_parser
47%expect 0
48
49%code requires {
50}
51
52%destructor { zend_ast_destroy($$); } <ast>
53%destructor { if ($$) zend_string_release($$); } <str>
54
55%left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
56%left ','
57%left T_LOGICAL_OR
58%left T_LOGICAL_XOR
59%left T_LOGICAL_AND
60%right T_PRINT
61%right T_YIELD
62%right T_DOUBLE_ARROW
63%right T_YIELD_FROM
64%left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL T_POW_EQUAL
65%left '?' ':'
66%right T_COALESCE
67%left T_BOOLEAN_OR
68%left T_BOOLEAN_AND
69%left '|'
70%left '^'
71%left '&'
72%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL T_SPACESHIP
73%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
74%left T_SL T_SR
75%left '+' '-' '.'
76%left '*' '/' '%'
77%right '!'
78%nonassoc T_INSTANCEOF
79%right '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_ARRAY_CAST T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@'
80%right T_POW
81%right '['
82%nonassoc T_NEW T_CLONE
83%left T_NOELSE
84%left T_ELSEIF
85%left T_ELSE
86%left T_ENDIF
87%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC
88
89%token <ast> T_LNUMBER   "integer number (T_LNUMBER)"
90%token <ast> T_DNUMBER   "floating-point number (T_DNUMBER)"
91%token <ast> T_STRING    "identifier (T_STRING)"
92%token <ast> T_VARIABLE  "variable (T_VARIABLE)"
93%token <ast> T_INLINE_HTML
94%token <ast> T_ENCAPSED_AND_WHITESPACE  "quoted-string and whitespace (T_ENCAPSED_AND_WHITESPACE)"
95%token <ast> T_CONSTANT_ENCAPSED_STRING "quoted-string (T_CONSTANT_ENCAPSED_STRING)"
96%token <ast> T_STRING_VARNAME "variable name (T_STRING_VARNAME)"
97%token <ast> T_NUM_STRING "number (T_NUM_STRING)"
98
99%token END 0 "end of file"
100%token T_INCLUDE      "include (T_INCLUDE)"
101%token T_INCLUDE_ONCE "include_once (T_INCLUDE_ONCE)"
102%token T_EVAL         "eval (T_EVAL)"
103%token T_REQUIRE      "require (T_REQUIRE)"
104%token T_REQUIRE_ONCE "require_once (T_REQUIRE_ONCE)"
105%token T_LOGICAL_OR   "or (T_LOGICAL_OR)"
106%token T_LOGICAL_XOR  "xor (T_LOGICAL_XOR)"
107%token T_LOGICAL_AND  "and (T_LOGICAL_AND)"
108%token T_PRINT        "print (T_PRINT)"
109%token T_YIELD        "yield (T_YIELD)"
110%token T_YIELD_FROM   "yield from (T_YIELD_FROM)"
111%token T_PLUS_EQUAL   "+= (T_PLUS_EQUAL)"
112%token T_MINUS_EQUAL  "-= (T_MINUS_EQUAL)"
113%token T_MUL_EQUAL    "*= (T_MUL_EQUAL)"
114%token T_DIV_EQUAL    "/= (T_DIV_EQUAL)"
115%token T_CONCAT_EQUAL ".= (T_CONCAT_EQUAL)"
116%token T_MOD_EQUAL    "%= (T_MOD_EQUAL)"
117%token T_AND_EQUAL    "&= (T_AND_EQUAL)"
118%token T_OR_EQUAL     "|= (T_OR_EQUAL)"
119%token T_XOR_EQUAL    "^= (T_XOR_EQUAL)"
120%token T_SL_EQUAL     "<<= (T_SL_EQUAL)"
121%token T_SR_EQUAL     ">>= (T_SR_EQUAL)"
122%token T_BOOLEAN_OR   "|| (T_BOOLEAN_OR)"
123%token T_BOOLEAN_AND  "&& (T_BOOLEAN_AND)"
124%token T_IS_EQUAL     "== (T_IS_EQUAL)"
125%token T_IS_NOT_EQUAL "!= (T_IS_NOT_EQUAL)"
126%token T_IS_IDENTICAL "=== (T_IS_IDENTICAL)"
127%token T_IS_NOT_IDENTICAL "!== (T_IS_NOT_IDENTICAL)"
128%token T_IS_SMALLER_OR_EQUAL "<= (T_IS_SMALLER_OR_EQUAL)"
129%token T_IS_GREATER_OR_EQUAL ">= (T_IS_GREATER_OR_EQUAL)"
130%token T_SPACESHIP "<=> (T_SPACESHIP)"
131%token T_SL "<< (T_SL)"
132%token T_SR ">> (T_SR)"
133%token T_INSTANCEOF  "instanceof (T_INSTANCEOF)"
134%token T_INC "++ (T_INC)"
135%token T_DEC "-- (T_DEC)"
136%token T_INT_CAST    "(int) (T_INT_CAST)"
137%token T_DOUBLE_CAST "(double) (T_DOUBLE_CAST)"
138%token T_STRING_CAST "(string) (T_STRING_CAST)"
139%token T_ARRAY_CAST  "(array) (T_ARRAY_CAST)"
140%token T_OBJECT_CAST "(object) (T_OBJECT_CAST)"
141%token T_BOOL_CAST   "(bool) (T_BOOL_CAST)"
142%token T_UNSET_CAST  "(unset) (T_UNSET_CAST)"
143%token T_NEW       "new (T_NEW)"
144%token T_CLONE     "clone (T_CLONE)"
145%token T_EXIT      "exit (T_EXIT)"
146%token T_IF        "if (T_IF)"
147%token T_ELSEIF    "elseif (T_ELSEIF)"
148%token T_ELSE      "else (T_ELSE)"
149%token T_ENDIF     "endif (T_ENDIF)"
150%token T_ECHO       "echo (T_ECHO)"
151%token T_DO         "do (T_DO)"
152%token T_WHILE      "while (T_WHILE)"
153%token T_ENDWHILE   "endwhile (T_ENDWHILE)"
154%token T_FOR        "for (T_FOR)"
155%token T_ENDFOR     "endfor (T_ENDFOR)"
156%token T_FOREACH    "foreach (T_FOREACH)"
157%token T_ENDFOREACH "endforeach (T_ENDFOREACH)"
158%token T_DECLARE    "declare (T_DECLARE)"
159%token T_ENDDECLARE "enddeclare (T_ENDDECLARE)"
160%token T_AS         "as (T_AS)"
161%token T_SWITCH     "switch (T_SWITCH)"
162%token T_ENDSWITCH  "endswitch (T_ENDSWITCH)"
163%token T_CASE       "case (T_CASE)"
164%token T_DEFAULT    "default (T_DEFAULT)"
165%token T_BREAK      "break (T_BREAK)"
166%token T_CONTINUE   "continue (T_CONTINUE)"
167%token T_GOTO       "goto (T_GOTO)"
168%token T_FUNCTION   "function (T_FUNCTION)"
169%token T_CONST      "const (T_CONST)"
170%token T_RETURN     "return (T_RETURN)"
171%token T_TRY        "try (T_TRY)"
172%token T_CATCH      "catch (T_CATCH)"
173%token T_FINALLY    "finally (T_FINALLY)"
174%token T_THROW      "throw (T_THROW)"
175%token T_USE        "use (T_USE)"
176%token T_INSTEADOF  "insteadof (T_INSTEADOF)"
177%token T_GLOBAL     "global (T_GLOBAL)"
178%token T_STATIC     "static (T_STATIC)"
179%token T_ABSTRACT   "abstract (T_ABSTRACT)"
180%token T_FINAL      "final (T_FINAL)"
181%token T_PRIVATE    "private (T_PRIVATE)"
182%token T_PROTECTED  "protected (T_PROTECTED)"
183%token T_PUBLIC     "public (T_PUBLIC)"
184%token T_VAR        "var (T_VAR)"
185%token T_UNSET      "unset (T_UNSET)"
186%token T_ISSET      "isset (T_ISSET)"
187%token T_EMPTY      "empty (T_EMPTY)"
188%token T_HALT_COMPILER "__halt_compiler (T_HALT_COMPILER)"
189%token T_CLASS      "class (T_CLASS)"
190%token T_TRAIT      "trait (T_TRAIT)"
191%token T_INTERFACE  "interface (T_INTERFACE)"
192%token T_EXTENDS    "extends (T_EXTENDS)"
193%token T_IMPLEMENTS "implements (T_IMPLEMENTS)"
194%token T_OBJECT_OPERATOR "-> (T_OBJECT_OPERATOR)"
195%token T_DOUBLE_ARROW    "=> (T_DOUBLE_ARROW)"
196%token T_LIST            "list (T_LIST)"
197%token T_ARRAY           "array (T_ARRAY)"
198%token T_CALLABLE        "callable (T_CALLABLE)"
199%token T_LINE            "__LINE__ (T_LINE)"
200%token T_FILE            "__FILE__ (T_FILE)"
201%token T_DIR             "__DIR__ (T_DIR)"
202%token T_CLASS_C         "__CLASS__ (T_CLASS_C)"
203%token T_TRAIT_C         "__TRAIT__ (T_TRAIT_C)"
204%token T_METHOD_C        "__METHOD__ (T_METHOD_C)"
205%token T_FUNC_C          "__FUNCTION__ (T_FUNC_C)"
206%token T_COMMENT         "comment (T_COMMENT)"
207%token T_DOC_COMMENT     "doc comment (T_DOC_COMMENT)"
208%token T_OPEN_TAG        "open tag (T_OPEN_TAG)"
209%token T_OPEN_TAG_WITH_ECHO "open tag with echo (T_OPEN_TAG_WITH_ECHO)"
210%token T_CLOSE_TAG       "close tag (T_CLOSE_TAG)"
211%token T_WHITESPACE      "whitespace (T_WHITESPACE)"
212%token T_START_HEREDOC   "heredoc start (T_START_HEREDOC)"
213%token T_END_HEREDOC     "heredoc end (T_END_HEREDOC)"
214%token T_DOLLAR_OPEN_CURLY_BRACES "${ (T_DOLLAR_OPEN_CURLY_BRACES)"
215%token T_CURLY_OPEN      "{$ (T_CURLY_OPEN)"
216%token T_PAAMAYIM_NEKUDOTAYIM ":: (T_PAAMAYIM_NEKUDOTAYIM)"
217%token T_NAMESPACE       "namespace (T_NAMESPACE)"
218%token T_NS_C            "__NAMESPACE__ (T_NS_C)"
219%token T_NS_SEPARATOR    "\\ (T_NS_SEPARATOR)"
220%token T_ELLIPSIS        "... (T_ELLIPSIS)"
221%token T_COALESCE        "?? (T_COALESCE)"
222%token T_POW             "** (T_POW)"
223%token T_POW_EQUAL       "**= (T_POW_EQUAL)"
224
225/* Token used to force a parse error from the lexer */
226%token T_ERROR
227
228%type <ast> top_statement namespace_name name statement function_declaration_statement
229%type <ast> class_declaration_statement trait_declaration_statement
230%type <ast> interface_declaration_statement interface_extends_list
231%type <ast> group_use_declaration inline_use_declarations inline_use_declaration
232%type <ast> mixed_group_use_declaration use_declaration unprefixed_use_declaration
233%type <ast> unprefixed_use_declarations const_decl inner_statement
234%type <ast> expr optional_expr while_statement for_statement foreach_variable
235%type <ast> foreach_statement declare_statement finally_statement unset_variable variable
236%type <ast> extends_from parameter optional_type argument expr_without_variable global_var
237%type <ast> static_var class_statement trait_adaptation trait_precedence trait_alias
238%type <ast> absolute_trait_method_reference trait_method_reference property echo_expr
239%type <ast> new_expr anonymous_class class_name class_name_reference simple_variable
240%type <ast> internal_functions_in_yacc
241%type <ast> exit_expr scalar backticks_expr lexical_var function_call member_name property_name
242%type <ast> variable_class_name dereferencable_scalar constant dereferencable
243%type <ast> callable_expr callable_variable static_member new_variable
244%type <ast> assignment_list_element array_pair encaps_var encaps_var_offset isset_variables
245%type <ast> top_statement_list use_declarations const_list inner_statement_list if_stmt
246%type <ast> alt_if_stmt for_exprs switch_case_list global_var_list static_var_list
247%type <ast> echo_expr_list unset_variables catch_list parameter_list class_statement_list
248%type <ast> implements_list case_list if_stmt_without_else
249%type <ast> non_empty_parameter_list argument_list non_empty_argument_list property_list
250%type <ast> class_const_list class_const_decl name_list trait_adaptations method_body non_empty_for_exprs
251%type <ast> ctor_arguments alt_if_stmt_without_else trait_adaptation_list lexical_vars
252%type <ast> lexical_var_list encaps_list array_pair_list non_empty_array_pair_list
253%type <ast> assignment_list isset_variable type return_type
254%type <ast> identifier
255
256%type <num> returns_ref function is_reference is_variadic variable_modifiers
257%type <num> method_modifiers non_empty_member_modifiers member_modifier
258%type <num> class_modifiers class_modifier use_type
259
260%type <str> backup_doc_comment
261
262%% /* Rules */
263
264start:
265    top_statement_list  { CG(ast) = $1; }
266;
267
268reserved_non_modifiers:
269      T_INCLUDE | T_INCLUDE_ONCE | T_EVAL | T_REQUIRE | T_REQUIRE_ONCE | T_LOGICAL_OR | T_LOGICAL_XOR | T_LOGICAL_AND
270    | T_INSTANCEOF | T_NEW | T_CLONE | T_EXIT | T_IF | T_ELSEIF | T_ELSE | T_ENDIF | T_ECHO | T_DO | T_WHILE | T_ENDWHILE
271    | T_FOR | T_ENDFOR | T_FOREACH | T_ENDFOREACH | T_DECLARE | T_ENDDECLARE | T_AS | T_TRY | T_CATCH | T_FINALLY
272    | T_THROW | T_USE | T_INSTEADOF | T_GLOBAL | T_VAR | T_UNSET | T_ISSET | T_EMPTY | T_CONTINUE | T_GOTO
273    | T_FUNCTION | T_CONST | T_RETURN | T_PRINT | T_YIELD | T_LIST | T_SWITCH | T_ENDSWITCH | T_CASE | T_DEFAULT | T_BREAK
274    | T_ARRAY | T_CALLABLE | T_EXTENDS | T_IMPLEMENTS | T_NAMESPACE | T_TRAIT | T_INTERFACE | T_CLASS
275;
276
277semi_reserved:
278      reserved_non_modifiers
279    | T_STATIC | T_ABSTRACT | T_FINAL | T_PRIVATE | T_PROTECTED | T_PUBLIC
280;
281
282identifier:
283        T_STRING { $$ = $1; }
284    |   semi_reserved  {
285            zval zv;
286            zend_lex_tstring(&zv);
287            $$ = zend_ast_create_zval(&zv);
288        }
289;
290
291top_statement_list:
292        top_statement_list top_statement { $$ = zend_ast_list_add($1, $2); }
293    |   /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_STMT_LIST); }
294;
295
296namespace_name:
297        T_STRING                                { $$ = $1; }
298    |   namespace_name T_NS_SEPARATOR T_STRING  { $$ = zend_ast_append_str($1, $3); }
299;
300
301name:
302        namespace_name                              { $$ = $1; $$->attr = ZEND_NAME_NOT_FQ; }
303    |   T_NAMESPACE T_NS_SEPARATOR namespace_name   { $$ = $3; $$->attr = ZEND_NAME_RELATIVE; }
304    |   T_NS_SEPARATOR namespace_name               { $$ = $2; $$->attr = ZEND_NAME_FQ; }
305;
306
307top_statement:
308        statement                           { $$ = $1; }
309    |   function_declaration_statement      { $$ = $1; }
310    |   class_declaration_statement         { $$ = $1; }
311    |   trait_declaration_statement         { $$ = $1; }
312    |   interface_declaration_statement     { $$ = $1; }
313    |   T_HALT_COMPILER '(' ')' ';'
314            { $$ = zend_ast_create(ZEND_AST_HALT_COMPILER,
315                  zend_ast_create_zval_from_long(zend_get_scanned_file_offset()));
316              zend_stop_lexing(); }
317    |   T_NAMESPACE namespace_name ';'
318            { $$ = zend_ast_create(ZEND_AST_NAMESPACE, $2, NULL);
319              RESET_DOC_COMMENT(); }
320    |   T_NAMESPACE namespace_name { RESET_DOC_COMMENT(); }
321        '{' top_statement_list '}'
322            { $$ = zend_ast_create(ZEND_AST_NAMESPACE, $2, $5); }
323    |   T_NAMESPACE { RESET_DOC_COMMENT(); }
324        '{' top_statement_list '}'
325            { $$ = zend_ast_create(ZEND_AST_NAMESPACE, NULL, $4); }
326    |   T_USE mixed_group_use_declaration ';'       { $$ = $2; }
327    |   T_USE use_type group_use_declaration ';'    { $$ = $3; $$->attr = $2; }
328    |   T_USE use_declarations ';'                  { $$ = $2; $$->attr = T_CLASS; }
329    |   T_USE use_type use_declarations ';'         { $$ = $3; $$->attr = $2; }
330    |   T_CONST const_list ';'                      { $$ = $2; }
331;
332
333use_type:
334        T_FUNCTION      { $$ = T_FUNCTION; }
335    |   T_CONST         { $$ = T_CONST; }
336;
337
338group_use_declaration:
339        namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations '}'
340            { $$ = zend_ast_create(ZEND_AST_GROUP_USE, $1, $4); }
341    |   T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations '}'
342            { $$ = zend_ast_create(ZEND_AST_GROUP_USE, $2, $5); }
343;
344
345mixed_group_use_declaration:
346        namespace_name T_NS_SEPARATOR '{' inline_use_declarations '}'
347            { $$ = zend_ast_create(ZEND_AST_GROUP_USE, $1, $4);}
348    |   T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' inline_use_declarations '}'
349            { $$ = zend_ast_create(ZEND_AST_GROUP_USE, $2, $5); }
350;
351
352inline_use_declarations:
353        inline_use_declarations ',' inline_use_declaration
354            { $$ = zend_ast_list_add($1, $3); }
355    |   inline_use_declaration
356            { $$ = zend_ast_create_list(1, ZEND_AST_USE, $1); }
357;
358
359unprefixed_use_declarations:
360        unprefixed_use_declarations ',' unprefixed_use_declaration
361            { $$ = zend_ast_list_add($1, $3); }
362    |   unprefixed_use_declaration
363            { $$ = zend_ast_create_list(1, ZEND_AST_USE, $1); }
364;
365
366use_declarations:
367        use_declarations ',' use_declaration
368            { $$ = zend_ast_list_add($1, $3); }
369    |   use_declaration
370            { $$ = zend_ast_create_list(1, ZEND_AST_USE, $1); }
371;
372
373inline_use_declaration:
374        unprefixed_use_declaration { $$ = $1; $$->attr = T_CLASS; }
375    |   use_type unprefixed_use_declaration { $$ = $2; $$->attr = $1; }
376;
377
378unprefixed_use_declaration:
379        namespace_name
380            { $$ = zend_ast_create(ZEND_AST_USE_ELEM, $1, NULL); }
381    |   namespace_name T_AS T_STRING
382            { $$ = zend_ast_create(ZEND_AST_USE_ELEM, $1, $3); }
383;
384
385use_declaration:
386        unprefixed_use_declaration                { $$ = $1; }
387    |   T_NS_SEPARATOR unprefixed_use_declaration { $$ = $2; }
388;
389
390const_list:
391        const_list ',' const_decl { $$ = zend_ast_list_add($1, $3); }
392    |   const_decl { $$ = zend_ast_create_list(1, ZEND_AST_CONST_DECL, $1); }
393;
394
395inner_statement_list:
396        inner_statement_list inner_statement
397            { $$ = zend_ast_list_add($1, $2); }
398    |   /* empty */
399            { $$ = zend_ast_create_list(0, ZEND_AST_STMT_LIST); }
400;
401
402
403inner_statement:
404        statement { $$ = $1; }
405    |   function_declaration_statement      { $$ = $1; }
406    |   class_declaration_statement         { $$ = $1; }
407    |   trait_declaration_statement         { $$ = $1; }
408    |   interface_declaration_statement     { $$ = $1; }
409    |   T_HALT_COMPILER '(' ')' ';'
410            { $$ = NULL; zend_error_noreturn(E_COMPILE_ERROR,
411                  "__HALT_COMPILER() can only be used from the outermost scope"); }
412;
413
414
415statement:
416        '{' inner_statement_list '}' { $$ = $2; }
417    |   if_stmt { $$ = $1; }
418    |   alt_if_stmt { $$ = $1; }
419    |   T_WHILE '(' expr ')' while_statement
420            { $$ = zend_ast_create(ZEND_AST_WHILE, $3, $5); }
421    |   T_DO statement T_WHILE '(' expr ')' ';'
422            { $$ = zend_ast_create(ZEND_AST_DO_WHILE, $2, $5); }
423    |   T_FOR '(' for_exprs ';' for_exprs ';' for_exprs ')' for_statement
424            { $$ = zend_ast_create(ZEND_AST_FOR, $3, $5, $7, $9); }
425    |   T_SWITCH '(' expr ')' switch_case_list
426            { $$ = zend_ast_create(ZEND_AST_SWITCH, $3, $5); }
427    |   T_BREAK optional_expr ';'       { $$ = zend_ast_create(ZEND_AST_BREAK, $2); }
428    |   T_CONTINUE optional_expr ';'    { $$ = zend_ast_create(ZEND_AST_CONTINUE, $2); }
429    |   T_RETURN optional_expr ';'      { $$ = zend_ast_create(ZEND_AST_RETURN, $2); }
430    |   T_GLOBAL global_var_list ';'    { $$ = $2; }
431    |   T_STATIC static_var_list ';'    { $$ = $2; }
432    |   T_ECHO echo_expr_list ';'       { $$ = $2; }
433    |   T_INLINE_HTML { $$ = zend_ast_create(ZEND_AST_ECHO, $1); }
434    |   expr ';' { $$ = $1; }
435    |   T_UNSET '(' unset_variables ')' ';' { $$ = $3; }
436    |   T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
437            { $$ = zend_ast_create(ZEND_AST_FOREACH, $3, $5, NULL, $7); }
438    |   T_FOREACH '(' expr T_AS foreach_variable T_DOUBLE_ARROW foreach_variable ')'
439        foreach_statement
440            { $$ = zend_ast_create(ZEND_AST_FOREACH, $3, $7, $5, $9); }
441    |   T_DECLARE '(' const_list ')'
442            { zend_handle_encoding_declaration($3); }
443        declare_statement
444            { $$ = zend_ast_create(ZEND_AST_DECLARE, $3, $6); }
445    |   ';' /* empty statement */ { $$ = NULL; }
446    |   T_TRY '{' inner_statement_list '}' catch_list finally_statement
447            { $$ = zend_ast_create(ZEND_AST_TRY, $3, $5, $6); }
448    |   T_THROW expr ';' { $$ = zend_ast_create(ZEND_AST_THROW, $2); }
449    |   T_GOTO T_STRING ';' { $$ = zend_ast_create(ZEND_AST_GOTO, $2); }
450    |   T_STRING ':' { $$ = zend_ast_create(ZEND_AST_LABEL, $1); }
451;
452
453catch_list:
454        /* empty */
455            { $$ = zend_ast_create_list(0, ZEND_AST_CATCH_LIST); }
456    |   catch_list T_CATCH '(' name T_VARIABLE ')' '{' inner_statement_list '}'
457            { $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_CATCH, $4, $5, $8)); }
458;
459
460finally_statement:
461        /* empty */ { $$ = NULL; }
462    |   T_FINALLY '{' inner_statement_list '}' { $$ = $3; }
463;
464
465unset_variables:
466        unset_variable { $$ = zend_ast_create_list(1, ZEND_AST_STMT_LIST, $1); }
467    |   unset_variables ',' unset_variable { $$ = zend_ast_list_add($1, $3); }
468;
469
470unset_variable:
471        variable { $$ = zend_ast_create(ZEND_AST_UNSET, $1); }
472;
473
474function_declaration_statement:
475    function returns_ref T_STRING '(' parameter_list ')' return_type
476    backup_doc_comment '{' inner_statement_list '}'
477        { $$ = zend_ast_create_decl(ZEND_AST_FUNC_DECL, $2, $1, $8,
478              zend_ast_get_str($3), $5, NULL, $10, $7); }
479;
480
481is_reference:
482        /* empty */ { $$ = 0; }
483    |   '&'         { $$ = ZEND_PARAM_REF; }
484;
485
486is_variadic:
487        /* empty */ { $$ = 0; }
488    |   T_ELLIPSIS  { $$ = ZEND_PARAM_VARIADIC; }
489;
490
491class_declaration_statement:
492        class_modifiers T_CLASS { $<num>$ = CG(zend_lineno); }
493        T_STRING extends_from implements_list backup_doc_comment '{' class_statement_list '}'
494            { $$ = zend_ast_create_decl(ZEND_AST_CLASS, $1, $<num>3, $7, zend_ast_get_str($4), $5, $6, $9, NULL); }
495    |   T_CLASS { $<num>$ = CG(zend_lineno); }
496        T_STRING extends_from implements_list backup_doc_comment '{' class_statement_list '}'
497            { $$ = zend_ast_create_decl(ZEND_AST_CLASS, 0, $<num>2, $6, zend_ast_get_str($3), $4, $5, $8, NULL); }
498;
499
500class_modifiers:
501        class_modifier                  { $$ = $1; }
502    |   class_modifiers class_modifier  { $$ = zend_add_class_modifier($1, $2); }
503;
504
505class_modifier:
506        T_ABSTRACT      { $$ = ZEND_ACC_EXPLICIT_ABSTRACT_CLASS; }
507    |   T_FINAL         { $$ = ZEND_ACC_FINAL; }
508;
509
510trait_declaration_statement:
511        T_TRAIT { $<num>$ = CG(zend_lineno); }
512        T_STRING backup_doc_comment '{' class_statement_list '}'
513            { $$ = zend_ast_create_decl(ZEND_AST_CLASS, ZEND_ACC_TRAIT, $<num>2, $4, zend_ast_get_str($3), NULL, NULL, $6, NULL); }
514;
515
516interface_declaration_statement:
517        T_INTERFACE { $<num>$ = CG(zend_lineno); }
518        T_STRING interface_extends_list backup_doc_comment '{' class_statement_list '}'
519            { $$ = zend_ast_create_decl(ZEND_AST_CLASS, ZEND_ACC_INTERFACE, $<num>2, $5, zend_ast_get_str($3), NULL, $4, $7, NULL); }
520;
521
522extends_from:
523        /* empty */     { $$ = NULL; }
524    |   T_EXTENDS name  { $$ = $2; }
525;
526
527interface_extends_list:
528        /* empty */         { $$ = NULL; }
529    |   T_EXTENDS name_list { $$ = $2; }
530;
531
532implements_list:
533        /* empty */             { $$ = NULL; }
534    |   T_IMPLEMENTS name_list  { $$ = $2; }
535;
536
537foreach_variable:
538        variable            { $$ = $1; }
539    |   '&' variable        { $$ = zend_ast_create(ZEND_AST_REF, $2); }
540    |   T_LIST '(' assignment_list ')' { $$ = $3; }
541;
542
543for_statement:
544        statement { $$ = $1; }
545    |   ':' inner_statement_list T_ENDFOR ';' { $$ = $2; }
546;
547
548foreach_statement:
549        statement { $$ = $1; }
550    |   ':' inner_statement_list T_ENDFOREACH ';' { $$ = $2; }
551;
552
553declare_statement:
554        statement { $$ = $1; }
555    |   ':' inner_statement_list T_ENDDECLARE ';' { $$ = $2; }
556;
557
558switch_case_list:
559        '{' case_list '}'                   { $$ = $2; }
560    |   '{' ';' case_list '}'               { $$ = $3; }
561    |   ':' case_list T_ENDSWITCH ';'       { $$ = $2; }
562    |   ':' ';' case_list T_ENDSWITCH ';'   { $$ = $3; }
563;
564
565case_list:
566        /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_SWITCH_LIST); }
567    |   case_list T_CASE expr case_separator inner_statement_list
568            { $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_SWITCH_CASE, $3, $5)); }
569    |   case_list T_DEFAULT case_separator inner_statement_list
570            { $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_SWITCH_CASE, NULL, $4)); }
571;
572
573case_separator:
574        ':'
575    |   ';'
576;
577
578
579while_statement:
580        statement { $$ = $1; }
581    |   ':' inner_statement_list T_ENDWHILE ';' { $$ = $2; }
582;
583
584
585if_stmt_without_else:
586        T_IF '(' expr ')' statement
587            { $$ = zend_ast_create_list(1, ZEND_AST_IF,
588                  zend_ast_create(ZEND_AST_IF_ELEM, $3, $5)); }
589    |   if_stmt_without_else T_ELSEIF '(' expr ')' statement
590            { $$ = zend_ast_list_add($1,
591                  zend_ast_create(ZEND_AST_IF_ELEM, $4, $6)); }
592;
593
594if_stmt:
595        if_stmt_without_else %prec T_NOELSE { $$ = $1; }
596    |   if_stmt_without_else T_ELSE statement
597            { $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_IF_ELEM, NULL, $3)); }
598;
599
600alt_if_stmt_without_else:
601        T_IF '(' expr ')' ':' inner_statement_list
602            { $$ = zend_ast_create_list(1, ZEND_AST_IF,
603                  zend_ast_create(ZEND_AST_IF_ELEM, $3, $6)); }
604    |   alt_if_stmt_without_else T_ELSEIF '(' expr ')' ':' inner_statement_list
605            { $$ = zend_ast_list_add($1,
606                  zend_ast_create(ZEND_AST_IF_ELEM, $4, $7)); }
607;
608
609alt_if_stmt:
610        alt_if_stmt_without_else T_ENDIF ';' { $$ = $1; }
611    |   alt_if_stmt_without_else T_ELSE ':' inner_statement_list T_ENDIF ';'
612            { $$ = zend_ast_list_add($1,
613                  zend_ast_create(ZEND_AST_IF_ELEM, NULL, $4)); }
614;
615
616parameter_list:
617        non_empty_parameter_list { $$ = $1; }
618    |   /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_PARAM_LIST); }
619;
620
621
622non_empty_parameter_list:
623        parameter
624            { $$ = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, $1); }
625    |   non_empty_parameter_list ',' parameter
626            { $$ = zend_ast_list_add($1, $3); }
627;
628
629parameter:
630        optional_type is_reference is_variadic T_VARIABLE
631            { $$ = zend_ast_create_ex(ZEND_AST_PARAM, $2 | $3, $1, $4, NULL); }
632    |   optional_type is_reference is_variadic T_VARIABLE '=' expr
633            { $$ = zend_ast_create_ex(ZEND_AST_PARAM, $2 | $3, $1, $4, $6); }
634;
635
636
637optional_type:
638        /* empty */ { $$ = NULL; }
639    |   type        { $$ = $1; }
640;
641
642type:
643        T_ARRAY     { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_ARRAY); }
644    |   T_CALLABLE  { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_CALLABLE); }
645    |   name        { $$ = $1; }
646;
647
648return_type:
649        /* empty */ { $$ = NULL; }
650    |   ':' type    { $$ = $2; }
651;
652
653argument_list:
654        '(' ')' { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST); }
655    |   '(' non_empty_argument_list ')' { $$ = $2; }
656;
657
658non_empty_argument_list:
659        argument
660            { $$ = zend_ast_create_list(1, ZEND_AST_ARG_LIST, $1); }
661    |   non_empty_argument_list ',' argument
662            { $$ = zend_ast_list_add($1, $3); }
663;
664
665argument:
666        expr            { $$ = $1; }
667    |   T_ELLIPSIS expr { $$ = zend_ast_create(ZEND_AST_UNPACK, $2); }
668;
669
670global_var_list:
671        global_var_list ',' global_var { $$ = zend_ast_list_add($1, $3); }
672    |   global_var { $$ = zend_ast_create_list(1, ZEND_AST_STMT_LIST, $1); }
673;
674
675global_var:
676    simple_variable
677        { $$ = zend_ast_create(ZEND_AST_GLOBAL, zend_ast_create(ZEND_AST_VAR, $1)); }
678;
679
680
681static_var_list:
682        static_var_list ',' static_var { $$ = zend_ast_list_add($1, $3); }
683    |   static_var { $$ = zend_ast_create_list(1, ZEND_AST_STMT_LIST, $1); }
684;
685
686static_var:
687        T_VARIABLE          { $$ = zend_ast_create(ZEND_AST_STATIC, $1, NULL); }
688    |   T_VARIABLE '=' expr { $$ = zend_ast_create(ZEND_AST_STATIC, $1, $3); }
689;
690
691
692class_statement_list:
693        class_statement_list class_statement
694            { $$ = zend_ast_list_add($1, $2); }
695    |   /* empty */
696            { $$ = zend_ast_create_list(0, ZEND_AST_STMT_LIST); }
697;
698
699
700class_statement:
701        variable_modifiers property_list ';'
702            { $$ = zend_ast_append_doc_comment($2); $$->attr = $1; }
703    |   T_CONST class_const_list ';'
704            { $$ = $2; RESET_DOC_COMMENT(); }
705    |   T_USE name_list trait_adaptations
706            { $$ = zend_ast_create(ZEND_AST_USE_TRAIT, $2, $3); }
707    |   method_modifiers function returns_ref identifier '(' parameter_list ')'
708        return_type backup_doc_comment method_body
709            { $$ = zend_ast_create_decl(ZEND_AST_METHOD, $3 | $1, $2, $9,
710                  zend_ast_get_str($4), $6, NULL, $10, $8); }
711;
712
713name_list:
714        name { $$ = zend_ast_create_list(1, ZEND_AST_NAME_LIST, $1); }
715    |   name_list ',' name { $$ = zend_ast_list_add($1, $3); }
716;
717
718trait_adaptations:
719        ';'                             { $$ = NULL; }
720    |   '{' '}'                         { $$ = NULL; }
721    |   '{' trait_adaptation_list '}'   { $$ = $2; }
722;
723
724trait_adaptation_list:
725        trait_adaptation
726            { $$ = zend_ast_create_list(1, ZEND_AST_TRAIT_ADAPTATIONS, $1); }
727    |   trait_adaptation_list trait_adaptation
728            { $$ = zend_ast_list_add($1, $2); }
729;
730
731trait_adaptation:
732        trait_precedence ';'    { $$ = $1; }
733    |   trait_alias ';'         { $$ = $1; }
734;
735
736trait_precedence:
737    absolute_trait_method_reference T_INSTEADOF name_list
738        { $$ = zend_ast_create(ZEND_AST_TRAIT_PRECEDENCE, $1, $3); }
739;
740
741trait_alias:
742        trait_method_reference T_AS T_STRING
743            { $$ = zend_ast_create_ex(ZEND_AST_TRAIT_ALIAS, 0, $1, $3); }
744    |   trait_method_reference T_AS reserved_non_modifiers
745            { zval zv; zend_lex_tstring(&zv); $$ = zend_ast_create_ex(ZEND_AST_TRAIT_ALIAS, 0, $1, zend_ast_create_zval(&zv)); }
746    |   trait_method_reference T_AS member_modifier identifier
747            { $$ = zend_ast_create_ex(ZEND_AST_TRAIT_ALIAS, $3, $1, $4); }
748    |   trait_method_reference T_AS member_modifier
749            { $$ = zend_ast_create_ex(ZEND_AST_TRAIT_ALIAS, $3, $1, NULL); }
750;
751
752trait_method_reference:
753        identifier
754            { $$ = zend_ast_create(ZEND_AST_METHOD_REFERENCE, NULL, $1); }
755    |   absolute_trait_method_reference { $$ = $1; }
756;
757
758absolute_trait_method_reference:
759    name T_PAAMAYIM_NEKUDOTAYIM identifier
760        { $$ = zend_ast_create(ZEND_AST_METHOD_REFERENCE, $1, $3); }
761;
762
763method_body:
764        ';' /* abstract method */       { $$ = NULL; }
765    |   '{' inner_statement_list '}'    { $$ = $2; }
766;
767
768variable_modifiers:
769        non_empty_member_modifiers      { $$ = $1; }
770    |   T_VAR                           { $$ = ZEND_ACC_PUBLIC; }
771;
772
773method_modifiers:
774        /* empty */                     { $$ = ZEND_ACC_PUBLIC; }
775    |   non_empty_member_modifiers
776            { $$ = $1; if (!($$ & ZEND_ACC_PPP_MASK)) { $$ |= ZEND_ACC_PUBLIC; } }
777;
778
779non_empty_member_modifiers:
780        member_modifier         { $$ = $1; }
781    |   non_empty_member_modifiers member_modifier
782            { $$ = zend_add_member_modifier($1, $2); }
783;
784
785member_modifier:
786        T_PUBLIC                { $$ = ZEND_ACC_PUBLIC; }
787    |   T_PROTECTED             { $$ = ZEND_ACC_PROTECTED; }
788    |   T_PRIVATE               { $$ = ZEND_ACC_PRIVATE; }
789    |   T_STATIC                { $$ = ZEND_ACC_STATIC; }
790    |   T_ABSTRACT              { $$ = ZEND_ACC_ABSTRACT; }
791    |   T_FINAL                 { $$ = ZEND_ACC_FINAL; }
792;
793
794property_list:
795        property_list ',' property { $$ = zend_ast_list_add($1, $3); }
796    |   property { $$ = zend_ast_create_list(1, ZEND_AST_PROP_DECL, $1); }
797;
798
799property:
800        T_VARIABLE { $$ = zend_ast_create(ZEND_AST_PROP_ELEM, $1, NULL); }
801    |   T_VARIABLE '=' expr
802            { $$ = zend_ast_create(ZEND_AST_PROP_ELEM, $1, $3); }
803;
804
805class_const_list:
806        class_const_list ',' class_const_decl { $$ = zend_ast_list_add($1, $3); }
807    |   class_const_decl { $$ = zend_ast_create_list(1, ZEND_AST_CLASS_CONST_DECL, $1); }
808;
809
810class_const_decl:
811    identifier '=' expr { $$ = zend_ast_create(ZEND_AST_CONST_ELEM, $1, $3); }
812;
813
814const_decl:
815    T_STRING '=' expr { $$ = zend_ast_create(ZEND_AST_CONST_ELEM, $1, $3); }
816;
817
818echo_expr_list:
819        echo_expr_list ',' echo_expr { $$ = zend_ast_list_add($1, $3); }
820    |   echo_expr { $$ = zend_ast_create_list(1, ZEND_AST_STMT_LIST, $1); }
821;
822echo_expr:
823    expr { $$ = zend_ast_create(ZEND_AST_ECHO, $1); }
824;
825
826for_exprs:
827        /* empty */         { $$ = NULL; }
828    |   non_empty_for_exprs { $$ = $1; }
829;
830
831non_empty_for_exprs:
832        non_empty_for_exprs ',' expr { $$ = zend_ast_list_add($1, $3); }
833    |   expr { $$ = zend_ast_create_list(1, ZEND_AST_EXPR_LIST, $1); }
834;
835
836anonymous_class:
837        T_CLASS { $<num>$ = CG(zend_lineno); } ctor_arguments
838        extends_from implements_list backup_doc_comment '{' class_statement_list '}' {
839            zend_ast *decl = zend_ast_create_decl(
840                ZEND_AST_CLASS, ZEND_ACC_ANON_CLASS, $<num>2, $6, NULL,
841                $4, $5, $8, NULL);
842            $$ = zend_ast_create(ZEND_AST_NEW, decl, $3);
843        }
844;
845
846new_expr:
847        T_NEW class_name_reference ctor_arguments
848            { $$ = zend_ast_create(ZEND_AST_NEW, $2, $3); }
849    |   T_NEW anonymous_class
850            { $$ = $2; }
851;
852
853expr_without_variable:
854        T_LIST '(' assignment_list ')' '=' expr
855            { $$ = zend_ast_create(ZEND_AST_ASSIGN, $3, $6); }
856    |   variable '=' expr
857            { $$ = zend_ast_create(ZEND_AST_ASSIGN, $1, $3); }
858    |   variable '=' '&' variable
859            { $$ = zend_ast_create(ZEND_AST_ASSIGN_REF, $1, $4); }
860    |   T_CLONE expr { $$ = zend_ast_create(ZEND_AST_CLONE, $2); }
861    |   variable T_PLUS_EQUAL expr
862            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_ADD, $1, $3); }
863    |   variable T_MINUS_EQUAL expr
864            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_SUB, $1, $3); }
865    |   variable T_MUL_EQUAL expr
866            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_MUL, $1, $3); }
867    |   variable T_POW_EQUAL expr
868            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_POW, $1, $3); }
869    |   variable T_DIV_EQUAL expr
870            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_DIV, $1, $3); }
871    |   variable T_CONCAT_EQUAL expr
872            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_CONCAT, $1, $3); }
873    |   variable T_MOD_EQUAL expr
874            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_MOD, $1, $3); }
875    |   variable T_AND_EQUAL expr
876            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_BW_AND, $1, $3); }
877    |   variable T_OR_EQUAL expr
878            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_BW_OR, $1, $3); }
879    |   variable T_XOR_EQUAL expr
880            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_BW_XOR, $1, $3); }
881    |   variable T_SL_EQUAL expr
882            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_SL, $1, $3); }
883    |   variable T_SR_EQUAL expr
884            { $$ = zend_ast_create_assign_op(ZEND_ASSIGN_SR, $1, $3); }
885    |   variable T_INC { $$ = zend_ast_create(ZEND_AST_POST_INC, $1); }
886    |   T_INC variable { $$ = zend_ast_create(ZEND_AST_PRE_INC, $2); }
887    |   variable T_DEC { $$ = zend_ast_create(ZEND_AST_POST_DEC, $1); }
888    |   T_DEC variable { $$ = zend_ast_create(ZEND_AST_PRE_DEC, $2); }
889    |   expr T_BOOLEAN_OR expr
890            { $$ = zend_ast_create(ZEND_AST_OR, $1, $3); }
891    |   expr T_BOOLEAN_AND expr
892            { $$ = zend_ast_create(ZEND_AST_AND, $1, $3); }
893    |   expr T_LOGICAL_OR expr
894            { $$ = zend_ast_create(ZEND_AST_OR, $1, $3); }
895    |   expr T_LOGICAL_AND expr
896            { $$ = zend_ast_create(ZEND_AST_AND, $1, $3); }
897    |   expr T_LOGICAL_XOR expr
898            { $$ = zend_ast_create_binary_op(ZEND_BOOL_XOR, $1, $3); }
899    |   expr '|' expr   { $$ = zend_ast_create_binary_op(ZEND_BW_OR, $1, $3); }
900    |   expr '&' expr   { $$ = zend_ast_create_binary_op(ZEND_BW_AND, $1, $3); }
901    |   expr '^' expr   { $$ = zend_ast_create_binary_op(ZEND_BW_XOR, $1, $3); }
902    |   expr '.' expr   { $$ = zend_ast_create_binary_op(ZEND_CONCAT, $1, $3); }
903    |   expr '+' expr   { $$ = zend_ast_create_binary_op(ZEND_ADD, $1, $3); }
904    |   expr '-' expr   { $$ = zend_ast_create_binary_op(ZEND_SUB, $1, $3); }
905    |   expr '*' expr   { $$ = zend_ast_create_binary_op(ZEND_MUL, $1, $3); }
906    |   expr T_POW expr { $$ = zend_ast_create_binary_op(ZEND_POW, $1, $3); }
907    |   expr '/' expr   { $$ = zend_ast_create_binary_op(ZEND_DIV, $1, $3); }
908    |   expr '%' expr   { $$ = zend_ast_create_binary_op(ZEND_MOD, $1, $3); }
909    |   expr T_SL expr  { $$ = zend_ast_create_binary_op(ZEND_SL, $1, $3); }
910    |   expr T_SR expr  { $$ = zend_ast_create_binary_op(ZEND_SR, $1, $3); }
911    |   '+' expr %prec T_INC { $$ = zend_ast_create(ZEND_AST_UNARY_PLUS, $2); }
912    |   '-' expr %prec T_INC { $$ = zend_ast_create(ZEND_AST_UNARY_MINUS, $2); }
913    |   '!' expr { $$ = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, $2); }
914    |   '~' expr { $$ = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BW_NOT, $2); }
915    |   expr T_IS_IDENTICAL expr
916            { $$ = zend_ast_create_binary_op(ZEND_IS_IDENTICAL, $1, $3); }
917    |   expr T_IS_NOT_IDENTICAL expr
918            { $$ = zend_ast_create_binary_op(ZEND_IS_NOT_IDENTICAL, $1, $3); }
919    |   expr T_IS_EQUAL expr
920            { $$ = zend_ast_create_binary_op(ZEND_IS_EQUAL, $1, $3); }
921    |   expr T_IS_NOT_EQUAL expr
922            { $$ = zend_ast_create_binary_op(ZEND_IS_NOT_EQUAL, $1, $3); }
923    |   expr '<' expr
924            { $$ = zend_ast_create_binary_op(ZEND_IS_SMALLER, $1, $3); }
925    |   expr T_IS_SMALLER_OR_EQUAL expr
926            { $$ = zend_ast_create_binary_op(ZEND_IS_SMALLER_OR_EQUAL, $1, $3); }
927    |   expr '>' expr
928            { $$ = zend_ast_create(ZEND_AST_GREATER, $1, $3); }
929    |   expr T_IS_GREATER_OR_EQUAL expr
930            { $$ = zend_ast_create(ZEND_AST_GREATER_EQUAL, $1, $3); }
931    |   expr T_SPACESHIP expr
932            { $$ = zend_ast_create_binary_op(ZEND_SPACESHIP, $1, $3); }
933    |   expr T_INSTANCEOF class_name_reference
934            { $$ = zend_ast_create(ZEND_AST_INSTANCEOF, $1, $3); }
935    |   '(' expr ')' { $$ = $2; }
936    |   new_expr { $$ = $1; }
937    |   expr '?' expr ':' expr
938            { $$ = zend_ast_create(ZEND_AST_CONDITIONAL, $1, $3, $5); }
939    |   expr '?' ':' expr
940            { $$ = zend_ast_create(ZEND_AST_CONDITIONAL, $1, NULL, $4); }
941    |   expr T_COALESCE expr
942            { $$ = zend_ast_create(ZEND_AST_COALESCE, $1, $3); }
943    |   internal_functions_in_yacc { $$ = $1; }
944    |   T_INT_CAST expr     { $$ = zend_ast_create_cast(IS_LONG, $2); }
945    |   T_DOUBLE_CAST expr  { $$ = zend_ast_create_cast(IS_DOUBLE, $2); }
946    |   T_STRING_CAST expr  { $$ = zend_ast_create_cast(IS_STRING, $2); }
947    |   T_ARRAY_CAST expr   { $$ = zend_ast_create_cast(IS_ARRAY, $2); }
948    |   T_OBJECT_CAST expr  { $$ = zend_ast_create_cast(IS_OBJECT, $2); }
949    |   T_BOOL_CAST expr    { $$ = zend_ast_create_cast(_IS_BOOL, $2); }
950    |   T_UNSET_CAST expr   { $$ = zend_ast_create_cast(IS_NULL, $2); }
951    |   T_EXIT exit_expr    { $$ = zend_ast_create(ZEND_AST_EXIT, $2); }
952    |   '@' expr            { $$ = zend_ast_create(ZEND_AST_SILENCE, $2); }
953    |   scalar { $$ = $1; }
954    |   '`' backticks_expr '`' { $$ = zend_ast_create(ZEND_AST_SHELL_EXEC, $2); }
955    |   T_PRINT expr { $$ = zend_ast_create(ZEND_AST_PRINT, $2); }
956    |   T_YIELD { $$ = zend_ast_create(ZEND_AST_YIELD, NULL, NULL); }
957    |   T_YIELD expr { $$ = zend_ast_create(ZEND_AST_YIELD, $2, NULL); }
958    |   T_YIELD expr T_DOUBLE_ARROW expr { $$ = zend_ast_create(ZEND_AST_YIELD, $4, $2); }
959    |   T_YIELD_FROM expr { $$ = zend_ast_create(ZEND_AST_YIELD_FROM, $2); }
960    |   function returns_ref '(' parameter_list ')' lexical_vars return_type
961        backup_doc_comment '{' inner_statement_list '}'
962            { $$ = zend_ast_create_decl(ZEND_AST_CLOSURE, $2, $1, $8,
963                  zend_string_init("{closure}", sizeof("{closure}") - 1, 0),
964                  $4, $6, $10, $7); }
965    |   T_STATIC function returns_ref '(' parameter_list ')' lexical_vars
966        return_type backup_doc_comment '{' inner_statement_list '}'
967            { $$ = zend_ast_create_decl(ZEND_AST_CLOSURE, $3 | ZEND_ACC_STATIC, $2, $9,
968                  zend_string_init("{closure}", sizeof("{closure}") - 1, 0),
969                  $5, $7, $11, $8); }
970;
971
972function:
973    T_FUNCTION { $$ = CG(zend_lineno); }
974;
975
976backup_doc_comment:
977    /* empty */ { $$ = CG(doc_comment); CG(doc_comment) = NULL; }
978;
979
980returns_ref:
981        /* empty */ { $$ = 0; }
982    |   '&'         { $$ = ZEND_ACC_RETURN_REFERENCE; }
983;
984
985lexical_vars:
986        /* empty */ { $$ = NULL; }
987    |   T_USE '(' lexical_var_list ')' { $$ = $3; }
988;
989
990lexical_var_list:
991        lexical_var_list ',' lexical_var { $$ = zend_ast_list_add($1, $3); }
992    |   lexical_var { $$ = zend_ast_create_list(1, ZEND_AST_CLOSURE_USES, $1); }
993;
994
995lexical_var:
996        T_VARIABLE      { $$ = $1; }
997    |   '&' T_VARIABLE  { $$ = $2; $$->attr = 1; }
998;
999
1000function_call:
1001        name argument_list
1002            { $$ = zend_ast_create(ZEND_AST_CALL, $1, $2); }
1003    |   class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
1004            { $$ = zend_ast_create(ZEND_AST_STATIC_CALL, $1, $3, $4); }
1005    |   variable_class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
1006            { $$ = zend_ast_create(ZEND_AST_STATIC_CALL, $1, $3, $4); }
1007    |   callable_expr argument_list
1008            { $$ = zend_ast_create(ZEND_AST_CALL, $1, $2); }
1009;
1010
1011class_name:
1012        T_STATIC
1013            { zval zv; ZVAL_STRINGL(&zv, "static", sizeof("static")-1);
1014              $$ = zend_ast_create_zval_ex(&zv, ZEND_NAME_NOT_FQ); }
1015    |   name { $$ = $1; }
1016;
1017
1018class_name_reference:
1019        class_name      { $$ = $1; }
1020    |   new_variable    { $$ = $1; }
1021;
1022
1023exit_expr:
1024        /* empty */             { $$ = NULL; }
1025    |   '(' optional_expr ')'   { $$ = $2; }
1026;
1027
1028backticks_expr:
1029        /* empty */
1030            { $$ = zend_ast_create_zval_from_str(ZSTR_EMPTY_ALLOC()); }
1031    |   T_ENCAPSED_AND_WHITESPACE { $$ = $1; }
1032    |   encaps_list { $$ = $1; }
1033;
1034
1035
1036ctor_arguments:
1037        /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST); }
1038    |   argument_list { $$ = $1; }
1039;
1040
1041
1042dereferencable_scalar:
1043        T_ARRAY '(' array_pair_list ')' { $$ = $3; }
1044    |   '[' array_pair_list ']'         { $$ = $2; }
1045    |   T_CONSTANT_ENCAPSED_STRING      { $$ = $1; }
1046;
1047
1048scalar:
1049        T_LNUMBER   { $$ = $1; }
1050    |   T_DNUMBER   { $$ = $1; }
1051    |   T_LINE      { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_LINE); }
1052    |   T_FILE      { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_FILE); }
1053    |   T_DIR       { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_DIR); }
1054    |   T_TRAIT_C   { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_TRAIT_C); }
1055    |   T_METHOD_C  { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_METHOD_C); }
1056    |   T_FUNC_C    { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_FUNC_C); }
1057    |   T_NS_C      { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_NS_C); }
1058    |   T_CLASS_C   { $$ = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_CLASS_C); }
1059    |   T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC { $$ = $2; }
1060    |   T_START_HEREDOC T_END_HEREDOC
1061            { $$ = zend_ast_create_zval_from_str(ZSTR_EMPTY_ALLOC()); }
1062    |   '"' encaps_list '"'     { $$ = $2; }
1063    |   T_START_HEREDOC encaps_list T_END_HEREDOC { $$ = $2; }
1064    |   dereferencable_scalar   { $$ = $1; }
1065    |   constant            { $$ = $1; }
1066;
1067
1068constant:
1069        name { $$ = zend_ast_create(ZEND_AST_CONST, $1); }
1070    |   class_name T_PAAMAYIM_NEKUDOTAYIM identifier
1071            { $$ = zend_ast_create(ZEND_AST_CLASS_CONST, $1, $3); }
1072    |   variable_class_name T_PAAMAYIM_NEKUDOTAYIM identifier
1073            { $$ = zend_ast_create(ZEND_AST_CLASS_CONST, $1, $3); }
1074;
1075
1076possible_comma:
1077        /* empty */
1078    |   ','
1079;
1080
1081expr:
1082        variable                    { $$ = $1; }
1083    |   expr_without_variable       { $$ = $1; }
1084;
1085
1086optional_expr:
1087        /* empty */ { $$ = NULL; }
1088    |   expr        { $$ = $1; }
1089;
1090
1091variable_class_name:
1092    dereferencable { $$ = $1; }
1093;
1094
1095dereferencable:
1096        variable                { $$ = $1; }
1097    |   '(' expr ')'            { $$ = $2; }
1098    |   dereferencable_scalar   { $$ = $1; }
1099;
1100
1101callable_expr:
1102        callable_variable       { $$ = $1; }
1103    |   '(' expr ')'            { $$ = $2; }
1104    |   dereferencable_scalar   { $$ = $1; }
1105;
1106
1107callable_variable:
1108        simple_variable
1109            { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1110    |   dereferencable '[' optional_expr ']'
1111            { $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
1112    |   constant '[' optional_expr ']'
1113            { $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
1114    |   dereferencable '{' expr '}'
1115            { $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
1116    |   dereferencable T_OBJECT_OPERATOR property_name argument_list
1117            { $$ = zend_ast_create(ZEND_AST_METHOD_CALL, $1, $3, $4); }
1118    |   function_call { $$ = $1; }
1119;
1120
1121variable:
1122        callable_variable
1123            { $$ = $1; }
1124    |   static_member
1125            { $$ = $1; }
1126    |   dereferencable T_OBJECT_OPERATOR property_name
1127            { $$ = zend_ast_create(ZEND_AST_PROP, $1, $3); }
1128;
1129
1130simple_variable:
1131        T_VARIABLE          { $$ = $1; }
1132    |   '$' '{' expr '}'    { $$ = $3; }
1133    |   '$' simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
1134;
1135
1136static_member:
1137        class_name T_PAAMAYIM_NEKUDOTAYIM simple_variable
1138            { $$ = zend_ast_create(ZEND_AST_STATIC_PROP, $1, $3); }
1139    |   variable_class_name T_PAAMAYIM_NEKUDOTAYIM simple_variable
1140            { $$ = zend_ast_create(ZEND_AST_STATIC_PROP, $1, $3); }
1141;
1142
1143new_variable:
1144        simple_variable
1145            { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1146    |   new_variable '[' optional_expr ']'
1147            { $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
1148    |   new_variable '{' expr '}'
1149            { $$ = zend_ast_create(ZEND_AST_DIM, $1, $3); }
1150    |   new_variable T_OBJECT_OPERATOR property_name
1151            { $$ = zend_ast_create(ZEND_AST_PROP, $1, $3); }
1152    |   class_name T_PAAMAYIM_NEKUDOTAYIM simple_variable
1153            { $$ = zend_ast_create(ZEND_AST_STATIC_PROP, $1, $3); }
1154    |   new_variable T_PAAMAYIM_NEKUDOTAYIM simple_variable
1155            { $$ = zend_ast_create(ZEND_AST_STATIC_PROP, $1, $3); }
1156;
1157
1158member_name:
1159        identifier { $$ = $1; }
1160    |   '{' expr '}'    { $$ = $2; }
1161    |   simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1162;
1163
1164property_name:
1165        T_STRING { $$ = $1; }
1166    |   '{' expr '}'    { $$ = $2; }
1167    |   simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1168;
1169
1170assignment_list:
1171        assignment_list ',' assignment_list_element
1172            { $$ = zend_ast_list_add($1, $3); }
1173    |   assignment_list_element
1174            { $$ = zend_ast_create_list(1, ZEND_AST_LIST, $1); }
1175;
1176
1177assignment_list_element:
1178        variable                        { $$ = $1; }
1179    |   T_LIST '(' assignment_list ')'  { $$ = $3; }
1180    |   /* empty */                     { $$ = NULL; }
1181;
1182
1183
1184array_pair_list:
1185        /* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_ARRAY); }
1186    |   non_empty_array_pair_list possible_comma { $$ = $1; }
1187;
1188
1189non_empty_array_pair_list:
1190        non_empty_array_pair_list ',' array_pair
1191            { $$ = zend_ast_list_add($1, $3); }
1192    |   array_pair
1193            { $$ = zend_ast_create_list(1, ZEND_AST_ARRAY, $1); }
1194;
1195
1196array_pair:
1197        expr T_DOUBLE_ARROW expr
1198            { $$ = zend_ast_create(ZEND_AST_ARRAY_ELEM, $3, $1); }
1199    |   expr { $$ = zend_ast_create(ZEND_AST_ARRAY_ELEM, $1, NULL); }
1200    |   expr T_DOUBLE_ARROW '&' variable
1201            { $$ = zend_ast_create_ex(ZEND_AST_ARRAY_ELEM, 1, $4, $1); }
1202    |   '&' variable
1203            { $$ = zend_ast_create_ex(ZEND_AST_ARRAY_ELEM, 1, $2, NULL); }
1204;
1205
1206encaps_list:
1207        encaps_list encaps_var
1208            { $$ = zend_ast_list_add($1, $2); }
1209    |   encaps_list T_ENCAPSED_AND_WHITESPACE
1210            { $$ = zend_ast_list_add($1, $2); }
1211    |   encaps_var
1212            { $$ = zend_ast_create_list(1, ZEND_AST_ENCAPS_LIST, $1); }
1213    |   T_ENCAPSED_AND_WHITESPACE encaps_var
1214            { $$ = zend_ast_create_list(2, ZEND_AST_ENCAPS_LIST, $1, $2); }
1215;
1216
1217encaps_var:
1218        T_VARIABLE
1219            { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1220    |   T_VARIABLE '[' encaps_var_offset ']'
1221            { $$ = zend_ast_create(ZEND_AST_DIM,
1222                  zend_ast_create(ZEND_AST_VAR, $1), $3); }
1223    |   T_VARIABLE T_OBJECT_OPERATOR T_STRING
1224            { $$ = zend_ast_create(ZEND_AST_PROP,
1225                  zend_ast_create(ZEND_AST_VAR, $1), $3); }
1226    |   T_DOLLAR_OPEN_CURLY_BRACES expr '}'
1227            { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
1228    |   T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}'
1229            { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
1230    |   T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
1231            { $$ = zend_ast_create(ZEND_AST_DIM,
1232                  zend_ast_create(ZEND_AST_VAR, $2), $4); }
1233    |   T_CURLY_OPEN variable '}' { $$ = $2; }
1234;
1235
1236encaps_var_offset:
1237        T_STRING        { $$ = $1; }
1238    |   T_NUM_STRING    { $$ = $1; }
1239    |   T_VARIABLE      { $$ = zend_ast_create(ZEND_AST_VAR, $1); }
1240;
1241
1242
1243internal_functions_in_yacc:
1244        T_ISSET '(' isset_variables ')' { $$ = $3; }
1245    |   T_EMPTY '(' expr ')' { $$ = zend_ast_create(ZEND_AST_EMPTY, $3); }
1246    |   T_INCLUDE expr
1247            { $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_INCLUDE, $2); }
1248    |   T_INCLUDE_ONCE expr
1249            { $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_INCLUDE_ONCE, $2); }
1250    |   T_EVAL '(' expr ')'
1251            { $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_EVAL, $3); }
1252    |   T_REQUIRE expr
1253            { $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_REQUIRE, $2); }
1254    |   T_REQUIRE_ONCE expr
1255            { $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_REQUIRE_ONCE, $2); }
1256;
1257
1258isset_variables:
1259        isset_variable { $$ = $1; }
1260    |   isset_variables ',' isset_variable
1261            { $$ = zend_ast_create(ZEND_AST_AND, $1, $3); }
1262;
1263
1264isset_variable:
1265        expr { $$ = zend_ast_create(ZEND_AST_ISSET, $1); }
1266;
1267
1268%%
1269
1270/* Copy to YYRES the contents of YYSTR after stripping away unnecessary
1271   quotes and backslashes, so that it's suitable for yyerror.  The
1272   heuristic is that double-quoting is unnecessary unless the string
1273   contains an apostrophe, a comma, or backslash (other than
1274   backslash-backslash).  YYSTR is taken from yytname.  If YYRES is
1275   null, do not copy; instead, return the length of what the result
1276   would have been.  */
1277static YYSIZE_T zend_yytnamerr(char *yyres, const char *yystr)
1278{
1279    if (!yyres) {
1280        return yystrlen(yystr);
1281    }
1282    {
1283        if (CG(parse_error) == 0) {
1284            char buffer[120];
1285            const unsigned char *end, *str, *tok1 = NULL, *tok2 = NULL;
1286            unsigned int len = 0, toklen = 0, yystr_len;
1287
1288            CG(parse_error) = 1;
1289
1290            if (LANG_SCNG(yy_text)[0] == 0 &&
1291                LANG_SCNG(yy_leng) == 1 &&
1292                memcmp(yystr, "\"end of file\"", sizeof("\"end of file\"") - 1) == 0) {
1293                yystpcpy(yyres, "end of file");
1294                return sizeof("end of file")-1;
1295            }
1296
1297            str = LANG_SCNG(yy_text);
1298            end = memchr(str, '\n', LANG_SCNG(yy_leng));
1299            yystr_len = (unsigned int)yystrlen(yystr);
1300
1301            if ((tok1 = memchr(yystr, '(', yystr_len)) != NULL
1302                && (tok2 = zend_memrchr(yystr, ')', yystr_len)) != NULL) {
1303                toklen = (tok2 - tok1) + 1;
1304            } else {
1305                tok1 = tok2 = NULL;
1306                toklen = 0;
1307            }
1308
1309            if (end == NULL) {
1310                len = LANG_SCNG(yy_leng) > 30 ? 30 : LANG_SCNG(yy_leng);
1311            } else {
1312                len = (end - str) > 30 ? 30 : (end - str);
1313            }
1314            if (toklen) {
1315                snprintf(buffer, sizeof(buffer), "'%.*s' %.*s", len, str, toklen, tok1);
1316            } else {
1317                snprintf(buffer, sizeof(buffer), "'%.*s'", len, str);
1318            }
1319            yystpcpy(yyres, buffer);
1320            return len + (toklen ? toklen + 1 : 0) + 2;
1321        }
1322    }
1323    if (*yystr == '"') {
1324        YYSIZE_T yyn = 0;
1325        const char *yyp = yystr;
1326
1327        for (; *++yyp != '"'; ++yyn) {
1328            yyres[yyn] = *yyp;
1329        }
1330        yyres[yyn] = '\0';
1331        return yyn;
1332    }
1333    yystpcpy(yyres, yystr);
1334    return strlen(yystr);
1335}
1336
1337/*
1338 * Local variables:
1339 * tab-width: 4
1340 * c-basic-offset: 4
1341 * indent-tabs-mode: t
1342 * End:
1343 */
1344