home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / gcc-2.3.3-src.lha / GNU / src / amiga / gcc-2.3.3 / c-parse.y < prev    next >
GNU Bison Grammar  |  1994-02-06  |  53KB  |  1,794 lines

  1. /* YACC parser for C syntax and for Objective C.  -*-c-*-
  2.    Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC 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.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* This file defines the grammar of C and that of Objective C.
  21.    ifobjc ... end ifobjc  conditionals contain code for Objective C only.
  22.    ifc ... end ifc  conditionals contain code for C only.
  23.    The awk script cond.awk is used to convert this file into
  24.    c-parse.y and into objc-parse.y.  */
  25.  
  26. /* To whomever it may concern: I have heard that such a thing was once
  27. written by AT&T, but I have never seen it.  */
  28.  
  29. %expect 8
  30.  
  31. /* These are the 8 conflicts you should get in parse.output;
  32.    the state numbers may vary if minor changes in the grammar are made.
  33.  
  34. State 41 contains 1 shift/reduce conflict.  (Two ways to recover from error.)
  35. State 92 contains 1 shift/reduce conflict.  (Two ways to recover from error.)
  36. State 99 contains 1 shift/reduce conflict.  (Two ways to recover from error.)
  37. State 103 contains 1 shift/reduce conflict.  (Two ways to recover from error.)
  38. State 119 contains 1 shift/reduce conflict.  (See comment at component_decl.)
  39. State 183 contains 1 shift/reduce conflict.  (Two ways to recover from error.)
  40. State 193 contains 1 shift/reduce conflict.  (Two ways to recover from error.)
  41. State 199 contains 1 shift/reduce conflict.  (Two ways to recover from error.)
  42. */
  43.  
  44. %{
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <setjmp.h>
  48.  
  49. #include "config.h"
  50. #include "tree.h"
  51. #include "input.h"
  52. #include "c-lex.h"
  53. #include "c-tree.h"
  54. #include "flags.h"
  55.  
  56. #ifdef MULTIBYTE_CHARS
  57. #include <stdlib.h>
  58. #include <locale.h>
  59. #endif
  60.  
  61.  
  62. #ifndef errno
  63. extern int errno;
  64. #endif
  65.  
  66. void yyerror ();
  67.  
  68. /* Like YYERROR but do call yyerror.  */
  69. #define YYERROR1 { yyerror ("syntax error"); YYERROR; }
  70.  
  71. /* Cause the `yydebug' variable to be defined.  */
  72. #define YYDEBUG 1
  73. %}
  74.  
  75. %start program
  76.  
  77. %union {long itype; tree ttype; enum tree_code code;
  78.     char *filename; int lineno; }
  79.  
  80. /* All identifiers that are not reserved words
  81.    and are not declared typedefs in the current block */
  82. %token IDENTIFIER
  83.  
  84. /* All identifiers that are declared typedefs in the current block.
  85.    In some contexts, they are treated just like IDENTIFIER,
  86.    but they can also serve as typespecs in declarations.  */
  87. %token TYPENAME
  88.  
  89. /* Reserved words that specify storage class.
  90.    yylval contains an IDENTIFIER_NODE which indicates which one.  */
  91. %token SCSPEC
  92.  
  93. /* Reserved words that specify type.
  94.    yylval contains an IDENTIFIER_NODE which indicates which one.  */
  95. %token TYPESPEC
  96.  
  97. /* Reserved words that qualify type: "const" or "volatile".
  98.    yylval contains an IDENTIFIER_NODE which indicates which one.  */
  99. %token TYPE_QUAL
  100.  
  101. /* Character or numeric constants.
  102.    yylval is the node for the constant.  */
  103. %token CONSTANT
  104.  
  105. /* String constants in raw form.
  106.    yylval is a STRING_CST node.  */
  107. %token STRING
  108.  
  109. /* "...", used for functions with variable arglists.  */
  110. %token ELLIPSIS
  111.  
  112. /* the reserved words */
  113. /* SCO include files test "ASM", so use something else. */
  114. %token SIZEOF ENUM STRUCT UNION IF ELSE WHILE DO FOR SWITCH CASE DEFAULT
  115. %token BREAK CONTINUE RETURN GOTO ASM_KEYWORD TYPEOF ALIGNOF ALIGN
  116. %token ATTRIBUTE EXTENSION LABEL
  117.  
  118. /* Add precedence rules to solve dangling else s/r conflict */
  119. %nonassoc IF
  120. %nonassoc ELSE
  121.  
  122. /* Define the operator tokens and their precedences.
  123.    The value is an integer because, if used, it is the tree code
  124.    to use in the expression made from the operator.  */
  125.  
  126. %right <code> ASSIGN '='
  127. %right <code> '?' ':'
  128. %left <code> OROR
  129. %left <code> ANDAND
  130. %left <code> '|'
  131. %left <code> '^'
  132. %left <code> '&'
  133. %left <code> EQCOMPARE
  134. %left <code> ARITHCOMPARE
  135. %left <code> LSHIFT RSHIFT
  136. %left <code> '+' '-'
  137. %left <code> '*' '/' '%'
  138. %right <code> UNARY PLUSPLUS MINUSMINUS
  139. %left HYPERUNARY
  140. %left <code> POINTSAT '.' '(' '['
  141.  
  142. /* The Objective-C keywords.  These are included in C and in
  143.    Objective C, so that the token codes are the same in both.  */
  144. %token INTERFACE IMPLEMENTATION END SELECTOR DEFS ENCODE
  145. %token CLASSNAME PUBLIC
  146.  
  147.  
  148. %type <code> unop
  149.  
  150. %type <ttype> identifier IDENTIFIER TYPENAME CONSTANT expr nonnull_exprlist exprlist
  151. %type <ttype> expr_no_commas cast_expr unary_expr primary string STRING
  152. %type <ttype> typed_declspecs reserved_declspecs
  153. %type <ttype> typed_typespecs reserved_typespecquals
  154. %type <ttype> declmods typespec typespecqual_reserved
  155. %type <ttype> SCSPEC TYPESPEC TYPE_QUAL nonempty_type_quals maybe_type_qual
  156. %type <ttype> initdecls notype_initdecls initdcl notype_initdcl
  157. %type <ttype> init initlist maybeasm
  158. %type <ttype> asm_operands nonnull_asm_operands asm_operand asm_clobbers
  159. %type <ttype> maybe_attribute attribute_list attrib
  160.  
  161. %type <ttype> compstmt
  162.  
  163. %type <ttype> declarator
  164. %type <ttype> notype_declarator after_type_declarator
  165. %type <ttype> parm_declarator
  166.  
  167. %type <ttype> structsp component_decl_list component_decl_list2
  168. %type <ttype> component_decl components component_declarator
  169. %type <ttype> enumlist enumerator
  170. %type <ttype> typename absdcl absdcl1 type_quals
  171. %type <ttype> xexpr parms parm identifiers
  172.  
  173. %type <ttype> parmlist parmlist_1 parmlist_2
  174. %type <ttype> parmlist_or_identifiers parmlist_or_identifiers_1
  175. %type <ttype> identifiers_or_typenames
  176.  
  177. %type <itype> setspecs
  178.  
  179. %type <filename> save_filename
  180. %type <lineno> save_lineno
  181.  
  182.  
  183. %{
  184. /* Number of statements (loosely speaking) seen so far.  */
  185. static int stmt_count;
  186.  
  187. /* Input file and line number of the end of the body of last simple_if;
  188.    used by the stmt-rule immediately after simple_if returns.  */
  189. static char *if_stmt_file;
  190. static int if_stmt_line;
  191.  
  192. /* List of types and structure classes of the current declaration.  */
  193. static tree current_declspecs;
  194.  
  195. /* Stack of saved values of current_declspecs.  */
  196. static tree declspec_stack;
  197.  
  198. /* 1 if we explained undeclared var errors.  */
  199. static int undeclared_variable_notice;
  200.  
  201.  
  202. /* Tell yyparse how to print a token's value, if yydebug is set.  */
  203.  
  204. #define YYPRINT(FILE,YYCHAR,YYLVAL) yyprint(FILE,YYCHAR,YYLVAL)
  205. extern void yyprint ();
  206. %}
  207.  
  208. %%
  209. program: /* empty */
  210.         { if (pedantic)
  211.             pedwarn ("ANSI C forbids an empty source file");
  212.         }
  213.     | extdefs
  214.         {
  215.         }
  216.     ;
  217.  
  218. /* the reason for the strange actions in this rule
  219.  is so that notype_initdecls when reached via datadef
  220.  can find a valid list of type and sc specs in $0. */
  221.  
  222. extdefs:
  223.     {$<ttype>$ = NULL_TREE; } extdef
  224.     | extdefs {$<ttype>$ = NULL_TREE; } extdef
  225.     ;
  226.  
  227. extdef:
  228.     fndef
  229.     | datadef
  230.     | ASM_KEYWORD '(' expr ')' ';'
  231.         { STRIP_NOPS ($3);
  232.           if ((TREE_CODE ($3) == ADDR_EXPR
  233.                && TREE_CODE (TREE_OPERAND ($3, 0)) == STRING_CST)
  234.               || TREE_CODE ($3) == STRING_CST)
  235.             assemble_asm ($3);
  236.           else
  237.             error ("argument of `asm' is not a constant string"); }
  238.     ;
  239.  
  240. datadef:
  241.       setspecs notype_initdecls ';'
  242.         { if (pedantic)
  243.             error ("ANSI C forbids data definition with no type or storage class");
  244.           else if (!flag_traditional)
  245.             warning ("data definition has no type or storage class"); }
  246.         | declmods setspecs notype_initdecls ';'
  247.       {}
  248.     | typed_declspecs setspecs initdecls ';'
  249.       {}
  250.         | declmods ';'
  251.       { pedwarn ("empty declaration"); }
  252.     | typed_declspecs ';'
  253.       { shadow_tag ($1); }
  254.     | error ';'
  255.     | error '}'
  256.     | ';'
  257.         { if (pedantic)
  258.             pedwarn ("ANSI C does not allow extra `;' outside of a function"); }
  259.     ;
  260.  
  261. fndef:
  262.       typed_declspecs setspecs declarator
  263.         { if (! start_function ($1, $3, 0))
  264.             YYERROR1;
  265.           reinit_parse_for_function (); }
  266.       xdecls
  267.         { store_parm_decls (); }
  268.       compstmt_or_error
  269.         { finish_function (0); }
  270.     | typed_declspecs setspecs declarator error
  271.         { }
  272.     | declmods setspecs notype_dec