📄 main.c
字号:
#ifndef lint
static char const
yyrcsid[] = "$FreeBSD: src/usr.bin/yacc/skeleton.c,v 1.28 2000/01/17 02:04:06 bde Exp $";
#endif
#include <stdlib.h>
#define YYBYACC 1
#define YYMAJOR 1
#define YYMINOR 9
#define YYLEX yylex()
#define YYEMPTY -1
#define yyclearin (yychar=(YYEMPTY))
#define yyerrok (yyerrflag=0)
#define YYRECOVERING() (yyerrflag!=0)
static int yygrowstack();
#define YYPREFIX "yy"
#line 1 "drop.y"
#include "iostream.h"
#include "stdio.h"
#include "conio.h"
#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;
#line 27 "drop.y"
typedef 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;
} YYSTYPE;
#line 64 "main.c"
#define YYERRCODE 256
#define SELECT 257
#define FROM 258
#define WHERE 259
#define IDENTIFIER 260
#define NUMBER 261
#define CREATE 262
#define TABLE 263
#define CHAR 264
#define INT 265
#define DATE 266
#define DROP 267
#define INSERT 268
#define INTO 269
#define VALUES 270
#define DELETE 271
#define AND 272
#define OR 273
#define QUIT 274
#define SHOW 275
#define TABLES 276
#define TABLE_DIC 277
#define OF 278
#define COLS 279
const short yylhs[] = { -1,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
2, 2, 5, 5, 3, 3, 4, 4, 7, 6,
8, 8, 12, 10, 10, 11, 9, 9, 9, 13,
18, 15, 15, 14, 14, 16, 16, 17, 17, 17,
19, 19, 24, 24, 24, 20, 21, 22, 22, 22,
22, 23, 23, 25, 25, 25, 26, 26, 26,
};
const short yylen[] = { 2,
1, 1, 1, 1, 1, 2, 2, 2, 5, 7,
1, 1, 1, 3, 1, 3, 1, 3, 1, 1,
1, 1, 7, 1, 3, 2, 4, 1, 1, 4,
9, 0, 3, 1, 3, 1, 3, 3, 3, 1,
6, 4, 1, 7, 7, 3, 1, 1, 3, 3,
1, 1, 3, 1, 1, 1, 2, 2, 4,
};
const short yydefred[] = { 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 52, 2, 3, 4, 5, 43, 0, 47,
0, 0, 12, 0, 0, 15, 0, 17, 0, 0,
0, 0, 7, 57, 58, 0, 0, 0, 54, 55,
56, 0, 6, 8, 0, 0, 0, 20, 0, 0,
0, 0, 0, 0, 19, 53, 51, 0, 46, 48,
0, 13, 16, 18, 0, 30, 0, 0, 0, 42,
59, 0, 0, 0, 0, 0, 9, 0, 0, 0,
24, 34, 0, 0, 0, 0, 0, 49, 50, 0,
14, 0, 28, 29, 26, 0, 0, 0, 33, 0,
41, 0, 0, 10, 0, 25, 23, 35, 40, 0,
0, 36, 44, 45, 0, 0, 0, 0, 0, 27,
38, 39, 37, 31,
};
const short yydgoto[] = { 10,
11, 24, 25, 26, 61, 12, 13, 0, 95, 80,
81, 14, 15, 83, 68, 111, 112, 16, 17, 18,
19, 59, 20, 21, 42, 22,
};
const short yysindex[] = { -40,
-34, 0, -256, -249, -251, -234, -18, -266, -39, 0,
0, -20, 0, 0, 0, 0, 0, 0, -33, 0,
-7, -4, 0, -202, 13, 0, 12, 0, -201, -201,
-201, -201, 0, 0, 0, -218, 21, -197, 0, 0,
0, -37, 0, 0, -201, -196, -197, 0, 25, 9,
27, -55, -201, -238, 0, 0, 0, -214, 0, 0,
-23, 0, 0, 0, -197, 0, -197, -200, -39, 0,
0, 32, 34, 36, 37, -39, 0, -201, -221, -25,
0, 0, -21, 38, 18, -39, -39, 0, 0, 20,
0, 40, 0, 0, 0, -197, 22, -197, 0, -36,
0, 41, 44, 0, -174, 0, 0, 0, 0, -212,
-19, 0, 0, 0, 47, 50, 51, -36, 33, 0,
0, 0, 0, 0,
};
const short yyrindex[] = { 0,
0, -29, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -167, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-177, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
};
const short yygindex[] = { 0,
0, 0, 0, 48, 0, 8, 4, 0, 0, 0,
-1, 0, 0, 0, 0, 0, -22, 0, 0, 0,
0, 0, 55, -3, 0, 0,
};
#define YYTABLESIZE 236
const short yytable[] = { 9,
9, 58, 110, 70, 28, 37, 29, 23, 27, 34,
35, 19, 36, 30, 19, 97, 20, 31, 96, 99,
78, 119, 98, 32, 118, 38, 39, 41, 40, 19,
19, 19, 19, 72, 73, 77, 49, 50, 51, 52,
33, 56, 92, 93, 94, 74, 75, 116, 117, 28,
64, 43, 62, 27, 44, 45, 46, 47, 48, 53,
71, 54, 55, 2, 65, 85, 67, 66, 79, 84,
82, 86, 90, 87, 88, 89, 101, 100, 104, 105,
107, 113, 102, 103, 114, 91, 115, 120, 121, 122,
11, 124, 32, 63, 106, 123, 60, 0, 0, 79,
0, 108, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 69, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 2,
2, 3, 2, 57, 109, 2, 4, 5, 19, 0,
6, 0, 0, 7, 8, 76,
};
const short yycheck[] = { 40,
40, 39, 39, 59, 1, 9, 263, 42, 1, 276,
277, 41, 279, 263, 44, 41, 46, 269, 44, 41,
44, 41, 44, 258, 44, 46, 60, 61, 62, 59,
60, 61, 62, 272, 273, 59, 29, 30, 31, 32,
59, 38, 264, 265, 266, 260, 261, 260, 261, 46,
47, 59, 45, 46, 59, 258, 44, 46, 260, 278,
53, 41, 260, 260, 40, 69, 40, 59, 65, 270,
67, 40, 76, 40, 39, 39, 59, 40, 59, 40,
59, 41, 86, 87, 41, 78, 261, 41, 39, 39,
258, 59, 270, 46, 96, 118, 42, -1, -1, 96,
-1, 98, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, 259, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, 257, -1, -1, 260,
260, 262, 260, 261, 261, 260, 267, 268, 258, -1,
271, -1, -1, 274, 275, 259,
};
#define YYFINAL 10
#ifndef YYDEBUG
#define YYDEBUG 0
#endif
#define YYMAXTOKEN 279
#if YYDEBUG
const char * const yyname[] = {
"end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,"'\\''","'('","')'","'*'",0,"','",0,"'.'",0,0,0,0,0,0,0,0,0,0,0,0,
"';'","'<'","'='","'>'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,"SELECT","FROM","WHERE","IDENTIFIER","NUMBER","CREATE","TABLE",
"CHAR","INT","DATE","DROP","INSERT","INTO","VALUES","DELETE","AND","OR","QUIT",
"SHOW","TABLES","TABLE_DIC","OF","COLS",
};
const char * const yyrule[] = {
"$accept : statement",
"statement : selectsql",
"statement : createsql",
"statement : dropsql",
"statement : insertsql",
"statement : deletesql",
"statement : conditions ';'",
"statement : QUIT ';'",
"statement : show ';'",
"selectsql : SELECT fields_star FROM tables ';'",
"selectsql : SELECT fields_star FROM tables WHERE conditions ';'",
"fields_star : table_fields",
"fields_star : '*'",
"tables : table",
"tables : tables ',' table",
"table_fields : table_field",
"table_fields : table_fields ',' table_field",
"table_field : field",
"table_field : table '.' field",
"field : IDENTIFIER",
"table : IDENTIFIER",
"express : IDENTIFIER",
"express : NUMBER",
"createsql : CREATE TABLE table '(' fieldsdefinition ')' ';'",
"fieldsdefinition : field_type",
"fieldsdefinition : fieldsdefinition ',' field_type",
"field_type : field type",
"type : CHAR '(' NUMBER ')'",
"type : INT",
"type : DATE",
"dropsql : DROP TABLE table ';'",
"insertsql : INSERT INTO table fields_choosed VALUES '(' values ')' ';'",
"fields_choosed :",
"fields_choosed : '(' fields ')'",
"fields : field",
"fields : fields ',' field",
"values : avalue",
"values : values ',' avalue",
"avalue : '\\'' IDENTIFIER '\\''",
"avalue : '\\'' NUMBER '\\''",
"avalue : NUMBER",
"deletesql : DELETE FROM table WHERE conditions ';'",
"deletesql : DELETE FROM table ';'",
"conditions : condition",
"conditions : '(' conditions ')' AND '(' conditions ')'",
"conditions : '(' conditions ')' OR '(' conditions ')'",
"condition : comp_left comp_op comp_right",
"comp_left : table_field_",
"comp_right : table_field_",
"comp_right : '\\'' IDENTIFIER '\\''",
"comp_right : '\\'' NUMBER '\\''",
"comp_right : NUMBER",
"table_field_ : field",
"table_field_ : table '.' field",
"comp_op : '<'",
"comp_op : '>'",
"comp_op : '='",
"show : SHOW TABLES",
"show : SHOW TABLE_DIC",
"show : SHOW COLS OF table",
};
#endif
#if YYDEBUG
#include <stdio.h>
#endif
#ifdef YYSTACKSIZE
#undef YYMAXDEPTH
#define YYMAXDEPTH YYSTACKSIZE
#else
#ifdef YYMAXDEPTH
#define YYSTACKSIZE YYMAXDEPTH
#else
#define YYSTACKSIZE 10000
#define YYMAXDEPTH 10000
#endif
#endif
#define YYINITSTACKSIZE 200
int yydebug;
int yynerrs;
int yyerrflag;
int yychar;
short *yyssp;
YYSTYPE *yyvsp;
YYSTYPE yyval;
YYSTYPE yylval;
short *yyss;
short *yysslim;
YYSTYPE *yyvs;
int yystacksize;
#line 489 "drop.y"
typedef struct tokentype{
char *name;
int value;
}tokentype;
tokentype tokens[] = /*这里只列出了没有定义类型的终结符*/
{ "SELECT", SELECT,
"FROM", FROM,
"WHERE", WHERE,
"CREATE", CREATE,
"DROP", DROP,
"TABLE", TABLE,
"CHAR", CHAR,
"INT", INT,
"DATE", DATE,
"INSERT", INSERT,
"INTO", INTO,
"VALUES", VALUES,
"DELETE", DELETE,
"AND", AND,
"OR", OR,
"QUIT", QUIT,
"SHOW", SHOW,
"TABLES", TABLES,
"TABLE_DIC", TABLE_DIC,
"COLS", COLS,
"OF", OF,
"\0", '\0'
};
int is_number(char *s) /*用来判断一个字符串是不是数字*/
{
char *ss;
ss=s;
while (*ss!='\0')
{ if (*ss<'0' || *ss>'9') {
return(0);
}
ss++;
}
return(1);
}
void low_to_up(char *s) /*把一个字符串变为大写的*/
{ unsigned int i;
for(i=0;i<strlen(s);i++)
{ s[i]=toupper(s[i]);
}
}
int is_token(char *s) /*用来判断一个字符串是不是一个无类型定义的token*/
{
char *str;
int i=0;
str=strdup(s);
low_to_up(str);
while(1)
{
if (strlen(tokens[i].name)==0) break;
if (strcmp(tokens[i].name,str)==0)
{ free(str);
return(tokens[i].value); /*是token,返回对应的值*/
}
i++;
}
free(str);
return(0); /*不是,则返回0*/
}
int is_legal(char s) /*用来判断一个字符是不是字符串中的合法字符*/
{ int flag=0;
if(s=='-') flag=1; //zhao
if(s=='&') flag=1; //zhao
if(s=='#') flag=1; //zhao
if(s>='a' && s<='z') flag=1;
if(s>='A' && s<='Z') flag=1;
if(s=='_') flag=1;
if(s>='0' && s<='9') flag=1;
if(flag==0) { return(0); }/*不是*/
return(1); /*是*/
}
int is_identifier(char *s) /*用来判断一个字符串是不是合格的标识符*/
{
char *ss;
ss=s;
while(*ss!='\0')
{ if(is_legal(*ss)==0) return(0);
ss++;
}
return(1);
}
void readaword(char *aword) /*将下一个词读入aword中*/
{
char ss;
int awordnum=0;
ss=sql[sqlnum];
while( is_legal(ss))
{ aword[awordnum]=ss;
awordnum++;
sqlnum++;
ss=sql[sqlnum];
}
aword[awordnum]='\0';
}
yylex()
{
char ss;
int is_a_token;
char theword[256]; /*用于分解出输入行的一个单词*/
while (sql[sqlnum]==' ')
sqlnum++;
ss=sql[sqlnum];
if (ss=='\0') return(0);
if (is_legal(ss)==0)
{ /*主要识别标点和运算符*/
sqlnum++;
return(ss);
}
else
{ readaword(theword);
/*主要识别保留字*/
if (is_a_token=is_token(theword)){
return(is_a_token);
} /*识别数字和标识符*/
yylval.yych=(char *)malloc(strlen(theword)+1);
strcpy(yylval.yych,theword);
if (is_number(theword)) {return(NUMBER);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -