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

📄 drop.y

📁 一个c语言开发的小型的dbms系统
💻 Y
📖 第 1 页 / 共 2 页
字号:
%{
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "struct.h"
#include "error.h"
/*#include "wbexe.c"*/	/*里面有语法树显示,初步语义检查,数据字典显示*/



char	sql[256];		/*用于读输入行的*/
int	sqlnum=0;		/*用于表示读到了第几个字符*/
char	*error_var;	/*用来接收各个程序返回的错误提示*/
int 	finish_flag;	/*用来控制主程序的循环,当用户输入"QUIT"时,它的值为1,表示应用结束*/

_dic_type		dic;	/*用来存放数据字典*/

_selectedfields_type	*sf_var1,*sf_end;
_selectedtables_type	*st_var1,*st_end;
_createfieldsdef_type	*cfdef_end;
_insertvalues_type		*iv_var1,*iv_end;
_insertfields_type		*if_var1,*if_end;

%}

%union			/*定义yylval的格式*/
{	char			char_var;
	char			*yych;
	/*---------------------------------属于select语法树的类型*/
	_selectedfields_type	*sf_var;
	_selectedtables_type	*st_var;
	_selectstruct_type		*ss_var;
	/*---------------------------------属于create语法树的类型*/
	_createfieldsdef_type	*cfdef_var;
	_createstruct_type		*cs_var;
	/*---------------------------------DROP-----------------*/
	_dropstruct_type	*drs_var;
	/*---------------------------------属于insert语法树的类型*/
	_insertfields_type		*if_var;
	_insertvalues_type		*iv_var;
	_insertstruct_type		*is_var;
	/*---------------------------------属于delete语法树的类型*/
	_deletestruct_type		*ds_var;
	_conditions_type		*cons_var;

}
%start statement
			/*select语句中涉及的符*/
%token	SELECT	FROM	WHERE	
%token	<yych>	IDENTIFIER	NUMBER	
%type	<ss_var>	selectsql		
%type	<sf_var>	fields_star	table_fields	table_field
%type	<st_var>	tables
%type	<yych>	table	field	express	

			/*create语句中涉及的符*/
%token	CREATE	TABLE	CHAR	INT	DATE
%type	<yych>	type
%type	<cfdef_var>	fieldsdefinition		field_type
%type	<cs_var>	createsql

			/*drop*/
%token	DROP	TABLE	
%type	<drs_var>	dropsql

			/*insert语句中涉及的符*/
%token	INSERT	INTO	VALUES
%type	<if_var>	fields	fields_choosed
%type	<iv_var>	values	avalue
%type	<is_var>	insertsql

			/*delete语句中涉及的符*/
%token	DELETE	
%type	<ds_var>	deletesql

%token	AND	OR
%type	<cons_var>	condition		comp_left		comp_right
%type	<cons_var>	table_field_	conditions
%type	<char_var>	comp_op

%token	QUIT	
%token	SHOW	TABLES	TABLE_DIC		OF	COLS

%%
statement:	selectsql
		{/*调用select对应的函数*/
		error_var=select_errorcheck(&dic,$1);
		if (strcmp(error_var,"")!=0) 
			{yyerror(error_var);
			return(1);
			}
		selectpro($1,&dic);
		/*selectexe($1);*/
		}
	|createsql
		{/*调用createexe对应的函数*/
		error_var=create_errorcheck(&dic,$1);
		if (strcmp(error_var,"")!=0) 
			{yyerror(error_var);
			return(1);
			}
		createpro($1,&dic);
		/*createexe($1);*/
		}
	|dropsql
		{
		error_var=drop_errorcheck(&dic,$1);
		if (strcmp(error_var,"")!=0)
			{
			yyerror(error_var);
			return(1);
			}
		droppro($1,&dic);
		/*dropexe($1)*/
		}
	|insertsql
		{/*调用insertexe对应的函数*/
		error_var=insert_errorcheck(&dic,$1);
		if (strcmp(error_var,"")!=0) 
			{yyerror(error_var);
			return(1);
			}
		insertpro($1,&dic);
		/*insertexe($1);*/
		}
	|deletesql
		{/*调用deleteexe对应的函数*/
		error_var=delete_errorcheck(&dic,$1);
		if (strcmp(error_var,"")!=0) 
			{yyerror(error_var);
			return(1);
			}
		deletepro($1,&dic);
		/*deleteexe($1);*/
		}
	|conditions ';'
		{
		/*conditionsexe($1);*/
		}
	|QUIT ';'
		{
		finish_flag=1;	/*表示应当退出了*/
		}
	|show ';'
	;

	/*------------------------------------------------------下面是select语句的定义--------*/
selectsql:	SELECT fields_star FROM tables ';'	
		{
		$$=(_selectstruct_type *)malloc(sizeof(_selectstruct_type));
		$$->sf=$2;
		$$->st=$4;
		$$->cons=NULL;
		}
	|SELECT fields_star FROM tables WHERE conditions ';'	
		{
		$$=(_selectstruct_type *)malloc(sizeof(_selectstruct_type));
		$$->sf=$2;
		$$->st=$4;
		$$->cons=$6;
		}
	;
fields_star:	table_fields	/*如果输入了具体的字段名称*/
		{
		$$=$1;
		}
	|'*'			/*如果输入了星号*/
		{
		$$=(_selectedfields_type *)malloc(sizeof(_selectedfields_type));
		$$->table=NULL;
		$$->field="*";
		$$->next_sf=NULL;
		}
	;
tables:	table			/*第一个表*/
		{
		$$=(_selectedtables_type *)malloc(sizeof(_selectedtables_type));
		$$->table=$1;
		$$->next_st=NULL;
		st_end=$$;
		}
	|tables ',' table	/*后面的表*/
		{
		$$=$1;
		st_var1=(_selectedtables_type *)malloc(sizeof(_selectedtables_type));
		st_var1->table=$3;
		st_var1->next_st=NULL;
		st_end->next_st=st_var1;	/*建立表名的连接*/
		st_end=st_var1;
		}
	;
table_fields:	table_field
		{
		$$=$1;
		sf_end=$$;		/*第一个字段名称*/
		}
	|table_fields ',' table_field	/*后面的字段*/
		{
		$$=$1;
		sf_end->next_sf=$3;	/*建立字段名的连接*/
		sf_end=$3;
		}
	;
table_field:	field
		{
		$$=(_selectedfields_type *)malloc(sizeof(_selectedfields_type));
		$$->table=(char *)malloc(sizeof(10));	/*为以后填上表名预留空间*/
		$$->table[0]='\0';
		$$->field=$1;
		$$->next_sf=NULL;
		}
	|table '.' field
		{
		$$=(_selectedfields_type *)malloc(sizeof(_selectedfields_type));		
		$$->table=$1;
		$$->field=$3;
		$$->next_sf=NULL;
		}
	;
field:	IDENTIFIER	
	;
table:	IDENTIFIER
	;
express:	IDENTIFIER
	|NUMBER	
	;

	/*-----------------------------------------------------下面是create语句的定义--------*/
createsql:	CREATE TABLE table '(' fieldsdefinition ')' ';'
		{
		$$=(_createstruct_type *)malloc(sizeof(_createstruct_type));
		$$->table=$3;
		$$->fdef=$5;
		}
	;
fieldsdefinition:	field_type
		{
		$$=$1;
		cfdef_end=$1;
		}	
	|fieldsdefinition ',' field_type
		{
		$$=$1;
		cfdef_end->next_fdef=$3;
		cfdef_end=$3;
		}
	;
field_type:	field  type
		{
		$$=(_createfieldsdef_type *)malloc(sizeof(_createfieldsdef_type));
		$$->field=$1;
		if (strlen($2)==0) 	/*表示类型为int的时候,用0表示类型,长度定为4*/
			{$$->type="0";
			$$->length="4";
			$$->next_fdef=NULL;
			}
		else	if(strcmp($2,"d")==0)
			{
			$$->type="3";
			$$->length="8";
			$$->next_fdef=NULL;
			}

		else	/*表示类型为char的时候,用1表示类型,长度定为$2*/
			{$$->type="1";
			$$->length=$2;
			$$->next_fdef=NULL;
			}
		}
	;
type:	CHAR '(' NUMBER ')'
		{$$=$3;}
	|INT
		{$$="\0";}
	|DATE
		{$$="d";}
	;

	/*--------------------------------------drop--------------------*/
dropsql:	DROP	TABLE	table ';'
		{
		$$=(_dropstruct_type *)malloc(sizeof(_dropstruct_type));
		$$->table=$3;
		}
	;
	/*------------------------------------------------------下面是insert语句的定义--------*/
insertsql:	INSERT	INTO  table fields_choosed  VALUES '('  values ')'  ';'
		{
		$$=(_insertstruct_type *)malloc(sizeof(_insertstruct_type));
		$$->table=$3;
		$$->ifs=$4;
		$$->iv=$7;
		}
	;
fields_choosed:	/*empty*/
		{
		$$=(_insertfields_type *)malloc(sizeof(_insertfields_type));
		$$->field=(char *)malloc(sizeof(10));
		$$->field="";
		$$->next_if=(_insertfields_type *)malloc(sizeof(_insertfields_type));
		$$->next_if=NULL;
		}
	|'(' fields ')'
		{
		$$=$2;
		}
	;
fields:	field
		{
		$$=(_insertfields_type *)malloc(sizeof(_insertfields_type));
		$$->field=$1;
		$$->next_if=NULL;
		if_end=$$;
		}
	|fields ',' field
		{
		$$=$1;
		if_var1=(_insertfields_type *)malloc(sizeof(_insertfields_type));
		if_var1->field=$3;
		if_var1->next_if=NULL;
		if_end->next_if=if_var1;
		if_end=if_var1;
		}
	;
values:	avalue
		{
		$$=$1;
		$$->next_iv=NULL;
		iv_end=$$;
		}
	|values ',' avalue		
		{
		$$=$1;
		iv_end->next_iv=$3;
		iv_end=$3;
		}
	;
avalue:	'\'' IDENTIFIER '\''
		{
		$$=(_insertvalues_type *)malloc(sizeof(_insertvalues_type));
		$$->value=$2;
		$$->type='1';
		$$->next_iv=NULL;
		}
	|'\'' NUMBER '\''
		{
		$$=(_insertvalues_type *)malloc(sizeof(_insertvalues_type));
		$$->value=$2;
		$$->type='1';
		$$->next_iv=NULL;
		}
	|NUMBER

⌨️ 快捷键说明

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