sql 解析器的 Makefile...编写依赖项
我正在 lex 和 yacc 中实现一个 sql 解析器, 因为我使用了一个符号表,我将其保存在单独的 .h 文件(sql.h)中,并且在这个头文件中我有一些函数声明。 这些函数的定义保存在 .c 文件 (sql.c) 中。现在我已将 sql.h 包含在 sql.c 中, 我在 lex 文件(1.l)和 yacc 文件(1.y)中引用了 sql.h 中的符号和函数。
问题是我无法为此编写正确的 makefile。 我收到诸如多个声明之类的错误。 我在哪里包含哪个文件以及如何编写依赖项? 请帮忙。我已经寻找了一个解决方案,但我没有得到它......
更新:
我像这样编译代码:
lex 1.l yacc -d 1.y gcc lex.yy.c y.tab.c sql.c -ll -ly
在 gcc 的第三个命令之后出现以下错误:
In file included from 1.l:5: sql.h:17: warning: ‘SQL’ initialized and declared ‘extern’ sql.h:18: warning: ‘SQL_SEL’ initialized and declared ‘extern’ 1.l: In function ‘makeTable’: 1.l:80: warning: assignment from incompatible pointer type In file included from 1.y:7: sql.h:17: warning: ‘SQL’ initialized and declared ‘extern’ sql.h:18: warning: ‘SQL_SEL’ initialized and declared ‘extern’ sql.c:3: error: redefinition of ‘SQL’ sql.h:15: note: previous definition of ‘SQL’ was here sql.c:4: error: redefinition of ‘SQL_SEL’ sql.h:16: note: previous definition of ‘SQL_SEL’ was here
sql.h:
#ifndef SQL_H
#define SQL_H
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct sym_table {
char *token;
char *value;
struct sym_table *next;
};
struct sym_select {
char **cols;
};
extern struct sym_table *SQL = NULL;
extern struct sym_select *SQL_SEL = NULL;
void addSymbol(char *, char *);
void print(struct sym_table *);
void showTable(struct sym_table *);
void makeTable(struct sym_table *, int);
sql.c:
#include "sql.h"
struct sym_table *SQL = NULL;
struct sym_select *SQL_SEL = NULL;
以及定义sql.h 1.l 文件中声明的函数
:
%{
#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h"
#include "sql.h"
int lineno=1;
void makeTable(struct sym_table *, int);
%}
..... 和其他 lex 文件
1.y
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int lineno;
extern void yyerror(char *);
#include "sql.h"
%}
.... 以及其他 yacc 文件数据
你能建议我一些其他方法来解决这个问题吗?
I'm implementing a sql parser in lex and yacc,
in that I use a symbol table which I kept in a separate .h file (sql.h) and in this header file I have some functions declarations.
The definitions of these functions are kept in a .c file (sql.c). Now I have included sql.h in sql.c,
I refer to the symbols and functions from sql.h in both my lex file(1.l) and yacc file(1.y).
The problem is that I'm not able to write a proper makefile for this.
I'm getting errors like multiple declarations.
Where do I include which file and how to write dependencies?
Please help. I have searched for a solution but I'm not getting it.....
Update:
I compile the code like this:
lex 1.l yacc -d 1.y gcc lex.yy.c y.tab.c sql.c -ll -ly
I get the following errors after the third command of gcc:
In file included from 1.l:5: sql.h:17: warning: ‘SQL’ initialized and declared ‘extern’ sql.h:18: warning: ‘SQL_SEL’ initialized and declared ‘extern’ 1.l: In function ‘makeTable’: 1.l:80: warning: assignment from incompatible pointer type In file included from 1.y:7: sql.h:17: warning: ‘SQL’ initialized and declared ‘extern’ sql.h:18: warning: ‘SQL_SEL’ initialized and declared ‘extern’ sql.c:3: error: redefinition of ‘SQL’ sql.h:15: note: previous definition of ‘SQL’ was here sql.c:4: error: redefinition of ‘SQL_SEL’ sql.h:16: note: previous definition of ‘SQL_SEL’ was here
sql.h:
#ifndef SQL_H
#define SQL_H
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct sym_table {
char *token;
char *value;
struct sym_table *next;
};
struct sym_select {
char **cols;
};
extern struct sym_table *SQL = NULL;
extern struct sym_select *SQL_SEL = NULL;
void addSymbol(char *, char *);
void print(struct sym_table *);
void showTable(struct sym_table *);
void makeTable(struct sym_table *, int);
sql.c:
#include "sql.h"
struct sym_table *SQL = NULL;
struct sym_select *SQL_SEL = NULL;
And the definitions of the functions declared in sql.h
1.l file:
%{
#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h"
#include "sql.h"
int lineno=1;
void makeTable(struct sym_table *, int);
%}
..... and othr lex file
1.y
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int lineno;
extern void yyerror(char *);
#include "sql.h"
%}
.... and other yacc file data
Can you suggest me some other way to get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请发布你的 Makefile。据我了解,代码也有问题,而不仅仅是 Makefile。或者您可能尝试从
1.l
生成1.o
并从1.y< 生成不同的
1.o
/代码>。通常,依赖关系应该类似于:
可能您还需要依赖于令牌的声明文件。
编辑:
啊,所以您可能需要将该表的定义放入一个文件中,并将声明放入标头中。您必须首先了解C中声明和定义的区别。例如,如果您有以下文件:
aaa.h
int arr[]={1};
aaa.c
#include "aaa.h"
bbb.c
#include "aaa.h"
然后您尝试
cc -o aaa aaa.c bbb.c
,您会收到多重定义错误。这意味着,实际的数组必须位于一个文件中,并且在标头中它应该类似于 extern int arr[];更新:
您应该删除 sql.h 中对 NULL 的设置。这只是一个声明,某处有这样那样的变量。实际值将在sql.c 中分配。
Please post your Makefile. As far as i understand there's also a problem with code, not only with Makefile. Or it could be that you try to make
1.o
from1.l
and different1.o
from1.y
.Normally the dependencies should look something like:
Probably you will also need to depend on tokens' declaration file.
EDIT:
Ah, so probably you need to put the definition of that table into one file, and the declaration into the header. You must first understand the difference between declaration and definition in C. For example if you have the following files:
aaa.h
int arr[]={1};
aaa.c
#include "aaa.h"
bbb.c
#include "aaa.h"
And then you try to
cc -o aaa aaa.c bbb.c
, you get the multiple definition error. That means, that the actual array must be in one file, and in the header it should be something likeextern int arr[];
Update:
You should remove setting to NULL in sql.h. It's only a declaration there, that there is such and such variable somewhere. The actual value is to be assigned in sql.c.
从头文件中删除初始化
= NULL
。Remove the initialization
= NULL
from the header file.