-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathParseCreate.cpp
More file actions
134 lines (118 loc) · 3.6 KB
/
ParseCreate.cpp
File metadata and controls
134 lines (118 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// ParseCreate.cpp
// SQL parser
//
// Created by 王璇 on 17/2/1.
// Copyright © 2017年 Xuan Wang. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "lexanal.h"
#include <stdlib.h>
//pay attention to the parameter list
int ParseCreate(char* tname,char *tb_info, int *size, char* p){
int index=0;//to identify the position of new info
int i=0;
int attri_num=0;
char token[100]; // Output for lexanal( )
int token_code=0;
p=lex_anal( p, token, &token_code );//watch out for space
if(token_code!=TABLE){
printf("Error: keyword TABLE excepted");
return 0;
}
//get next token--table name
p=lex_anal( p, token, &token_code );
if(token_code!=IDENTIFIER){
printf("Error: table name expected");
return 0;
}
else{
//store table name
for(i=0;i<strlen(token);i++){
tname[i]=token[i];
}
tname[i]='\0';
}
//get next token--(
p=lex_anal( p, token, &token_code );
if(token_code!='('){
printf("Error: ( expected");
return 0;
}
do{
//get next token--attribute name
p=lex_anal( p, token, &token_code );
if(token_code!=IDENTIFIER){
printf("Error '%s': attribute name expected\n", token);
return 0;
}
else{
//store attribute name
for(i=0;i<strlen(token);i++){
tb_info[index+i]=token[i];
}
tb_info[index+i]=' ';
index=index+(int)strlen(token)+1;
}
//get next token--data type(int|float|char)
p=lex_anal( p, token, &token_code );
if(strcasecmp(token, "INT")==0){
token_code='I';
size[attri_num]=sizeof(int);
attri_num++;
tb_info[index]='I';
tb_info[index+1]=' ';
index=index+2;
}
else if(strcasecmp(token, "FLOAT")==0){
token_code='F';
size[attri_num]=8;
attri_num++;
tb_info[index]='F';
tb_info[index+1]=' ';
index=index+2;
}
else if(strcasecmp(token, "CHAR")==0){
//get next token--(
p=lex_anal(p, token, &token_code);
if(token_code!='('){
printf("Error '%s': ( expected", token);
return 0;
}
else{
//get next token--number
p=lex_anal(p, token, &token_code);
if(token_code!=INTEGER_NUMBER){
printf("Error '%s': integer size expected", token);
return 0;
}
else{
token_code='C';
size[attri_num]=atoi(token)+1;
attri_num++;
tb_info[index]='C';
tb_info[index+1]=' ';
index=index+2;
//get next token
p=lex_anal(p, token, &token_code);
if(token_code!=')'){
printf("Error '%s': ) expected",token);
return 0;
}
}
}
}
else{
printf("Error: data type expected!");
return 0;
}
//get next token
p=lex_anal(p, token, &token_code);
if(token_code !=','&& token_code !=')')
return 0;
}while(token_code==',');
return 1;
}