词法分析
https://blog.csdn.net/D1124615130/article/details/106923146
由于当初是用C语言写的代码,没考虑周全导致代码可读性差,因此只贴了语法分析,删除了语义。
C语言文件操作
https://blog.csdn.net/D1124615130/article/details/106785256
%{
/****************************************************************************
mysql4yacc.y
ParserWizard generated YACC file.
Date: 2020年6月11日
****************************************************************************/
#include "mysql4lex.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <direct.h>
#include <io.h>
#include<ctype.h>
#include<windows.h>
%}
%left OR
%left AND
%right NOT
%nonassoc '=' '>' '<'
%left '+','-'
%left '*','/'
%term CREATE TABLE CHAR INT DATABASE USE SHOW DATABASES
%term EXIT QUIT DROP TABLE TABLES INSERT INTO VALUES
%term SELECT FROM WHERE FALSE TRUE AND NOT OR DELETE UPDATE SET
%term ID NUMBER STRING
%nonterm table field type databasename data string expr
%nonterm fieldsdefinition field_type fields
%nonterm datas
%nonterm usedatabasesql showsql exitsql dropsql insertsql selectsql deletesql updatesql createsql
%nonterm fields_star selectfields
%nonterm tables
%nonterm conditions
%nonterm condition
%nonterm fields_values
%nonterm statement statements
%%
statements : statements statement ;
statement : usedatabasesql | showsql | createsql | dropsql | insertsql | selectsql | deletesql | updatesql | exitsql ;
usedatabasesql : USE databasename ';';
showsql : SHOW DATABASES ';' | SHOW TABLES ';';
databasename : ID ;
createsql : CREATE DATABASE databasename ';' | CREATE TABLE table '(' fieldsdefinition ')' ';' ;
dropsql : DROP DATABASE databasename ';' | DROP TABLE table ';';
insertsql : INSERT INTO table '(' fields ')' VALUES '(' datas ')' ';' | INSERT INTO table VALUES '(' datas ')' ';' ;
selectsql : SELECT fields_star FROM tables ';' | SELECT fields_star FROM tables WHERE conditions ';' ;
deletesql : DELETE FROM table WHERE conditions ';'| DELETE FROM table ';';
updatesql : UPDATE table SET fields_values ';'| UPDATE table SET fields_values WHERE conditions ';';
fields_values : field '=' data ',' fields_values | field '=' data ;
tables : table ',' tables | table ;
fields_star : selectfields | '*';
selectfields : field ',' selectfields | field ;
conditions : '(' conditions ')' | conditions AND conditions | conditions OR conditions | NOT conditions
| condition '<' '>' condition | condition '!' '=' condition | condition '>' condition
| condition '>' '=' condition | condition '<' condition | condition '<' '=' condition
| condition '=' condition | TRUE | FALSE ;
condition : expr | field | string;
expr : expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | NUMBER ;
fields : field ',' fields | field ;
datas : data | data ',' datas;
data : string | NUMBER ;
string : '\'' STRING '\''| '\'' ID '\''| '\'' NUMBER '\'';
table : ID;
fieldsdefinition : field_type | field_type ',' fieldsdefinition;
field_type : field type ;
field : ID ;
type : CHAR '('NUMBER')' | INT ;
exitsql : EXIT | QUIT;
%%
int main(void)
{
while(1){
printf("mysql> ");
yyparse();
}
return 0;
}
觉得有用的话不妨点个赞,评个论,有什么问题可以在评论区提出。