当前位置:首页 > 发言稿 > 编译原理上机报告 编译原理实验总结
 

编译原理上机报告 编译原理实验总结

发布时间:2019-07-20 11:16:27 影响了:

编译原理上机报告

《DBMS 的设计与实现》

学号: 姓名: 手机:

完成时间:

邮箱:

目 录

1. 1.1 1.2 2. 2.1 2.2 2.3 2.4 3. 3.1 3.2

项目概况 .............................................................................................................. 3

基本目标 ........................................................................................................ 3 完成情况 ........................................................................................................ 3 项目实现方案 ...................................................................................................... 4

逻辑结构与物理结构 .................................................................................... 4 语法结构与数据结构 .................................................................................... 6 执行流程 ...................................................................................................... 12 功能测试 ...................................................................................................... 14 总结与未来工作. . .............................................................................................. 18

未完成功能 .................................................................................................. 18 未来实现方案 .............................................................................................. 18

4. 附录(源代码).....................................................................................................20

1、项目概况

1.1 基本目标

DBMS 的设计与实现:

设计并实现一个DBMS 原型系统,可以接受基本的SQL 语句,对其进行词法分析、语法分析,然后解释执行SQL 语句,完成对数据库文件的相应操作,实现 DBMS 的基本功能。 目的:

1. 加深编译原理基础知识的理解:词法分析、语法分析、语法制导翻译等; 2. 加深相关基础知识的理解:数据库系统、数据结构、操作系统等。

1.2 完成情况

已经实现的功能、语句:

1、CREATE DATABASE 、USE DATABASE 、CREATE TABLE 、INSERT 、SELECT 、UPDATE 、DELETE 、DROP TABLE

DATABASE 的词法规则

2、CREATE DATABASE 、USE DATABASE 、CREATE TABLE 、INSERT 、SELECT 、UPDATE 、DELETE 、DROP TABLE

DATABASE 的语法规则

3、CREATE DATABASE、USE DATABASE 、CREATE TABLE 、SELECT 的部分语法制导翻译

、DROP

、DROP

2. 项目实现方案

2.1 逻辑结构与物理结构

为了要实现SQL 语句,增加的元数据: 表字典结构: struct TableDictionary{ char *table; int page;

struct TableDictionary *nexttable; }; 列字典结构: struct RowDictionary{ char *table; int rowid; char *field; int offset; int width; };

元数据的逻辑结构:

数据库可分为系统数据库与用户数据库。数据库信息分为元数据和基本数据,可以作为两个物理文件分开保存。

系统数据库sys :

使用database 表保存用户数据库的相关信息,使用security 表保存安全控制的相关信息等,具体包含2个文件:

1、元数据文件sys.db

表字典:表在基本数据文件sys.dat 中的起始页数,如database 表开始于0页、security 表开始于4页;

列字典:

database 表的结构:列的名称、宽度等; security 表的结构:列的名称、宽度等; 2、基本数据文件sys.dat

database 表的具体内容:用户数据库名、元数据文件名、基本数据文件名等; security 表的具体内容:用户名、用户数据库名等; 用户数据库xjgl 结构: 1、元数据文件xjgl.db

表字典:表在基本数据文件xjgl.dat 中的起始页数,如course 表开始于0页,其它表的开始页数;

列字典:

course 表的结构:列的名称、宽度等; 其它表的结构; 2、基本数据文件sys.dat

course 表的具体内容:课程号cno ,课程名cname ,学分ccredit 等; 其它表的具体内容;

表2 系统基本数据文件逻辑结构

表4 用户基本数据文件的逻辑结构

2.2 语法结构与数据结构

CREATE 语句的产生式语法结构:

createsql: CREATE TABLE table "(" fieldsdefinition ")" ";"

非终结符createsql 的属性使用如下结构说明: typedef struct _createstruct{ /*create语法树根节点*/

char *table;

_createfieldsdef_type

*fdef;

}_createstruct_type;

非终结符fieldsdefinition 的_createfieldsdef_type数据结构如下: typedef struct _createfieldsdef{/*create语句中的字段定义*/

char *field;

enum TYPE type; int

length;

struct _createfieldsdef *next_fdef;

}_createfieldsdef_type;

配以实例说明该数据结构: CREATE TABLE Student

( Sno CHAR(9), Sname CHAR(20), Ssex CHAR(2), Sage INT );

对应的数据结构如下图所示:

图2 数据结构图

SELECT 语句的产生式语法结构:

selectsql: SELECT fields_star FROM tables ";"

| SELECT fields_star FROM tables WHERE conditions ";";

非终结符selectsql 的属性使用如下结构说明: struct Selectstruct{

/*select语法树的根节点*/

//所选字段

struct Selectedfields *sf;

struct Selectedtables *st; //所选基本表 struct Conditions };

*s_cons; //条件

非终结符conditions 的数据结构如下: struct Conditions{/*条件*/

struct Conditions struct Conditions char

*left; *right;

//左部条件 //右部条件

comp_op;/* "a"是and, "o"是or, "" , "=", "!=" */

int type; /* 0是字段,1是字符串,2是整数 */ char char };

*value;/* 根据type 存放字段名、字符串或整数 */ *table;

/* NULL或表名 */

非终结符fields_star 的Selectedfields 数据结构如下: struct Selectedfields{/*select语句中选中的字段*/

char *table; char *field;

//字段所属表 //字段名称

struct Selectedfields *next_sf;//下一个字段 };

非终结符tables 的Selectedtables 数据结构如下: struct Selectedtables{ char

*table;

/*select语句中选中的表*/ //基本表名称

//下一个表

struct Selectedtables *next_st; };

配以实例说明该数据结构:

SELECT Sno, Sname FROM student WHERE Ssex="男" AND Sage=20; 对应的数据结构如下图所示:

INSERT struct };

struct int

struct Datas *next_id;//下一字段

};

非终结符fields_name的Insertfields 数据结构如下:

struct Insertfields{

char *field;

struct Insertfields *nest_if;

};

DELETE 语句的产生式语法结构:

deletesql: DELETE FROM table WHERE conditions ";"

| DELETE FROM table ";";

非终结符deletesql 的数据结构如下:

struct Deletestruct{

char *table; //表

struct Conditions *d_cons; //条件

};

UPDATE 语句的产生式语法结构:

updatesql: UPDATE table SET fields_sets WHERE conditions ";";

非终结符updatesql 的数据结构如下:

struct Updatestruct{

char *table; //表

struct Updatefields *uf; //设置字段

struct Conditions *u_cons; //条件

};

非终结符fields_sets的Updatefields 数据结构如下:

struct Updatefields{

char *field;

int type; //type为1表示字符串,2表示数字

char *value; //根据type 的只存放字符串或者整数

struct Updatefields *next_uf; //下一字段

};

2.3 执行流程

CREATE DATABASE的执行:

函数名称:int createdatabase( );

函数说明:创建数据库的实现过程

输入参数:数据库名称

输出参数:创建数据库是否成功的语句提示

USE DATABASE的执行:

函数名称:int usedatabase( );

函数说明:选择数据库的实现过程

输入参数:数据库名称

输出参数:选择数据库是否成功的语句提示

CREATE TABLE的执行:

函数名称:int createtable( );

函数说明:创建表的实现过程create 语句语法树根节点

输入参数:create 语句语法树根节点

实现过程:修改xjgl.db

1、在表字典中加入student 的起始页数

2、在列字典中加入student 每个列的说明

输出参数:创建表是否成功的语句提示

SELECT 的执行:

函数名称:selecttable( );

函数说明:选择行的实现过程

输入参数:表名,条件

输出参数:输出选择的行

INSERT 的执行:

函数名称:int usedatabase( );

函数说明:插入行的实现过程

输入参数:表名

输出参数:插入行是否成功的语句提示

UPDATE 的执行:

函数名称:int updatetable( );

函数说明:更新表的实现过程

输入参数:表名,条件

输出参数:更新是否成功的语句提示

DELETE 的执行:

函数名称:int deletetable( );

函数说明:删除行的实现过程

输入参数:表名,条件

输出参数:删除行是否成功的语句提示

DROP TABLE 的执行:

函数名称:int droptable( );

函数说明:删除表的实现过程

输入参数:表名

输出参数:删除表是否成功的语句提示

DROP DATABASE 的执行:

函数名称:int dropdatabase( );

函数说明:删除数据库的实现过程

输入参数:数据库名称

输出参数:删除数据库是否成功的语句提示

2.4 功能测试

测试所实现的SQL 语句的基本功能:

测试1

输入:CREATE TABLE Student ( Sno CHAR(9), Sname CHAR(20), Ssex CHAR(2),

Sage INT );

输出:

测试2

输入:CREATE TABLE student (name CHAR(10),num INT);

输出:

测试3

输入:CREATE DATABASE;

输出:

测试4

输入:USE DATABASE;

输出:

测试5

输入:DELETE * FROM student;

输出:

测试6

输入:DROP TABLE Student;

输出:

3、总结与未来工作

2.5 未完成功能

由于时间关系及个人能力有限,主要完成了SQL 基本语句的词法、语法识别工作,关于语法制导翻译部分只完成了部分,没有全部完成,还望老师见谅。

2.6 未来实现方案

语法制导翻译的主要实现部分,是关键函数的实现,语法树的构造。根据输入的内容赋值给相关语句,构造语法树,完成相关操作。需要具体实现createdatabase( ),usedatabase( ),createtable( ),selecttable( ),usedatabase( ),updatetable

( ),deletetable( ),droptable( ),dropdatabase( )这些函数。

4、附录

Lex 源代码:

%{

/****************************************************************************

mylexer.l

ParserWizard generated Lex file.

Date: 2012年6月15日

****************************************************************************/

#include "myparser.h"

#include

%}

/////////////////////////////////////////////////////////////////////////////

// declarations section

//辅助定义正规式

digit [0-9]

digits {digit}+

optional_fraction ("."{digits})?

optional_exponent (E[+-]?{digits})?

char [a-zA-Z]

id {char}({char}|{digit})*

mum {digits}{optional_fraction}{optional_exponent} relation

// place any declarations here

%%

///////////////////////////////////////////////////////////////////////////// // rules section

CREATE {return CREATE;}

TABLE

CHAR

INT

[]+;

id {

yylval.yych=(char *)malloc(strlen(yytext)+1);

[0-9]+

{return TABLE;} {return CHAR;} {return INT;} strcpy(yylval.yych, yytext); return ID;} { yylval.yych=(char *)malloc(strlen(yytext)+1); strcpy(yylval.yych, yytext);

|

|

| return NUMBER; } ";" "(" ")"

","

{return yytext[0]; }

// place your Lex rules here

%%

/////////////////////////////////////////////////////////////////////////////

// programs section

Yacc 源代码:

myparser.y

ParserWizard generated YACC file.

Date: 2012年6月15日

****************************************************************************/

21

#include "mylexer.h"

#include

%}

/////////////////////////////////////////////////////////////////////////////

// declarations section

enum Type {INT,CHAR};

struct Createfieldsdef{

};

struct Createstruct{

};

%union /*定义yylval 的格式*/ char *table; /*create语法树根节点*/ //基本表名称 char *field; //字段可用类型 /*create语句中的字段定义*/ //字段名称 //字段类型 enum TYPE type; int length; //字段长度 struct Createfieldsdef *next_fdef; //下一字段 struct Createfieldsdef *fdef; //字段定义

{ /*属于create 语法树的类型*/

char * yych; //字面量 //字段定义 22 struct Createfieldsdef *cfdef_var;

} struct Createstruct *cs_var; //整个create 语句

绑定到终结符、非终结符:

/*create语句中涉及的符号*/

%term CREATE TABLE ID CHAR N UMBER INT

%nonterm table fileld type

%nonterm fieldsdefinition field_type %nonterm createsql

// attribute type

%include {

#ifndef YYSTYPE

#define YYSTYPE int

#endif

}

// place any declarations here

%token NUMBER

%token ID

%left "+", "-"

%left "*", "/"

23

// 无优先级与结合性 // 低优先级与左结合 // 高优先级与左结合

%%

/////////////////////////////////////////////////////////////////////////////

// rules section

statements: statements statement | statement

statement: createsql | selectsql | insertsql | deletesql | updatesql { int result;

}

createsql: CREATE TABLE table "(" fieldsdefinition ")" ";"

{

result=createtable($1); if(result==1){ printf("Create Table %s Successful!\n",$1->table); }else { printf("Create Table %s Failed!\n",$1->table); } $$=(struct Createstruct *)malloc(sizeof(struct Createstruct)); $$->table=$3; $$->fdef=$5; 24

}

table:ID

fieldsdefinition: field_type

{

$$=(struct Createstruct *)malloc(sizeof(struct Createfieldsdef));

}

| fieldsdefinition "," field_type

{

$1->next_fdef=$3;

$$=(struct Createstruct *)malloc(sizeof(struct Createfieldsdef));

}

field_type: field type

{

25

$$->field=$1->field; $$->type=$1->type; $$->length=$1->length; $$->next_fdef=$1->next_fdef; $$->field=$1->field; $$->type=$1->type; $$->length=$1->length; $$->next_fdef=$1->next_fdef;

$$=(struct Createstruct *)malloc(sizeof(struct Createfieldsdef)); $$->field=$1;

$$->type=$2;

}

field: ID

{

$$=$1;

}

type: CHAR "(" NUMBER ")"

{

$$=$3;

}

| INT

// place your YACC rules here (there must be at least one)

Grammar

%%

/////////////////////////////////////////////////////////////////////////////

26

: /* empty */ ;

// programs section

int main(void) {

return yyparse(); }

27

猜你想看
相关文章

Copyright © 2008 - 2022 版权所有 职场范文网

工业和信息化部 备案号:沪ICP备18009755号-3