简体   繁体   English

lex&yacc多定义错误

[英]lex & yacc multiple definition error

I want to make code scanner and parser but I don't know why this error happens just by looking at the error log. 我想制作代码扫描器和解析器,但我不知道为什么仅通过查看错误日志就可以发生此错误。 The scanner takes the sample code and divides it into tokens, then returns what each of the tokens in the code is doing.The parser receives the values returned from the scanner and parses the code according to the rules. 扫描程序获取示例代码并将其划分为令牌,然后返回代码中每个令牌的工作。解析器接收从扫描程序返回的值,并根据规则解析代码。 It checks validity of grammar of sample code. 它检查示例代码语法的有效性。

and finally this my error 最后这是我的错误

lex.yy.o: In function main:
lex.yy.c:(.text+0x1d2a): multiple definition of main
y.tab.o:y.tab.c:(.text+0x861): first defined here
collect2: error: ld returned 1 exit status

You have defined main in both your files, but C only allows a single definition of main in a program, which is what the linker error is telling you. 您已经在两个文件中都定义了main ,但是C仅在程序中允许main的单个定义,这就是链接器错误告诉您的。

The main in your scanner file has an invalid prototype (C hasn't allowed function definitions without a return type for almost 20 years) and also calls yylex only once, which is not going to do much. 扫描程序文件中的main包含无效的原型(C将近20年都不允许使用没有返回类型的函数定义),并且仅调用yylex一次,这不会做太多事情。 So it seems pretty well pointless. 因此,这似乎毫无意义。 If you want to debug your scanner without using the parser,byou can link the scanner with -lfl ; 如果要在不使用解析器的情况下调试扫描仪,则可以将扫描仪与-lfl链接; that library includes a definition of main which repeatedly calls yylex until end of file is signalled. 该库包含main的定义,该定义重复调用yylex直到发出信号通知文件末尾为止。

Instead of scattering printf calls through your scanner, you can just build a debugging version of the scanner using the --debug flag when you generate the scanner. 无需通过扫描程序散布printf调用,而是可以在生成扫描程序时使用--debug标志构建扫描程序的调试版本。 That will print out a trace of all scanner actions. 这将打印出所有扫描仪动作的痕迹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM