- 论坛徽章:
- 0
|
多谢版主的指点,我今天完善了下这个程序,又碰到些问题,烦请您再指点一下。
sql.l
- %{
- #include "sql.h"
- #include "y.tab.h"
- #include <stdlib.h>
- #include <string.h>
- void yyerror(char *);
- %}
- %%
- CREATE { return CREATE;}
- TABLE { return TABLE;}
- INT {
- strcpy(yylval.attr_type, yytext);
- return TYPE;
- }
- STR20 {
- strcpy(yylval.attr_type, yytext);
- return TYPE;
- }
- [0-9]+ {
- yylval.intval = atoi(yytext);
- return INTEGER;
- }
- [a-z][a-z0-9]* {
- strcpy(yylval.attr_name, yytext);
- // fprintf(stderr, "-----%s\n", *yytext);
- return NAME;
- }
- [()\n] {
- return *yytext;
- }
- [ \t] ;
- . {
- yyerror("Unknown Character!");
- }
- %%
- int yywrap(void)
- {
- return 1;
- }
复制代码
sql.y:
- %{
- #include <stdio.h>
- #include <string.h>
- #include "sql.h"
- int i = 0;
- struct attr_name_type array[20];
- void yyerror(char *s);
- int yylex(void);
- struct attr_name_type copy(char *b, char *c);
- struct attr_name_type * copy_to_list(struct attr_name_type a);
- %}
- %union {
- int intval;
- char attr_type[10];
- char attr_name[20];
- struct attr_name_type nametype;
- struct attr_name_type *list;
- };
- %token <intval> INTEGER
- %token <attr_name> NAME
- %token <attr_type> TYPE
- %token CREATE TABLE
- %type <attr_type> type
- %type <attr_name> name
- %type <nametype> attrnametype
- %type <list> attributelist
- %%
- start:
- createtablestatement |start createtablestatement;
- createtablestatement:
- CREATE TABLE name left_bracket attributelist ')' '\n' {
- printf("table name: %s\n", $3);
- printf("NAME TYPE\n");
- int j;
- for(j = 0; j < i; j++)
- {
- printf("%s %s\n", ($5[j]).name, ($5[j]).type);
- }
- i = 0;
- }
- ;
- attributelist:
- attrnametype { //zhi zhixing yici.
- $$ = copy_to_list($1);
- // printf("First added: %s %s\n", $$[i - 1].name, $$[i - 1].type);
- }
- | attributelist attrnametype { //wang zhebianzou
- copy_to_list($2);
- $$ = $1;
- }
- ;
- attrnametype:
- name type {
- $$ = copy($1, $2);
- }
- | attrnametype '\n'
- ;
- left_bracket:
- '('
- | left_bracket '\n'
- ;
- name: NAME
- | name '\n'
- ;
- type: TYPE
- ;
- %%
- void yyerror(char *s)
- {
- fprintf(stderr," %s\n", s);
- }
- struct attr_name_type * copy_to_list(struct attr_name_type a)
- {
- // memmove(&(a[i]), &b, sizeof(struct attr_name_type));
- strcpy(array[i].name, a.name);
- strcpy(array[i].type, a.type);
- i++;
- return array;
- }
- struct attr_name_type copy(char *b, char *c)
- {
- struct attr_name_type a;
- strcpy(a.name, b);
- strcpy(a.type, c);
- return a;
- }
-
- int main(void)
- {
- yyparse();
- }
复制代码
sql.h:
- struct attr_name_type
- {
- char type[10];
- char name[20];
- };
复制代码
编译运行如下:
/2$ lex sql.l
/2$ yacc -d sql.y
/2$ gcc -o create lex.yy.c y.tab.c -ll
/2$ ./create
CREATE TABLE aaa
(
bbb INT
ccc INT)
table name: aaa
NAME TYPE
bbb INT
ccc INT
这其中,
CREATE TABLE aaa
(
bbb INT
ccc INT)输入,下面是输出。
它建立了一个名字为aaa的表,有两个类型为int的参数bbb ccc.
不过我的程序如果这样输出时会有错误:
先输入一个或多个回车以后在输入
CREATE TABLE aaa
(
bbb INT
ccc INT)
就不能很好地分析了。
该怎么改才能让他支持可以先输入回车呢?请指点迷津,多谢了!
[ 本帖最后由 susesuse 于 2008-10-5 14:34 编辑 ] |
|