-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsql.y
66 lines (60 loc) · 1.11 KB
/
sql.y
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
%{
#include <stdio.h>
#include <string.h>
#include "struct.h"
int yylex();
int yyerror();
symbtbl *ptr;
%}
%union
{
struct{
int numerorighe;
symbtbl *Mystr;
};
};
%token <numerorighe> NL
%token <Mystr> IDENTIFIER CONST '<' '>' '=' '*'
%token SELECT FROM WHERE AND OR
%type <Mystr> identifiers cond compare op
%%
lines: line
| lines line
| lines error
;
line: select identifiers FROM identifiers WHERE cond NL {
/*printf("Line %d \n",$7);
printf("columns: %s\n",$2);
printf("tables: %s\n\n",$4);*/
ptr=putsymb($2,$4,$7);
};
identifiers: '*' {$$="ALL";}
| IDENTIFIER {$$=$1;}
| IDENTIFIER','identifiers{char* s=malloc(sizeof(char)*(strlen($1)+strlen($3)+1));
strcpy(s,$1);strcat(s," ");strcat(s,$3); $$=s;
};
select: SELECT;
cond: IDENTIFIER op compare
| IDENTIFIER op compare conn cond;
compare: IDENTIFIER
| CONST;
op: '<'
|'='
|'>';
conn: AND
| OR;
%%
int yyerror(char *s){
printf(" --ERROR-- %s\n\n",s);
/*printf("\tERROR IN LINE %4d\n", yylval+1);
*/
return *s;
}
int main() {
FILE* del;
char filename[] = "results.txt";
del = fopen(filename,"a");
remove(filename);
yyparse();
return 0;
}