1 /*-----------------------------------------------------------------------
3 SDCC.y - parser definition file for sdcc :
4 Written By : Sandeep Dutta . sandeep.dutta@usa.net (1997)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
28 #include "SDCCglobl.h"
30 #include "SDCChasht.h"
34 extern int yyerror (char *);
36 extern char srcLstFname[];
37 int NestLevel = 0 ; /* current NestLevel */
38 int stackPtr = 1 ; /* stack pointer */
39 int xstackPtr = 0 ; /* xstack pointer */
41 int blockNo = 0 ; /* sequential block number */
46 char lbuff[1024]; /* local buffer */
48 /* break & continue stacks */
49 STACK_DCL(continueStack ,symbol *,MAX_NEST_LEVEL)
50 STACK_DCL(breakStack ,symbol *,MAX_NEST_LEVEL)
51 STACK_DCL(forStack ,symbol *,MAX_NEST_LEVEL)
52 STACK_DCL(swStk ,ast *,MAX_NEST_LEVEL)
53 STACK_DCL(blockNum,int,MAX_NEST_LEVEL*3)
55 value *cenum = NULL ; /* current enumeration type chain*/
59 symbol *sym ; /* symbol table pointer */
60 structdef *sdef; /* structure definition */
61 char yychar[SDCC_NAME_MAX+1];
62 link *lnk ; /* declarator or specifier */
63 int yyint; /* integer value returned */
64 value *val ; /* for integer constant */
65 initList *ilist; /* initial list */
66 char yyinline[MAX_INLINEASM]; /* inlined assembler code */
67 ast *asts; /* expression tree */
70 %token <yychar> IDENTIFIER TYPE_NAME
71 %token <val> CONSTANT STRING_LITERAL
73 %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
75 %token <yyint> MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
76 %token <yyint> SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
77 %token <yyint> XOR_ASSIGN OR_ASSIGN
78 %token TYPEDEF EXTERN STATIC AUTO REGISTER CODE INTERRUPT SFR AT SBIT
79 %token REENTRANT USING XDATA DATA IDATA PDATA VAR_ARGS CRITICAL
80 %token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID BIT
81 %token STRUCT UNION ENUM ELIPSIS RANGE FAR _XDATA _CODE _GENERIC _NEAR _PDATA _IDATA
82 %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
83 %token <yyinline> INLINEASM
84 %token IFX ADDRESS_OF GET_VALUE_AT_ADDRESS SPIL UNSPIL GETHBIT
85 %token BITWISEAND UNARYMINUS IPUSH IPOP PCALL ENDFUNCTION JUMPTABLE
87 %token CAST CALL PARAM NULLOP BLOCK LABEL RECEIVE SEND
89 %type <yyint> Interrupt_storage
90 %type <sym> identifier declarator declarator2 enumerator_list enumerator
91 %type <sym> struct_declarator
92 %type <sym> struct_declarator_list struct_declaration struct_declaration_list
93 %type <sym> declaration init_declarator_list init_declarator
94 %type <sym> declaration_list identifier_list parameter_identifier_list
95 %type <sym> declarator2_using_reentrant while do for
96 %type <lnk> pointer type_specifier_list type_specifier type_name
97 %type <lnk> storage_class_specifier struct_or_union_specifier
98 %type <lnk> declaration_specifiers sfr_reg_bit type_specifier2
99 %type <lnk> using_reentrant using_reentrant_interrupt enum_specifier
100 %type <lnk> abstract_declarator abstract_declarator2 far_near_pointer far_near
101 %type <val> parameter_type_list parameter_list parameter_declaration opt_assign_expr
102 %type <sdef> stag opt_stag
103 %type <asts> primary_expr
104 %type <asts> postfix_expr unary_expr cast_expr multiplicative_expr
105 %type <asts> additive_expr shift_expr relational_expr equality_expr
106 %type <asts> and_expr exclusive_or_expr inclusive_or_expr logical_or_expr
107 %type <asts> logical_and_expr conditional_expr assignment_expr constant_expr
108 %type <asts> expr argument_expr_list function_definition expr_opt
109 %type <asts> statement_list statement labeled_statement compound_statement
110 %type <asts> expression_statement selection_statement iteration_statement
111 %type <asts> jump_statement function_body else_statement string_literal
112 %type <ilist> initializer initializer_list
113 %type <yyint> unary_operator assignment_operator struct_or_union
120 : external_definition
121 | file external_definition
125 : function_definition { blockNo=0;}
128 allocVariables ($1) ;
129 cleanUpLevel (SymbolTab,1);
134 : declarator function_body { /* function type not specified */
135 /* assume it to be 'int' */
136 addDecl($1,0,newIntLink());
137 $$ = createFunction($1,$2);
139 | declaration_specifiers declarator function_body
141 pointerTypes($2->type,copyLinkChain($1));
143 $$ = createFunction($2,$3);
148 : using_reentrant_interrupt
149 | using_reentrant_interrupt using_reentrant { $$ = mergeSpec($1,$2); }
152 using_reentrant_interrupt
155 $$->class = SPECIFIER ;
157 SPEC_BANK($$) = (int) floatFromVal($2);
159 | REENTRANT { $$ = newLink ();
160 $$->class = SPECIFIER ;
163 | CRITICAL { $$ = newLink ();
164 $$->class = SPECIFIER ;
170 $$->class = SPECIFIER ;
178 | declaration_list compound_statement
180 werror(E_OLD_STYLE,($1 ? $1->name: "")) ;
186 : identifier { $$ = newAst(EX_VALUE,symbolVal($1)); }
187 | CONSTANT { $$ = newAst(EX_VALUE,$1); }
189 | '(' expr ')' { $$ = $2 ; }
193 : STRING_LITERAL { $$ = newAst(EX_VALUE,$1); }
198 | postfix_expr '[' expr ']' { $$ = newNode ('[', $1, $3) ; }
199 | postfix_expr '(' ')' { $$ = newNode (CALL,$1,NULL);
200 $$->left->funcName = 1;}
201 | postfix_expr '(' argument_expr_list ')'
203 $$ = newNode (CALL,$1,$3) ; $$->left->funcName = 1;
205 | postfix_expr '.' identifier
207 $3 = newSymbol($3->name,NestLevel);
209 $$ = newNode(PTR_OP,newNode('&',$1,NULL),newAst(EX_VALUE,symbolVal($3)));
210 /* $$ = newNode('.',$1,newAst(EX_VALUE,symbolVal($3))) ; */
212 | postfix_expr PTR_OP identifier
214 $3 = newSymbol($3->name,NestLevel);
216 $$ = newNode(PTR_OP,$1,newAst(EX_VALUE,symbolVal($3)));
218 | postfix_expr INC_OP
219 { $$ = newNode(INC_OP,$1,NULL);}
220 | postfix_expr DEC_OP
221 { $$ = newNode(DEC_OP,$1,NULL); }
226 | assignment_expr ',' argument_expr_list { $$ = newNode(PARAM,$1,$3); }
231 | INC_OP unary_expr { $$ = newNode(INC_OP,NULL,$2); }
232 | DEC_OP unary_expr { $$ = newNode(DEC_OP,NULL,$2); }
233 | unary_operator cast_expr { $$ = newNode($1,$2,NULL) ; }
234 | SIZEOF unary_expr { $$ = newNode(SIZEOF,NULL,$2); }
235 | SIZEOF '(' type_name ')' { $$ = newAst(EX_VALUE,sizeofOp($3)); }
249 | '(' type_name ')' cast_expr { $$ = newNode(CAST,newAst(EX_LINK,$2),$4); }
254 | multiplicative_expr '*' cast_expr { $$ = newNode('*',$1,$3);}
255 | multiplicative_expr '/' cast_expr { $$ = newNode('/',$1,$3);}
256 | multiplicative_expr '%' cast_expr { $$ = newNode('%',$1,$3);}
260 : multiplicative_expr
261 | additive_expr '+' multiplicative_expr { $$=newNode('+',$1,$3);}
262 | additive_expr '-' multiplicative_expr { $$=newNode('-',$1,$3);}
267 | shift_expr LEFT_OP additive_expr { $$ = newNode(LEFT_OP,$1,$3); }
268 | shift_expr RIGHT_OP additive_expr { $$ = newNode(RIGHT_OP,$1,$3); }
273 | relational_expr '<' shift_expr { $$ = newNode('<',$1,$3); }
274 | relational_expr '>' shift_expr { $$ = newNode('>',$1,$3); }
275 | relational_expr LE_OP shift_expr {
276 /* $$ = newNode(LE_OP,$1,$3); */
277 /* getting 8051 specific here : will change
278 LE_OP operation to "not greater than" i.e.
279 ( a <= b ) === ( ! ( a > b )) */
281 newNode('>', $1 , $3 ),
284 | relational_expr GE_OP shift_expr {
285 /* $$ = newNode(GE_OP,$1,$3) ; */
286 /* getting 8051 specific here : will change
287 GE_OP operation to "not less than" i.e.
288 ( a >= b ) === ( ! ( a < b )) */
290 newNode('<', $1 , $3 ),
297 | equality_expr EQ_OP relational_expr { $$ = newNode(EQ_OP,$1,$3);}
298 | equality_expr NE_OP relational_expr
300 /* $$ = newNode(NE_OP,$1,$3); */
302 expr1 != expr2 is equivalent to
303 (! expr1 == expr2) */
305 newNode(EQ_OP,$1,$3),
312 | and_expr '&' equality_expr { $$ = newNode('&',$1,$3);}
317 | exclusive_or_expr '^' and_expr { $$ = newNode('^',$1,$3);}
322 | inclusive_or_expr '|' exclusive_or_expr { $$ = newNode('|',$1,$3);}
327 | logical_and_expr AND_OP inclusive_or_expr
328 { $$ = newNode(AND_OP,$1,$3);}
333 | logical_or_expr OR_OP logical_and_expr
334 { $$ = newNode(OR_OP,$1,$3); }
339 | logical_or_expr '?' logical_or_expr ':' conditional_expr
341 $$ = newNode(':',$3,$5) ;
342 $$ = newNode('?',$1,$$) ;
348 | unary_expr assignment_operator assignment_expr
353 $$ = newNode($2,$1,$3);
356 $$ = newNode('=',$1,newNode('*',copyAst($1),$3));
359 $$ = newNode('=',$1,newNode('/',copyAst($1),$3));
362 $$ = newNode('=',$1,newNode('+',copyAst($1),$3));
365 $$ = newNode('=',$1,newNode('-',copyAst($1),$3));
368 $$ = newNode('=',$1,newNode(LEFT_OP,copyAst($1),$3));
371 $$ = newNode('=',$1,newNode(RIGHT_OP,copyAst($1),$3));
374 $$ = newNode('=',$1,newNode('&',copyAst($1),$3));
377 $$ = newNode('=',$1,newNode('^',copyAst($1),$3));
380 $$ = newNode('=',$1,newNode('|',copyAst($1),$3));
405 | expr ',' assignment_expr { $$ = newNode(',',$1,$3);}
413 : declaration_specifiers ';' { $$ = NULL ; }
414 | declaration_specifiers init_declarator_list ';'
416 /* add the specifier list to the id */
419 for (sym1 = sym = reverseSyms($2);sym != NULL;sym = sym->next) {
420 link *lnk = copyLinkChain($1);
421 /* do the pointer stuff */
422 pointerTypes(sym->type,lnk);
423 addDecl (sym,0,lnk) ;
430 declaration_specifiers
431 : storage_class_specifier { $$ = $1; }
432 | storage_class_specifier declaration_specifiers {
433 /* if the decl $2 is not a specifier */
434 /* find the spec and replace it */
437 while (lnk && !IS_SPEC(lnk->next))
439 lnk->next = mergeSpec($1,lnk->next);
443 $$ = mergeSpec($1,$2);
445 | type_specifier { $$ = $1; }
446 | type_specifier declaration_specifiers {
447 /* if the decl $2 is not a specifier */
448 /* find the spec and replace it */
451 while (lnk && !IS_SPEC(lnk->next))
453 lnk->next = mergeSpec($1,lnk->next);
457 $$ = mergeSpec($1,$2);
463 | init_declarator_list ',' init_declarator { $3->next = $1 ; $$ = $3;}
467 : declarator { $1->ival = NULL ; }
468 | declarator '=' initializer { $1->ival = $3 ; }
472 storage_class_specifier
475 $$->class = SPECIFIER ;
476 SPEC_TYPEDEF($$) = 1 ;
480 $$->class = SPECIFIER ;
485 $$->class = SPECIFIER ;
490 $$->class = SPECIFIER ;
491 SPEC_SCLS($$) = S_AUTO ;
495 $$->class = SPECIFIER ;
496 SPEC_SCLS($$) = S_REGISTER ;
501 : INTERRUPT CONSTANT { $$ = (int) floatFromVal($2) ; }
506 | type_specifier2 AT CONSTANT
508 /* add this to the storage class specifier */
509 SPEC_ABSA($1) = 1; /* set the absolute addr flag */
510 /* now get the abs addr from value */
511 SPEC_ADDR($1) = (int) floatFromVal ($3) ;
518 $$->class = SPECIFIER ;
519 SPEC_NOUN($$) = V_CHAR ;
523 $$->class = SPECIFIER ;
529 $$->class = SPECIFIER ;
530 SPEC_NOUN($$) = V_INT ;
534 $$->class = SPECIFIER ;
540 $$->class = SPECIFIER ;
545 $$->class = SPECIFIER ;
550 $$->class = SPECIFIER ;
551 SPEC_NOUN($$) = V_VOID ;
555 $$->class = SPECIFIER ;
556 SPEC_SCLS($$) = S_CONSTANT ;
561 $$->class = SPECIFIER ;
562 SPEC_VOLATILE($$) = 1 ;
566 SPEC_NOUN($$) = V_FLOAT;
567 $$->class = SPECIFIER ;
571 $$->class = SPECIFIER ;
572 SPEC_SCLS($$) = S_XDATA ;
576 $$->class = SPECIFIER ;
577 SPEC_SCLS($$) = S_CODE ;
581 $$->class = SPECIFIER ;
582 SPEC_SCLS($$) = S_DATA ;
586 $$->class = SPECIFIER ;
587 SPEC_SCLS($$) = S_IDATA ;
591 $$->class = SPECIFIER ;
592 SPEC_SCLS($$) = S_PDATA ;
596 $$->class = SPECIFIER ;
597 SPEC_NOUN($$) = V_BIT ;
598 SPEC_SCLS($$) = S_BIT ;
603 | struct_or_union_specifier
613 sym = findSym(TypedefTab,NULL,$1) ;
614 $$ = p = copyLinkChain(sym->type);
615 SPEC_TYPEDEF(getSpec(p)) = 0;
623 $$->class = SPECIFIER ;
624 SPEC_NOUN($$) = V_SBIT;
625 SPEC_SCLS($$) = S_SBIT;
629 $$->class = SPECIFIER ;
630 SPEC_NOUN($$) = V_CHAR;
631 SPEC_SCLS($$) = S_SFR ;
636 struct_or_union_specifier
637 : struct_or_union opt_stag '{' struct_declaration_list '}'
641 /* Create a structdef */
643 sdef->fields = reverseSyms($4) ; /* link the fields */
644 sdef->size = compStructSize($1,sdef); /* update size of */
646 /* Create the specifier */
648 $$->class = SPECIFIER ;
649 SPEC_NOUN($$) = V_STRUCT;
650 SPEC_STRUCT($$)= sdef ;
652 | struct_or_union stag
655 $$->class = SPECIFIER ;
656 SPEC_NOUN($$) = V_STRUCT;
657 SPEC_STRUCT($$) = $2 ;
662 : STRUCT { $$ = STRUCT ; }
663 | UNION { $$ = UNION ; }
668 | { /* synthesize a name add to structtable */
669 $$ = newStruct(genSymName(NestLevel)) ;
670 $$->level = NestLevel ;
671 addSym (StructTab, $$, $$->tag,$$->level,currBlockno) ;
676 : identifier { /* add name to structure table */
677 $$ = findSymWithBlock (StructTab,$1,currBlockno);
679 $$ = newStruct($1->name) ;
680 $$->level = NestLevel ;
681 addSym (StructTab, $$, $$->tag,$$->level,currBlockno) ;
686 struct_declaration_list
688 | struct_declaration_list struct_declaration
691 /* go to the end of the chain */
692 while (sym->next) sym = sym->next;
700 : type_specifier_list struct_declarator_list ';'
702 /* add this type to all the symbols */
704 for ( sym = $2 ; sym != NULL ; sym = sym->next ) {
706 pointerTypes(sym->type,copyLinkChain($1));
708 sym->type = copyLinkChain($1);
709 sym->etype = getSpec(sym->type);
712 addDecl (sym,0,cloneSpec($1));
719 struct_declarator_list
721 | struct_declarator_list ',' struct_declarator
730 | ':' constant_expr {
731 $$ = newSymbol (genSymName(NestLevel),NestLevel) ;
732 $$->bitVar = (int) floatFromVal(constExprValue($2,TRUE));
734 | declarator ':' constant_expr
736 $1->bitVar = (int) floatFromVal(constExprValue($3,TRUE));
741 : ENUM '{' enumerator_list '}' {
743 allocVariables(reverseSyms($3)) ;
744 $$ = copyLinkChain(cenum->type);
746 | ENUM identifier '{' enumerator_list '}' {
749 $2->type = copyLinkChain(cenum->type);
750 $2->etype = getSpec($2->type);
751 /* add this to the enumerator table */
752 if (!(csym=findSym(enumTab,$2,$2->name)) &&
753 (csym && csym->level == $2->level))
754 werror(E_DUPLICATE_TYPEDEF,csym->name);
756 addSym ( enumTab,$2,$2->name,$2->level,$2->block);
758 allocVariables (reverseSyms($4));
759 $$ = copyLinkChain(cenum->type);
760 SPEC_SCLS(getSpec($$)) = 0 ;
765 /* check the enumerator table */
766 if ((csym = findSym(enumTab,$2,$2->name)))
767 $$ = copyLinkChain(csym->type);
770 $$->class = SPECIFIER ;
771 SPEC_NOUN($$) = V_INT ;
774 SPEC_SCLS(getSpec($$)) = 0 ;
780 | enumerator_list ',' enumerator {
787 : identifier opt_assign_expr {
788 /* make the symbol one level up */
790 $1->type = copyLinkChain($2->type);
791 $1->etype= getSpec($1->type);
792 SPEC_ENUM($1->etype) = 1;
799 : '=' constant_expr {
802 val = constExprValue($2,TRUE);
807 sprintf(lbuff,"%d",(int) floatFromVal(cenum)+1);
808 $$ = cenum = constVal(lbuff);
811 sprintf(lbuff,"%d",0);
812 $$ = cenum = constVal(lbuff);
818 : declarator2_using_reentrant { $$ = $1; }
819 | pointer declarator2_using_reentrant
821 addDecl ($2,0,reverseLink($1));
826 declarator2_using_reentrant
827 : declarator2 { $$ = $1 ; }
828 | declarator2 using_reentrant { addDecl ($1,0,$2); }
833 | '(' declarator ')' { $$ = $2; }
834 | declarator2 '[' ']'
839 DCL_TYPE(p) = ARRAY ;
843 | declarator2 '[' constant_expr ']'
848 p = (tval = constExprValue($3,TRUE))->etype;
849 /* if it is not a constant then Error */
850 if ( SPEC_SCLS(p) != S_LITERAL)
851 werror(E_CONST_EXPECTED) ;
854 DCL_TYPE(p) = ARRAY ;
855 DCL_ELEM(p) = (int) floatFromVal(tval) ;
859 | declarator2 '(' ')' { addDecl ($1,FUNCTION,NULL) ; }
860 | declarator2 '(' { NestLevel++ ; currBlockno++; } parameter_type_list ')'
863 addDecl ($1,FUNCTION,NULL) ;
865 $1->hasVargs = IS_VARG($4);
866 $1->args = reverseVal($4) ;
868 /* nest level was incremented to take care of the parms */
873 | declarator2 '(' parameter_identifier_list ')'
875 werror(E_OLD_STYLE,$1->name) ;
877 /* assume it returns an it */
878 $1->type = $1->etype = newIntLink();
884 : far_near_pointer { $$ = $1 ;}
885 | far_near_pointer type_specifier_list
890 | far_near_pointer pointer
895 | far_near_pointer type_specifier_list pointer
898 if (IS_SPEC($2) && DCL_TYPE($3) == UPOINTER) {
899 DCL_PTR_CONST($1) = SPEC_CONST($2);
900 DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
901 switch (SPEC_SCLS($2)) {
903 DCL_TYPE($3) = FPOINTER;
906 DCL_TYPE($3) = IPOINTER ;
909 DCL_TYPE($3) = PPOINTER ;
912 DCL_TYPE($3) = POINTER ;
915 DCL_PTR_CONST($3) = 1;
916 DCL_TYPE($3) = CPOINTER ;
921 werror (W_PTR_TYPE_INVALID);
930 DCL_TYPE($$) = POINTER ;
938 : _XDATA { $$ = newLink() ; DCL_TYPE($$) = FPOINTER ; }
939 | _CODE { $$ = newLink() ; DCL_TYPE($$) = CPOINTER ; DCL_PTR_CONST($$) = 1;}
940 | _PDATA { $$ = newLink() ; DCL_TYPE($$) = PPOINTER ; }
941 | _IDATA { $$ = newLink() ; DCL_TYPE($$) = IPOINTER ; }
942 | _NEAR { $$ = NULL ; }
943 | _GENERIC { $$ = newLink() ; DCL_TYPE($$) = GPOINTER ; }
944 | { $$ = newLink() ; DCL_TYPE($$) = UPOINTER ; }
949 | type_specifier_list type_specifier { $$ = mergeSpec ($1,$2); }
952 parameter_identifier_list
954 | identifier_list ',' ELIPSIS
959 | identifier_list ',' identifier
968 | parameter_list ',' VAR_ARGS { $1->vArgs = 1;}
972 : parameter_declaration
973 | parameter_list ',' parameter_declaration
980 parameter_declaration
981 : type_specifier_list declarator
984 pointerTypes($2->type,$1);
986 for (loop=$2;loop;loop->_isparm=1,loop=loop->next);
993 $$->etype = getSpec($$->type);
998 : type_specifier_list { $$ = $1 ;}
999 | type_specifier_list abstract_declarator
1001 /* go to the end of the list */
1003 pointerTypes($2,$1);
1004 for ( p = $2 ; p->next ; p=p->next);
1011 : pointer { $$ = reverseLink($1); }
1012 | abstract_declarator2
1013 | pointer abstract_declarator2 { $1 = reverseLink($1); $1->next = $2 ; $$ = $1;}
1016 abstract_declarator2
1017 : '(' abstract_declarator ')' { $$ = $2 ; }
1020 DCL_TYPE($$) = ARRAY ;
1023 | '[' constant_expr ']' {
1026 DCL_TYPE($$) = ARRAY ;
1027 DCL_ELEM($$) = (int) floatFromVal(val = constExprValue($2,TRUE));
1029 | abstract_declarator2 '[' ']' {
1031 DCL_TYPE($$) = ARRAY ;
1035 | abstract_declarator2 '[' constant_expr ']'
1039 DCL_TYPE($$) = ARRAY ;
1040 DCL_ELEM($$) = (int) floatFromVal(val = constExprValue($3,TRUE));
1043 | '(' ')' { $$ = NULL;}
1044 | '(' parameter_type_list ')' { $$ = NULL;}
1045 | abstract_declarator2 '(' ')'
1046 | abstract_declarator2 '(' parameter_type_list ')'
1050 : assignment_expr { $$ = newiList(INIT_NODE,$1); }
1051 | '{' initializer_list '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
1052 | '{' initializer_list ',' '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
1057 | initializer_list ',' initializer { $3->next = $1; $$ = $3; }
1062 | compound_statement
1063 | expression_statement
1064 | selection_statement
1065 | iteration_statement
1068 ast *ex = newNode(INLINEASM,NULL,NULL);
1069 ALLOC_ATOMIC(ex->values.inlineasm,strlen($1));
1070 strcpy(ex->values.inlineasm,$1);
1076 : identifier ':' statement { $$ = createLabel($1,$3); }
1077 | CASE constant_expr ':' statement { $$ = createCase(STACK_PEEK(swStk),$2,$4); }
1078 | DEFAULT ':' statement { $$ = createDefault(STACK_PEEK(swStk),$3); }
1081 start_block : '{' { STACK_PUSH(blockNum,currBlockno); currBlockno = ++blockNo ; }
1084 end_block : '}' { currBlockno = STACK_POP(blockNum); }
1088 : start_block end_block { $$ = createBlock(NULL,NULL); }
1089 | start_block statement_list end_block { $$ = createBlock(NULL,$2) ; }
1091 declaration_list { addSymChain($2); }
1092 end_block { $$ = createBlock($2,NULL) ; }
1094 declaration_list { addSymChain ($2); }
1096 end_block {$$ = createBlock($2,$4) ; }
1097 | error ';' { $$ = NULL ; }
1103 /* if this is typedef declare it immediately */
1104 if ( $1 && IS_TYPEDEF($1->etype)) {
1105 allocVariables ($1);
1112 | declaration_list declaration
1116 /* if this is a typedef */
1117 if ($2 && IS_TYPEDEF($2->etype)) {
1118 allocVariables ($2);
1122 /* get to the end of the previous decl */
1137 | statement_list statement { $$ = newNode(NULLOP,$1,$2) ;}
1140 expression_statement
1146 : ELSE statement { $$ = $2 ; }
1152 : IF '(' expr ')' statement else_statement { noLineno++ ; $$ = createIf ($3, $5, $6 ); noLineno--;}
1153 | SWITCH '(' expr ')' {
1155 static int swLabel = 0 ;
1157 /* create a node for expression */
1158 ex = newNode(SWITCH,$3,NULL);
1159 STACK_PUSH(swStk,ex); /* save it in the stack */
1160 ex->values.switchVals.swNum = swLabel ;
1162 /* now create the label */
1163 sprintf(lbuff,"_swBrk_%d",swLabel++);
1164 $<sym>$ = newSymbol(lbuff,NestLevel);
1165 /* put label in the break stack */
1166 STACK_PUSH(breakStack,$<sym>$);
1169 /* get back the switch form the stack */
1170 $$ = STACK_POP(swStk) ;
1171 $$->right = newNode (NULLOP,$6,createLabel($<sym>5,NULL));
1172 STACK_POP(breakStack);
1176 while : WHILE { /* create and push the continue , break & body labels */
1177 static int Lblnum = 0 ;
1179 sprintf (lbuff,"_whilecontinue_%d",Lblnum);
1180 STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
1182 sprintf (lbuff,"_whilebreak_%d",Lblnum);
1183 STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
1185 sprintf (lbuff,"_whilebody_%d",Lblnum++);
1186 $$ = newSymbol(lbuff,NestLevel);
1189 do : DO { /* create and push the continue , break & body Labels */
1190 static int Lblnum = 0 ;
1193 sprintf(lbuff,"_docontinue_%d",Lblnum);
1194 STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
1196 sprintf (lbuff,"_dobreak_%d",Lblnum);
1197 STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
1199 sprintf (lbuff,"_dobody_%d",Lblnum++);
1200 $$ = newSymbol (lbuff,NestLevel);
1202 for : FOR { /* create & push continue, break & body labels */
1203 static int Lblnum = 0 ;
1206 sprintf (lbuff,"_forcontinue_%d",Lblnum);
1207 STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
1209 sprintf (lbuff,"_forbreak_%d",Lblnum);
1210 STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
1212 sprintf (lbuff,"_forbody_%d",Lblnum);
1213 $$ = newSymbol(lbuff,NestLevel);
1215 sprintf (lbuff,"_forcond_%d",Lblnum++);
1216 STACK_PUSH(forStack,newSymbol(lbuff,NestLevel));
1220 : while '(' expr ')' statement
1223 $$ = createWhile ( $1, STACK_POP(continueStack),
1224 STACK_POP(breakStack), $3, $5 );
1225 $$->lineno = $1->lineDef ;
1228 | do statement WHILE '(' expr ')' ';'
1231 $$ = createDo ( $1 , STACK_POP(continueStack),
1232 STACK_POP(breakStack), $5, $2);
1233 $$->lineno = $1->lineDef ;
1236 | for '(' expr_opt ';' expr_opt ';' expr_opt ')' statement
1240 /* if break or continue statement present
1241 then create a general case loop */
1242 if (STACK_PEEK(continueStack)->isref ||
1243 STACK_PEEK(breakStack)->isref) {
1244 $$ = createFor ($1, STACK_POP(continueStack),
1245 STACK_POP(breakStack) ,
1246 STACK_POP(forStack) ,
1249 $$ = newNode(FOR,$9,NULL);
1250 AST_FOR($$,trueLabel) = $1;
1251 AST_FOR($$,continueLabel) = STACK_POP(continueStack);
1252 AST_FOR($$,falseLabel) = STACK_POP(breakStack);
1253 AST_FOR($$,condLabel) = STACK_POP(forStack) ;
1254 AST_FOR($$,initExpr) = $3;
1255 AST_FOR($$,condExpr) = $5;
1256 AST_FOR($$,loopExpr) = $7;
1269 : GOTO identifier ';' {
1271 $$ = newAst(EX_VALUE,symbolVal($2));
1272 $$ = newNode(GOTO,$$,NULL);
1275 /* make sure continue is in context */
1276 if (STACK_PEEK(continueStack) == NULL) {
1277 werror(E_BREAK_CONTEXT);
1281 $$ = newAst(EX_VALUE,symbolVal(STACK_PEEK(continueStack)));
1282 $$ = newNode(GOTO,$$,NULL);
1283 /* mark the continue label as referenced */
1284 STACK_PEEK(continueStack)->isref = 1;
1288 if (STACK_PEEK(breakStack) == NULL) {
1289 werror(E_BREAK_CONTEXT);
1292 $$ = newAst(EX_VALUE,symbolVal(STACK_PEEK(breakStack)));
1293 $$ = newNode(GOTO,$$,NULL);
1294 STACK_PEEK(breakStack)->isref = 1;
1297 | RETURN ';' { $$ = newNode(RETURN,NULL,NULL) ; }
1298 | RETURN expr ';' { $$ = newNode(RETURN,NULL,$2) ; }
1302 : IDENTIFIER { $$ = newSymbol ($1,NestLevel) ; }
1306 extern unsigned char *yytext;
1308 extern char *filename;
1309 extern int fatalError;
1311 int yyerror(char *s)
1316 fprintf(stderr,"\n%s(%d) %s: token -> '%s' ; column %d\n",