📄 c-exp.y
字号:
/* YACC parser for C expressions, for GDB. Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.This file is part of GDB.This program 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 of the License, or(at your option) any later version.This program 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 this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* Parse a C expression from text in a string, and return the result as a struct expression pointer. That structure contains arithmetic operations in reverse polish, with constants represented by operations that are followed by special data. See expression.h for the details of the format. What is important here is that it can be built up sequentially during the process of parsing; the lower levels of the tree always come first in the result. */ %{#include <stdio.h>#include <string.h>#include "defs.h"#include "symtab.h"#include "gdbtypes.h"#include "frame.h"#include "expression.h"#include "parser-defs.h"#include "value.h"#include "language.h"#include "bfd.h"#include "symfile.h"#include "objfiles.h"/* These MUST be included in any grammar file!!!! Please choose unique names! Note that this are a combined list of variables that can be produced by any one of bison, byacc, or yacc. */#define yymaxdepth c_maxdepth#define yyparse c_parse#define yylex c_lex#define yyerror c_error#define yylval c_lval#define yychar c_char#define yydebug c_debug#define yypact c_pact #define yyr1 c_r1 #define yyr2 c_r2 #define yydef c_def #define yychk c_chk #define yypgo c_pgo #define yyact c_act #define yyexca c_exca#define yyerrflag c_errflag#define yynerrs c_nerrs#define yyps c_ps#define yypv c_pv#define yys c_s#define yy_yys c_yys#define yystate c_state#define yytmp c_tmp#define yyv c_v#define yy_yyv c_yyv#define yyval c_val#define yylloc c_lloc#define yyss c_yyss /* byacc */#define yyssp c_yysp /* byacc */#define yyvs c_yyvs /* byacc */#define yyvsp c_yyvsp /* byacc */intyyparse PARAMS ((void));intyylex PARAMS ((void));voidyyerror PARAMS ((char *));/* #define YYDEBUG 1 */%}/* Although the yacc "value" of an expression is not used, since the result is stored in the structure being created, other node types do have values. */%union { LONGEST lval; unsigned LONGEST ulval; double dval; struct symbol *sym; struct type *tval; struct stoken sval; struct ttype tsym; struct symtoken ssym; int voidval; struct block *bval; enum exp_opcode opcode; struct internalvar *ivar; struct type **tvec; int *ivec; }%{/* YYSTYPE gets defined by %union */static intparse_number PARAMS ((char *, int, int, YYSTYPE *));%}%type <voidval> exp exp1 type_exp start variable qualified_name%type <tval> type typebase%type <tvec> nonempty_typelist/* %type <bval> block *//* Fancy type parsing. */%type <voidval> func_mod direct_abs_decl abs_decl%type <tval> ptype%type <lval> array_mod%token <lval> INT CHAR%token <ulval> UINT%token <dval> FLOAT/* Both NAME and TYPENAME tokens represent symbols in the input, and both convey their data as strings. But a TYPENAME is a string that happens to be defined as a typedef or builtin type name (such as int or char) and a NAME is any other symbol. Contexts where this distinction is not important can use the nonterminal "name", which matches either NAME or TYPENAME. */%token <sval> STRING%token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */%token <tsym> TYPENAME%type <sval> name%type <ssym> name_not_typename%type <tsym> typename/* A NAME_OR_INT is a symbol which is not known in the symbol table, but which would parse as a valid number in the current input radix. E.g. "c" when input_radix==16. Depending on the parse, it will be turned into a name or into a number. NAME_OR_UINT ditto. */%token <ssym> NAME_OR_INT NAME_OR_UINT%token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON%token TEMPLATE%token ERROR/* Special type cases, put in to allow the parser to distinguish different legal basetypes. */%token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD%token <lval> LAST REGNAME%token <ivar> VARIABLE%token <opcode> ASSIGN_MODIFY/* C++ */%token THIS%left ','%left ABOVE_COMMA%right '=' ASSIGN_MODIFY%right '?'%left OROR%left ANDAND%left '|'%left '^'%left '&'%left EQUAL NOTEQUAL%left '<' '>' LEQ GEQ%left LSH RSH%left '@'%left '+' '-'%left '*' '/' '%'%right UNARY INCREMENT DECREMENT%right ARROW '.' '[' '('%token <ssym> BLOCKNAME %type <bval> block%left COLONCOLON%{/* Ensure that if the generated parser contains any calls to malloc/realloc, that they get mapped to xmalloc/xrealloc. We have to do this here rather than earlier in the file because this is the first point after the place where the SVR4 yacc includes <malloc.h>, and if we do it before that, then the remapped declarations in <malloc.h> will collide with the ones in "defs.h". */#define malloc xmalloc#define realloc xrealloc%}%%start : exp1 | type_exp ;type_exp: type { write_exp_elt_opcode(OP_TYPE); write_exp_elt_type($1); write_exp_elt_opcode(OP_TYPE);} ;/* Expressions, including the comma operator. */exp1 : exp | exp1 ',' exp { write_exp_elt_opcode (BINOP_COMMA); } ;/* Expressions, not including the comma operator. */exp : '*' exp %prec UNARY { write_exp_elt_opcode (UNOP_IND); }exp : '&' exp %prec UNARY { write_exp_elt_opcode (UNOP_ADDR); }exp : '-' exp %prec UNARY { write_exp_elt_opcode (UNOP_NEG); } ;exp : '!' exp %prec UNARY { write_exp_elt_opcode (UNOP_ZEROP); } ;exp : '~' exp %prec UNARY { write_exp_elt_opcode (UNOP_LOGNOT); } ;exp : INCREMENT exp %prec UNARY { write_exp_elt_opcode (UNOP_PREINCREMENT); } ;exp : DECREMENT exp %prec UNARY { write_exp_elt_opcode (UNOP_PREDECREMENT); } ;exp : exp INCREMENT %prec UNARY { write_exp_elt_opcode (UNOP_POSTINCREMENT); } ;exp : exp DECREMENT %prec UNARY { write_exp_elt_opcode (UNOP_POSTDECREMENT); } ;exp : SIZEOF exp %prec UNARY { write_exp_elt_opcode (UNOP_SIZEOF); } ;exp : exp ARROW name { write_exp_elt_opcode (STRUCTOP_PTR); write_exp_string ($3); write_exp_elt_opcode (STRUCTOP_PTR); } ;exp : exp ARROW qualified_name { /* exp->type::name becomes exp->*(&type::name) */ /* Note: this doesn't work if name is a static member! FIXME */ write_exp_elt_opcode (UNOP_ADDR); write_exp_elt_opcode (STRUCTOP_MPTR); } ;exp : exp ARROW '*' exp { write_exp_elt_opcode (STRUCTOP_MPTR); } ;exp : exp '.' name { write_exp_elt_opcode (STRUCTOP_STRUCT); write_exp_string ($3); write_exp_elt_opcode (STRUCTOP_STRUCT); } ;exp : exp '.' qualified_name { /* exp.type::name becomes exp.*(&type::name) */ /* Note: this doesn't work if name is a static member! FIXME */ write_exp_elt_opcode (UNOP_ADDR); write_exp_elt_opcode (STRUCTOP_MEMBER); } ;exp : exp '.' '*' exp { write_exp_elt_opcode (STRUCTOP_MEMBER); } ;exp : exp '[' exp1 ']' { write_exp_elt_opcode (BINOP_SUBSCRIPT); } ;exp : exp '(' /* This is to save the value of arglist_len being accumulated by an outer function call. */ { start_arglist (); } arglist ')' %prec ARROW { write_exp_elt_opcode (OP_FUNCALL); write_exp_elt_longcst ((LONGEST) end_arglist ()); write_exp_elt_opcode (OP_FUNCALL); } ;arglist : ;arglist : exp { arglist_len = 1; } ;arglist : arglist ',' exp %prec ABOVE_COMMA { arglist_len++; } ;exp : '{' type '}' exp %prec UNARY { write_exp_elt_opcode (UNOP_MEMVAL); write_exp_elt_type ($2); write_exp_elt_opcode (UNOP_MEMVAL); } ;exp : '(' type ')' exp %prec UNARY { write_exp_elt_opcode (UNOP_CAST); write_exp_elt_type ($2); write_exp_elt_opcode (UNOP_CAST); } ;exp : '(' exp1 ')' { } ;/* Binary operators in order of decreasing precedence. */exp : exp '@' exp { write_exp_elt_opcode (BINOP_REPEAT); } ;exp : exp '*' exp { write_exp_elt_opcode (BINOP_MUL); } ;exp : exp '/' exp { write_exp_elt_opcode (BINOP_DIV); } ;exp : exp '%' exp { write_exp_elt_opcode (BINOP_REM); } ;exp : exp '+' exp { write_exp_elt_opcode (BINOP_ADD); } ;exp : exp '-' exp { write_exp_elt_opcode (BINOP_SUB); } ;exp : exp LSH exp { write_exp_elt_opcode (BINOP_LSH); } ;exp : exp RSH exp { write_exp_elt_opcode (BINOP_RSH); } ;exp : exp EQUAL exp { write_exp_elt_opcode (BINOP_EQUAL); } ;exp : exp NOTEQUAL exp { write_exp_elt_opcode (BINOP_NOTEQUAL); } ;exp : exp LEQ exp { write_exp_elt_opcode (BINOP_LEQ); } ;exp : exp GEQ exp { write_exp_elt_opcode (BINOP_GEQ); } ;exp : exp '<' exp { write_exp_elt_opcode (BINOP_LESS); } ;exp : exp '>' exp { write_exp_elt_opcode (BINOP_GTR); } ;exp : exp '&' exp { write_exp_elt_opcode (BINOP_LOGAND); } ;exp : exp '^' exp { write_exp_elt_opcode (BINOP_LOGXOR); } ;exp : exp '|' exp { write_exp_elt_opcode (BINOP_LOGIOR); } ;exp : exp ANDAND exp { write_exp_elt_opcode (BINOP_AND); } ;exp : exp OROR exp { write_exp_elt_opcode (BINOP_OR); } ;exp : exp '?' exp ':' exp %prec '?' { write_exp_elt_opcode (TERNOP_COND); } ; exp : exp '=' exp { write_exp_elt_opcode (BINOP_ASSIGN); } ;exp : exp ASSIGN_MODIFY exp { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); write_exp_elt_opcode ($2); write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); } ;exp : INT { write_exp_elt_opcode (OP_LONG); if ($1 == (int) $1 || $1 == (unsigned int) $1) write_exp_elt_type (builtin_type_int); else write_exp_elt_type (BUILTIN_TYPE_LONGEST); write_exp_elt_longcst ((LONGEST) $1); write_exp_elt_opcode (OP_LONG); } ;exp : NAME_OR_INT { YYSTYPE val; parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val); write_exp_elt_opcode (OP_LONG); if (val.lval == (int) val.lval || val.lval == (unsigned int) val.lval) write_exp_elt_type (builtin_type_int); else write_exp_elt_type (BUILTIN_TYPE_LONGEST); write_exp_elt_longcst (val.lval); write_exp_elt_opcode (OP_LONG); } ;exp : UINT { write_exp_elt_opcode (OP_LONG); if ($1 == (unsigned int) $1) write_exp_elt_type (builtin_type_unsigned_int); else write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST); write_exp_elt_longcst ((LONGEST) $1); write_exp_elt_opcode (OP_LONG); } ;exp : NAME_OR_UINT { YYSTYPE val; parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val); write_exp_elt_opcode (OP_LONG); if (val.ulval == (unsigned int) val.ulval) write_exp_elt_type (builtin_type_unsigned_int); else write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST); write_exp_elt_longcst ((LONGEST)val.ulval); write_exp_elt_opcode (OP_LONG); } ;exp : CHAR { write_exp_elt_opcode (OP_LONG); write_exp_elt_type (builtin_type_char); write_exp_elt_longcst ((LONGEST) $1); write_exp_elt_opcode (OP_LONG); } ;exp : FLOAT { write_exp_elt_opcode (OP_DOUBLE); write_exp_elt_type (builtin_type_double); write_exp_elt_dblcst ($1); write_exp_elt_opcode (OP_DOUBLE); } ;exp : variable ;exp : LAST { write_exp_elt_opcode (OP_LAST); write_exp_elt_longcst ((LONGEST) $1); write_exp_elt_opcode (OP_LAST); } ;exp : REGNAME { write_exp_elt_opcode (OP_REGISTER); write_exp_elt_longcst ((LONGEST) $1); write_exp_elt_opcode (OP_REGISTER); } ;exp : VARIABLE { write_exp_elt_opcode (OP_INTERNALVAR); write_exp_elt_intern ($1); write_exp_elt_opcode (OP_INTERNALVAR); } ;exp : SIZEOF '(' type ')' %prec UNARY { write_exp_elt_opcode (OP_LONG); write_exp_elt_type (builtin_type_int); write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3)); write_exp_elt_opcode (OP_LONG); } ;exp : STRING { write_exp_elt_opcode (OP_STRING); write_exp_string ($1); write_exp_elt_opcode (OP_STRING); } ;/* C++. */exp : THIS { write_exp_elt_opcode (OP_THIS); write_exp_elt_opcode (OP_THIS); } ;/* end of C++. */block : BLOCKNAME { if ($1.sym != 0) $$ = SYMBOL_BLOCK_VALUE ($1.sym); else { struct symtab *tem = lookup_symtab (copy_name ($1.stoken)); if (tem) $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK); else error ("No file or function \"%s\".", copy_name ($1.stoken)); } } ;block : block COLONCOLON name { struct symbol *tem = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE, 0, NULL); if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -