将 Bison/Flex/Yacc 集成到 XCode 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6296758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integrating Bison/Flex/Yacc into XCode
提问by David
Is there a simple way for integrating Bison/Flex/Yacc into XCode?
有没有一种简单的方法可以将 Bison/Flex/Yacc 集成到 XCode 中?
I want to write my own language to be parsed, which interacts with my ObjC objects.
我想编写自己的语言进行解析,它与我的 ObjC 对象交互。
But the tools will only take STDIN as input, and will only produce C code, instead of ObjC. They're basically only seem useful for command-line tools, otherwise they need massive pain to override the output every time I regenerate the parser code.
但是这些工具只会将 STDIN 作为输入,并且只会生成 C 代码,而不是 ObjC。它们基本上只对命令行工具有用,否则每次我重新生成解析器代码时,它们都需要大量的痛苦来覆盖输出。
采纳答案by Sherm Pendley
In a nutshell, give your grammar files a .ym extension instead of .y. Xcode will then run Bison with the necessary magic to support Objective-C.
简而言之,给你的语法文件一个 .ym 扩展名而不是 .y。然后 Xcode 将使用必要的魔法运行 Bison 以支持 Objective-C。
Oddly enough, I found this question because I wanted to do the same thing. I remembered a discussion about it on Apple's cocoa-dev mailing list, but this question came up higher in my Google search. I found the discussion I remembered though, on CocoaBuilder- there's a pretty extensive example at the end. I hope it helps both of us, LOL!
奇怪的是,我发现这个问题是因为我想做同样的事情。我记得在 Apple 的 cocoa-dev 邮件列表上有一个关于它的讨论,但这个问题在我的谷歌搜索中出现的更高。我在 CocoaBuilder 上找到了我记得的讨论- 最后有一个非常广泛的例子。我希望它对我们俩都有帮助,大声笑!
(Edit) Beats the heck out of me how anyone ever managed to find this out, by the way - I stillhaven't found any official documentation about it.
(编辑)顺便说一句,有人设法找到了这一点,这让我大吃一惊——我还没有找到任何关于它的官方文档。
回答by davehayden
Xcode does know how to process yacc and flex files, and will automatically compile the generated code files into your project. The trick is figuring out which extensions it expects. In Xcode 4, click on the top-level item in the source list to get the project settings. Click your target in the next list, then Build Rules in the table view header. Scroll down to "System Lex rule" and "System Yacc rule", then hover the cursor over "source files" in each of those to get a tool tip with the extensions it uses for that rule. (…What? That wasn't obvious?)
Xcode 确实知道如何处理 yacc 和 flex 文件,并且会自动将生成的代码文件编译到您的项目中。诀窍是弄清楚它期望哪些扩展。在 Xcode 4 中,单击源列表中的顶级项目以获取项目设置。在下一个列表中单击您的目标,然后在表视图标题中单击构建规则。向下滚动到“系统 Lex 规则”和“系统 Yacc 规则”,然后将光标悬停在每个文件中的“源文件”上,以获取带有用于该规则的扩展名的工具提示。(……什么?这不明显?)
For Lex, it's: *.l *.lm *.lmm *.lpp *.lp *.lxx
对于 Lex,它是: *.l *.lm *.lmm *.lpp *.lp *.lxx
For Yacc, it's: *.y *.ym *.ymm *.ypp *.yp *.yxx
对于 Yacc,它是: *.y *.ym *.ymm *.ypp *.yp *.yxx
If you have any Objective-C code in your lex/yacc files, you'll need to use the .?m extension to tell Xcode to generate a .m file; likewise, .?mm for Objective-C++. (I'm guessing .?pp, .?p, and .?xx are all C++.) From what I've seen, this Just Works, no Makefile needed. If you want to keep the .flex extension, you can click the "Copy to Target" button in the Lex rule and make a new rule for that extension.
如果你的 lex/yacc 文件中有任何 Objective-C 代码,你需要使用 .?m 扩展名来告诉 Xcode 生成一个 .m 文件;同样,.?mm 用于Objective-C++。(我猜 .?pp、.?p 和 .?xx 都是 C++。)据我所知,这个 Just Works,不需要 Makefile。如果要保留 .flex 扩展名,可以单击 Lex 规则中的“复制到目标”按钮并为该扩展名创建新规则。
Now the "fun" part is trying to integrate yacc into your code. By default, the yyparse() function which makes the whole thing go expects input from STDIN unless you jump through a bunch of hoops to make it work otherwise. If you're dead-set on using yacc, there's plenty of info on the web about how to bend it to your whims. But you might wind up with fewer keyboard dents on your forehead if you use a modern parser generator like lemon. The syntax is based on yacc's, and it's LALR(1) (whatever that means) so everything you know about shift/reduce conflicts still holds. I found it much easier to work with than yacc/flex.
现在“有趣”的部分是尝试将 yacc 集成到您的代码中。默认情况下,使整个过程运行的 yyparse() 函数期望来自 STDIN 的输入,除非您跳过一堆箍以使其正常工作。如果您对使用 yacc 一窍不通,网上有很多关于如何随心所欲地使用它的信息。但是,如果您使用像柠檬这样的现代解析器生成器,您额头上的键盘凹痕可能会更少。语法基于 yacc,它是 LALR(1)(不管这意味着什么),所以你知道的关于 shift/reduce 冲突的一切仍然有效。我发现它比 yacc/flex 更容易使用。
Edit: I seem to recall that Xcode didn't add the .l file to the build phase automatically, but it did add the .y file. Check your build phases by clicking the top-level item in the Xcode source list, click you target, click the Build Phases table header, then expand the Compile Sources row. If your .l file isn't in there, drag it in from the source list.
编辑:我似乎记得 Xcode 没有自动将 .l 文件添加到构建阶段,但它确实添加了 .y 文件。通过单击 Xcode 源列表中的顶级项目检查您的构建阶段,单击您的目标,单击 Build Phases 表标题,然后展开 Compile Sources 行。如果您的 .l 文件不在其中,请将其从源列表中拖入。
回答by phatmann
I found a great example: https://github.com/epatel/ParserTest. It is 3 years old but works in Xcode 6 and 7.
我找到了一个很好的例子:https: //github.com/epatel/ParserTest。它已有 3 年历史,但可在 Xcode 6 和 7 中运行。