⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c-parse.y

📁 使用yacc和lex编写的cmm语言的词法分析和语法分析程序.
💻 Y
📖 第 1 页 / 共 4 页
字号:
/* YACC parser for C syntax.   Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.This file is part of GNU CC.GNU CC is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU CC is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING.  If not, write tothe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  *//* To whomever it may concern: I have heard that such a thing was oncewritten by AT&T, but I have never seen it.  */%expect 8/* These are the 8 conflicts you should get in parse.output;   the state numbers may vary if minor changes in the grammar are made.State 41 contains 1 shift/reduce conflict.  (Two ways to recover from error.)State 92 contains 1 shift/reduce conflict.  (Two ways to recover from error.)State 99 contains 1 shift/reduce conflict.  (Two ways to recover from error.)State 103 contains 1 shift/reduce conflict.  (Two ways to recover from error.)State 119 contains 1 shift/reduce conflict.  (See comment at component_decl.)State 183 contains 1 shift/reduce conflict.  (Two ways to recover from error.)State 193 contains 1 shift/reduce conflict.  (Two ways to recover from error.)State 199 contains 1 shift/reduce conflict.  (Two ways to recover from error.)*/%{#include <stdio.h>#include <errno.h>#include <setjmp.h>#include "config.h"#include "tree.h"#include "input.h"#include "c-lex.h"#include "c-tree.h"#include "flags.h"#ifdef MULTIBYTE_CHARS#include <stdlib.h>#include <locale.h>#endif#ifndef errnoextern int errno;#endifvoid yyerror ();/* Like YYERROR but do call yyerror.  */#define YYERROR1 { yyerror ("syntax error"); YYERROR; }/* Cause the `yydebug' variable to be defined.  */#define YYDEBUG 1%}%start program%union {long itype; tree ttype; enum tree_code code;	char *filename; int lineno; }/* All identifiers that are not reserved words   and are not declared typedefs in the current block */%token IDENTIFIER/* All identifiers that are declared typedefs in the current block.   In some contexts, they are treated just like IDENTIFIER,   but they can also serve as typespecs in declarations.  */%token TYPENAME/* Reserved words that specify storage class.   yylval contains an IDENTIFIER_NODE which indicates which one.  */%token SCSPEC/* Reserved words that specify type.   yylval contains an IDENTIFIER_NODE which indicates which one.  */%token TYPESPEC/* Reserved words that qualify type: "const" or "volatile".   yylval contains an IDENTIFIER_NODE which indicates which one.  */%token TYPE_QUAL/* Character or numeric constants.   yylval is the node for the constant.  */%token CONSTANT/* String constants in raw form.   yylval is a STRING_CST node.  */%token STRING/* "...", used for functions with variable arglists.  */%token ELLIPSIS/* the reserved words */%token SIZEOF ENUM STRUCT UNION IF ELSE WHILE DO FOR SWITCH CASE DEFAULT%token BREAK CONTINUE RETURN GOTO ASM TYPEOF ALIGNOF ALIGN%token ATTRIBUTE EXTENSION LABEL/* Add precedence rules to solve dangling else s/r conflict */%nonassoc IF%nonassoc ELSE/* Define the operator tokens and their precedences.   The value is an integer because, if used, it is the tree code   to use in the expression made from the operator.  */%right <code> ASSIGN '='%right <code> '?' ':'%left <code> OROR%left <code> ANDAND%left <code> '|'%left <code> '^'%left <code> '&'%left <code> EQCOMPARE%left <code> ARITHCOMPARE%left <code> LSHIFT RSHIFT%left <code> '+' '-'%left <code> '*' '/' '%'%right <code> UNARY PLUSPLUS MINUSMINUS%left HYPERUNARY%left <code> POINTSAT '.' '(' '['/* The Objective-C keywords, here so our token codes match obj's.  */%token INTERFACE IMPLEMENTATION END SELECTOR DEFS ENCODE%token CLASSNAME PUBLIC%type <code> unop%type <ttype> identifier IDENTIFIER TYPENAME CONSTANT expr nonnull_exprlist exprlist%type <ttype> expr_no_commas cast_expr unary_expr primary string STRING%type <ttype> typed_declspecs reserved_declspecs%type <ttype> typed_typespecs reserved_typespecquals%type <ttype> declmods typespec typespecqual_reserved%type <ttype> SCSPEC TYPESPEC TYPE_QUAL nonempty_type_quals maybe_type_qual%type <ttype> initdecls notype_initdecls initdcl notype_initdcl%type <ttype> init initlist maybeasm%type <ttype> asm_operands nonnull_asm_operands asm_operand asm_clobbers%type <ttype> maybe_attribute attribute_list attrib%type <ttype> compstmt%type <ttype> declarator%type <ttype> notype_declarator after_type_declarator%type <ttype> parm_declarator%type <ttype> structsp component_decl_list component_decl_list2%type <ttype> component_decl components component_declarator%type <ttype> enumlist enumerator%type <ttype> typename absdcl absdcl1 type_quals%type <ttype> xexpr parms parm identifiers%type <ttype> parmlist parmlist_1 parmlist_2%type <ttype> parmlist_or_identifiers parmlist_or_identifiers_1%type <ttype> identifiers_or_typenames%type <itype> setspecs%type <filename> save_filename%type <lineno> save_lineno%{/* Number of statements (loosely speaking) seen so far.  */static int stmt_count;/* Input file and line number of the end of the body of last simple_if;   used by the stmt-rule immediately after simple_if returns.  */static char *if_stmt_file;static int if_stmt_line;/* List of types and structure classes of the current declaration.  */static tree current_declspecs;/* Stack of saved values of current_declspecs.  */static tree declspec_stack;/* 1 if we explained undeclared var errors.  */static int undeclared_variable_notice;/* Tell yyparse how to print a token's value, if yydebug is set.  */#define YYPRINT(FILE,YYCHAR,YYLVAL) yyprint(FILE,YYCHAR,YYLVAL)extern void yyprint ();%}%%program: /* empty */		{ if (pedantic)		    pedwarn ("ANSI C forbids an empty source file"); }	| extdefs	;/* the reason for the strange actions in this rule is so that notype_initdecls when reached via datadef can find a valid list of type and sc specs in $0. */extdefs:	{$<ttype>$ = NULL_TREE; } extdef	| extdefs {$<ttype>$ = NULL_TREE; } extdef	;extdef:	fndef	| datadef	| ASM '(' expr ')' ';'		{ STRIP_NOPS ($3);		  if ((TREE_CODE ($3) == ADDR_EXPR		       && TREE_CODE (TREE_OPERAND ($3, 0)) == STRING_CST)		      || TREE_CODE ($3) == STRING_CST)		    assemble_asm ($3);		  else		    error ("argument of `asm' is not a constant string"); }	;datadef:	  setspecs notype_initdecls ';'		{ if (pedantic)		    error ("ANSI C forbids data definition with no type or storage class");		  else if (!flag_traditional)		    warning ("data definition has no type or storage class"); }        | declmods setspecs notype_initdecls ';'	  {}	| typed_declspecs setspecs initdecls ';'	  {}        | declmods ';'	  { error ("empty declaration"); }	| typed_declspecs ';'	  { shadow_tag ($1); }	| error ';'	| error '}'	| ';'		{ if (pedantic)		    pedwarn ("ANSI C does not allow extra `;' outside of a function"); }	;fndef:	  typed_declspecs setspecs declarator		{ if (! start_function ($1, $3, 0))		    YYERROR1;		  reinit_parse_for_function (); }	  xdecls		{ store_parm_decls (); }	  compstmt_or_error		{ finish_function (0); }	| typed_declspecs setspecs declarator error		{ }	| declmods setspecs notype_declarator		{ if (! start_function ($1, $3, 0))		    YYERROR1;		  reinit_parse_for_function (); }	  xdecls		{ store_parm_decls (); }	  compstmt_or_error		{ finish_function (0); }	| declmods setspecs notype_declarator error		{ }	| setspecs notype_declarator		{ if (! start_function (0, $2, 0))		    YYERROR1;		  reinit_parse_for_function (); }	  xdecls		{ store_parm_decls (); }	  compstmt_or_error		{ finish_function (0); }	| setspecs notype_declarator error		{ }	;identifier:	IDENTIFIER	| TYPENAME	;unop:     '&'		{ $$ = ADDR_EXPR; }	| '-'		{ $$ = NEGATE_EXPR; }	| '+'		{ $$ = CONVERT_EXPR; }	| PLUSPLUS		{ $$ = PREINCREMENT_EXPR; }	| MINUSMINUS		{ $$ = PREDECREMENT_EXPR; }	| '~'		{ $$ = BIT_NOT_EXPR; }	| '!'		{ $$ = TRUTH_NOT_EXPR; }	;expr:	nonnull_exprlist		{ $$ = build_compound_expr ($1); }	;exprlist:	  /* empty */		{ $$ = NULL_TREE; }	| nonnull_exprlist	;nonnull_exprlist:	expr_no_commas		{ $$ = build_tree_list (NULL_TREE, $1); }	| nonnull_exprlist ',' expr_no_commas		{ chainon ($1, build_tree_list (NULL_TREE, $3)); }	;unary_expr:	primary	| '*' cast_expr   %prec UNARY		{ $$ = build_indirect_ref ($2, "unary *"); }	/* __extension__ turns off -pedantic for following primary.  */	| EXTENSION		{ $<itype>1 = pedantic;		  pedantic = 0; }	  cast_expr	  %prec UNARY		{ $$ = $3;		  pedantic = $<itype>1; }	| unop cast_expr  %prec UNARY		{ $$ = build_unary_op ($1, $2, 0); }	/* Refer to the address of a label as a pointer.  */	| ANDAND identifier		{ tree label = lookup_label ($2);		  TREE_USED (label) = 1;		  $$ = build1 (ADDR_EXPR, ptr_type_node, label);		  TREE_CONSTANT ($$) = 1; }/* This seems to be impossible on some machines, so let's turn it off.	| '&' ELLIPSIS		{ tree types = TYPE_ARG_TYPES (TREE_TYPE (current_function_decl));		  $$ = error_mark_node;		  if (TREE_VALUE (tree_last (types)) == void_type_node)		    error ("`&...' used in function with fixed number of arguments");		  else		    {		      if (pedantic)			pedwarn ("ANSI C forbids `&...'");		      $$ = tree_last (DECL_ARGUMENTS (current_function_decl));		      $$ = build_unary_op (ADDR_EXPR, $$, 0);		    } }*/	| SIZEOF unary_expr  %prec UNARY		{ if (TREE_CODE ($2) == COMPONENT_REF		      && DECL_BIT_FIELD (TREE_OPERAND ($2, 1)))		    error ("`sizeof' applied to a bit-field");		  $$ = c_sizeof (TREE_TYPE ($2)); }	| SIZEOF '(' typename ')'  %prec HYPERUNARY		{ $$ = c_sizeof (groktypename ($3)); }	| ALIGNOF unary_expr  %prec UNARY		{ $$ = c_alignof_expr ($2); }	| ALIGNOF '(' typename ')'  %prec HYPERUNARY		{ $$ = c_alignof (groktypename ($3)); }	;cast_expr:	unary_expr	| '(' typename ')' cast_expr  %prec UNARY		{ tree type = groktypename ($2);		  $$ = build_c_cast (type, $4); }	| '(' typename ')' '{' initlist maybecomma '}'  %prec UNARY		{ tree type = groktypename ($2);		  char *name;		  if (pedantic)		    pedwarn ("ANSI C forbids constructor expressions");		  if (TYPE_NAME (type) != 0)		    {		      if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)			name = IDENTIFIER_POINTER (TYPE_NAME (type));		      else			name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));		    }		  else		    name = "";		  $$ = digest_init (type, build_nt (CONSTRUCTOR, NULL_TREE, nreverse ($5)),				    0, 0, 0, name);		  if (TREE_CODE (type) == ARRAY_TYPE && TYPE_SIZE (type) == 0)		    {		      int failure = complete_array_type (type, $$, 1);		      if (failure)			abort ();		    }		}	;expr_no_commas:	  cast_expr	| expr_no_commas '+' expr_no_commas		{ $$ = parser_build_binary_op ($2, $1, $3); }	| expr_no_commas '-' expr_no_commas		{ $$ = parser_build_binary_op ($2, $1, $3); }	| expr_no_commas '*' expr_no_commas		{ $$ = parser_build_binary_op ($2, $1, $3); }	| expr_no_commas '/' expr_no_commas		{ $$ = parser_build_binary_op ($2, $1, $3); }	| expr_no_commas '%' expr_no_commas		{ $$ = parser_build_binary_op ($2, $1, $3); }	| expr_no_commas LSHIFT expr_no_commas		{ $$ = parser_build_binary_op ($2, $1, $3); }	| expr_no_commas RSHIFT expr_no_commas		{ $$ = parser_build_binary_op ($2, $1, $3); }	| expr_no_commas ARITHCOMPARE expr_no_commas		{ $$ = parser_build_binary_op ($2, $1, $3); }	| expr_no_commas EQCOMPARE expr_no_commas		{ $$ = parser_build_binary_op ($2, $1, $3); }	| expr_no_commas '&' expr_no_commas

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -