从单独的程序调用 lex/yacc
我一直在阅读 lex/yacc 。这些书籍和示例并不难理解。事实上,整个概念一清二楚。但有一个例外。 Lex/yacc 似乎生成独立的程序。如果我想将它们作为解析器函数调用怎么办? Yacc 似乎生成了一个 main 函数,那么我如何从自己的函数中调用而不发生冲突。
目前我还不清楚它们如何处理文件输入。我可以从文件中抓取一行并将其发送到解析器,但是如果您的解析器正在寻找多行结构(例如用大括号括起来的块)怎么办?
我意识到我对这两件事都很愚蠢,但我们将不胜感激。谢谢。
I've been reading up on lex/yacc. The books and examples are not hard to follow. In fact, the whole concept is clear as a bell. With one exception. Lex/yacc seem to generate standalone programs. What if I wanted to call them as a parser function? Yacc seems to generate a main function, so how would I call from my own without conflicts.
I'm also unclear at this point how they operate on file input. I'm okay with grabbing a line from a file and sending it to the parser, but what if your parser is looking for a multi-line structure like a block enclosed in braces?
I realize I'm being stupid about both of these, but any help is appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
lexx / yacc 组合的输入是通过名为 yyin 的 FILE * 进行的。
默认为 stdin - 围绕 lex.yy.c 进行搜索以找到它,
如果您将 FILE * 分配给 yyin,词法分析器将从该文件中读取,您可以执行类似
yyin = fopen ("parseme", "rt"); 的操作;
在调用 yyparse() 之前,通常在 main() 中。
input to the lexx / yacc combo is via a FILE * called yyin.
this defaults to stdin - trawl around lex.yy.c to find it
if you assign a FILE * to yyin, the lexer will read from that file, you do something like
yyin = fopen ("parseme", "rt");
before the call to yyparse(), typically in your main().
这两个程序都会生成
yylex()
和yyparse()
函数,但都不会生成 main 函数。您必须在某处添加您自己的main()
函数。许多教程将它们放入 .l 或 .y 文件中,但您可以随意将它们放在您想要的位置。Both programs generate the
yylex()
andyyparse()
functions, but none of them generates a main function. You have to add your ownmain()
function somewhere. Many tutorials place them into the .l or .y file, but you are free to place them where ever you want.