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

📄 ytab.c

📁 一个小型的c语言编译器,做的非常好,不过不是我做的.
💻 C
📖 第 1 页 / 共 5 页
字号:
case 10:
# line 322 "c.y"
{
				symbol	*decl_lst;

				PARSE_INFO("declaration :declaration_specifiers init_declarator_list oSEMI")
				// at same time check declarator , not be void
				unoin_specifier_to_declarator_list(yypvt[-2].p_symbol, yypvt[-1].p_symbol);
				del_symbol( yypvt[-2].p_symbol );

				// yypvt[-1].p_symbol is a list
				// so add all function in the list to function symtab
				decl_lst = yypvt[-1].p_symbol;
				while ( decl_lst )
				{
					if (IS_FUNCTION(decl_lst))
					{
						// is function declaration
						// so add it to function declaration symtab

						// set decl flag
						decl_lst->is_declaration = 1;

						// only declaration
						// add or only check parameter type 
						// not overwrite parameters type
						add_function_decl_to_functab(decl_lst);
					}
					else if ( IS_LOCAL_VAR(decl_lst) )
					{
						check_var_declarator(decl_lst);
					}

					decl_lst = decl_lst->next;
				}
				add_symbol_list_to_current_symtab(yypvt[-1].p_symbol);
				yyval.p_symbol = yypvt[-1].p_symbol;
			} break;
case 11:
# line 359 "c.y"
{
				PARSE_INFO("declaration :declaration_specifiers oSEMI")
				// empty declaration
				// so delete it
				del_symbol( yypvt[-1].p_symbol );
				yyerror("waring : ignored on left of specifier when no variable is declared");
				yyval.p_symbol = NULL;
			} break;
case 12:
# line 371 "c.y"
{
				PARSE_INFO("declaration_list :declaration")
			} break;
case 13:
# line 375 "c.y"
{
				PARSE_INFO("declaration_list :declaration_list declaration")
				link_symbol_list( yypvt[-1].p_symbol, yypvt[-0].p_symbol );
			} break;
case 14:
# line 383 "c.y"
{
				PARSE_INFO("declaration_specifiers :storage_class_specifier declaration_specifiers")
				union_specifier_symbol( yypvt[-1].p_symbol, yypvt[-0].p_symbol );
				del_symbol( yypvt[-0].p_symbol );
			} break;
case 15:
# line 389 "c.y"
{
				PARSE_INFO("declaration_specifiers :storage_class_specifier")
			} break;
case 16:
# line 393 "c.y"
{
				PARSE_INFO("declaration_specifiers :type_specifier declaration_specifiers")
				union_specifier_symbol( yypvt[-1].p_symbol, yypvt[-0].p_symbol );
				del_symbol( yypvt[-0].p_symbol );
			} break;
case 17:
# line 399 "c.y"
{
				PARSE_INFO("declaration_specifiers :type_specifier")
			} break;
case 18:
# line 403 "c.y"
{
				PARSE_INFO("declaration_specifiers :type_qualifier declaration_specifiers")
				union_specifier_symbol( yypvt[-1].p_symbol, yypvt[-0].p_symbol );
				del_symbol( yypvt[-0].p_symbol );
			} break;
case 19:
# line 409 "c.y"
{
				PARSE_INFO("declaration_specifiers :type_qualifier")
			} break;
case 20:
# line 416 "c.y"
{
				PARSE_INFO("storage_class_specifier :kAUTO")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->SCLASS = SPEC_AUTO;
			} break;
case 21:
# line 422 "c.y"
{
				PARSE_INFO("storage_class_specifier :kREGISTER")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->SCLASS = SPEC_REGISTER;
			} break;
case 22:
# line 428 "c.y"
{
				PARSE_INFO("storage_class_specifier :kSTATIC")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->IS_STATIC = 1;
			} break;
case 23:
# line 434 "c.y"
{
				PARSE_INFO("storage_class_specifier :kTYPEDEF")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->SCLASS = SPEC_TYPEDEF;
			} break;
case 24:
# line 440 "c.y"
{
				PARSE_INFO("storage_class_specifier :kEXTERN")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->IS_EXTERN = 1;
			} break;
case 25:
# line 449 "c.y"
{
				PARSE_INFO("type_specifier :kINT")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->NOUN = SPEC_INT;
			} break;
case 26:
# line 455 "c.y"
{
				PARSE_INFO("type_specifier :kLONG")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->NOUN = SPEC_INT;
				yyval.p_symbol->IS_LONG = 1;
			} break;
case 27:
# line 462 "c.y"
{
				PARSE_INFO("type_specifier :kSHORT")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->NOUN = SPEC_INT;
			} break;
case 28:
# line 468 "c.y"
{
				PARSE_INFO("type_specifier :kCHAR")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->NOUN = SPEC_CHAR;
			} break;
case 29:
# line 474 "c.y"
{
				PARSE_INFO("type_specifier :kVOID")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->NOUN = SPEC_VOID;
			} break;
case 30:
# line 480 "c.y"
{
				PARSE_INFO("type_specifier :kFLOAT")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->NOUN = SPEC_FLOAT;
			} break;
case 31:
# line 486 "c.y"
{
				PARSE_INFO("type_specifier :kDOUBLE")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->NOUN = SPEC_DOUBLE;
			} break;
case 32:
# line 492 "c.y"
{
				PARSE_INFO("type_specifier :kSIGNED")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->NOUN = SPEC_INT;
			} break;
case 33:
# line 498 "c.y"
{
				PARSE_INFO("type_specifier :kUNSIGNED")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->NOUN = SPEC_INT;
				yyval.p_symbol->IS_UNSIGNED = 1;
			} break;
case 34:
# line 505 "c.y"
{
				symbol	*tmp;

				PARSE_INFO("type_specifier :idTYPEDEF")
				tmp = find_symtab_typedef( yypvt[-0].p_char );
				assert( tmp );
				yyval.p_symbol = new_symbol_from_typedef( tmp );
				assert( yyval.p_symbol );
			} break;
case 35:
# line 518 "c.y"
{
				PARSE_INFO("type_qualifier :kCONST")
				yyval.p_symbol = new_symbol();
				yyval.p_symbol->SCLASS = SPEC_CONSTANT;
			} break;
case 36:
# line 524 "c.y"
{
				PARSE_INFO("type_qualifier :kVOLATILE")
				yyval.p_symbol = new_symbol();
				// ingore volatile attribute
			} break;
case 37:
# line 533 "c.y"
{
				PARSE_INFO("init_declarator_list :init_declarator")
			} break;
case 38:
# line 537 "c.y"
{
				PARSE_INFO("init_declarator_list :init_declarator_list oCOMMA init_declarator")
				link_symbol_list(yypvt[-2].p_symbol, yypvt[-0].p_symbol);
			} break;
case 39:
# line 545 "c.y"
{
				PARSE_INFO("init_declarator :declarator")
				// check array element
				// for globe or local var, the array element number could not be zero
				if ( IS_ARRAY(yypvt[-0].p_symbol) && !yypvt[-0].p_symbol->num_ele )
				{
					yyerror("Array must be declarated with element number");
					user_exit(1);
				}
			} break;
case 40:
# line 556 "c.y"
{
				PARSE_INFO("init_declarator :declarator oASSIGN initializer")
				if ( IS_FUNCTION(yypvt[-2].p_symbol) )
				{
					yyerror("initialization of a function");
					user_exit(1);
				}
				if ( !IS_CL(yypvt[-0].p_symbol) )
				{
					yyerror("initializer is not a constant");
					user_exit(1);
				}
				if ( IS_ARRAY(yypvt[-2].p_symbol) )
				{
					yyerror("array initialization unsupport");
					user_exit(1);
				}
				yypvt[-2].p_symbol->args = yypvt[-0].p_symbol;
				// set assign flag
				yypvt[-2].p_symbol->is_assign = 1;
			} break;
case 41:
# line 610 "c.y"
{
				PARSE_INFO("declarator :direct_declarator")
			} break;
case 42:
# line 617 "c.y"
{
				PARSE_INFO("direct_declarator :identifier")
				yyval.p_symbol = new_symbol();
				strncpy( yyval.p_symbol->name, yypvt[-0].p_char, NAME_LEN );
			} break;
case 43:
# line 629 "c.y"
{
				PARSE_INFO("direct_declarator :direct_declarator oLB constant_expression oRB")
				// array declaration
				if ( IS_FUNCTION(yypvt[-3].p_symbol) )
				{
					yyerror("function returns array");
					user_exit(1);
				}
				if ( IS_ARRAY(yypvt[-3].p_symbol) )
				{
					yyerror("only support one demision array");
					user_exit(1);
				}
				// check constant_expression
				if ( !IS_CL(yypvt[-1].p_symbol) )
				{
					yyerror("array unknown size");
					user_exit(1);
				}
				if ( !get_sym_value(yypvt[-1].p_symbol) )
				{
					yyerror("cannot allocate an array of constant size 0");
					user_exit(1);
				}
				if (get_sym_value(yypvt[-1].p_symbol) < 0)
				{
					yyerror("negative subscript or subscript is too large");
					user_exit(1);
				}
				// set array flag
				yypvt[-3].p_symbol->is_array = 1;
				yypvt[-3].p_symbol->num_ele = get_sym_value(yypvt[-1].p_symbol);
				del_symbol(yypvt[-1].p_symbol);
			} break;
case 44:
# line 664 "c.y"
{
				PARSE_INFO("direct_declarator :direct_declarator oLB oRB")
				if ( IS_FUNCTION(yypvt[-2].p_symbol) )
				{
					yyerror("function returns array");
					user_exit(1);
				}
				if ( IS_ARRAY(yypvt[-2].p_symbol) )
				{
					yyerror("only support one demision array");
					user_exit(1);
				}
				// set array flag
				yypvt[-2].p_symbol->is_array = 1;
				yypvt[-2].p_symbol->num_ele = 0;
			} break;
case 45:
# line 681 "c.y"
{
				PARSE_INFO("direct_declarator :direct_declarator oLP parameter_type_list oRP")
				if ( IS_FUNCTION(yypvt[-3].p_symbol) )
				{
					yyerror("function returns function");
					user_exit(1);
				}
				if ( IS_ARRAY(yypvt[-3].p_symbol) )
				{
					yyerror("array element type cannot be function");
					user_exit(1);
				}
				// set function flag
				yypvt[-3].p_symbol->is_function = 1;
				// assign function rname
				if ( IS_MAIN_FUNC(yypvt[-3].p_symbol) )
				{
					// main(..)
					strcpy(yypvt[-3].p_symbol->rname, yypvt[-3].p_symbol->name);
				}
				else
				{
					sprintf(yypvt[-3].p_symbol->rname, _FUNCTION_NAME, yypvt[-3].p_symbol->name);
				}
				// check weather func(void)
				if ( yypvt[-1].p_symbol->NOUN == SPEC_VOID )
				{
					if ( yypvt[-1].p_symbol->next )
					{
						yyerror("internal error in 'direct_declarator oLP parameter_type_list oRP'");
						user_exit(1);
					}
					//so, ...
					yypvt[-3].p_symbol->args = NULL;
					del_symbol(yypvt[-1].p_symbol);
				}
				else
				{
					yypvt[-3].p_symbol->args = yypvt[-1].p_symbol;
					// assign parameters rname
					assign_parameters_rname(yypvt[-1].p_symbol);
				}
			} break;
case 46:
# line 731 "c.y"
{
				PARSE_INFO("direct_declarator :direct_declarator oLP oRP")
				if ( IS_FUNCTION(yypvt[-2].p_symbol) )
				{
					yyerror("function returns function");
					user_exit(1);
				}
				if ( IS_ARRAY(yypvt[-2].p_symbol) )
				{
					yyerror("array element type cannot be function");
					user_exit(1);
				}
				// set function flag
				yypvt[-2].p_symbol->is_function = 1;
				// assign function rname
				if ( IS_MAIN_FUNC(yypvt[-2].p_symbol) )
				{
					// main(..)
					strcpy(yypvt[-2].p_symbol->rname, yypvt[-2].p_symbol->name);
				}
				else
				{
					sprintf(yypvt[-2].p_symbol->rname, _FUNCTION_NAME, yypvt[-2].p_symbol->name);
				}
				// no parameters , so ->args is NULL as default
			} break;
case 47:
# line 793 "c.y"
{
				PARSE_INFO("parameter_type_list :parameter_list")
			} break;
case 48:
# line 797 "c.y"
{
				PARSE_INFO("parameter_type_list :parameter_list oCOMMA oDOTDOTDOT")

⌨️ 快捷键说明

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