C 中的字符串操作
我正在帮助我的侄子完成他的 C 实验室作业,这是一个字符串操作作业并应用 Wang 的算法。
这是输入的 BNF 表示。
<s> ::= <l> # <r>
<l> ::= <list>| ε
<r> ::= <list>| ε
<list> ::= <f>|<f> , <list>
<f> ::= <letter>| - <f>| (<f><op><f>)
<op> ::= & | | | >
<letter> ::= A |... | Z
在 C 中处理和解析此类输入的最佳实践是什么?如何在不使用 struct 的情况下解析此结构?提前致谢。
I am helping my nephew for his C lab homework, it is a string manipulation assignment and applying Wang's algorithm.
Here is the BNF representation for the input.
<s> ::= <l> # <r>
<l> ::= <list>| ε
<r> ::= <list>| ε
<list> ::= <f>|<f> , <list>
<f> ::= <letter>| - <f>| (<f><op><f>)
<op> ::= & | | | >
<letter> ::= A |... | Z
What is the best practice to handle and parse this kind of input in C? How can I parse this structure without using struct
? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最直接的方法是使每个规则(或“产生式”)成为一个函数。这称为“递归下降”解析器。
编写两个例程来查看并获取下一个字符。
对于每个规则,这将为您提供一些看起来像这样的代码(伪代码):
等等。
总体思路:
The most straightforward approach is to make every rule (or "production") a function. This is called a "recursive descent" parser.
Write two routine that will peek at and get the next character as well.
This will give you some code that looks something like this (in pseudocode):
and so forth, for each rule.
The general idea:
由于您已经有了 BNF,解析此类输入的最简单方法是使用解析器生成器。但由于这是家庭作业,我不确定是否允许使用发电机。
尽管如此,您也可以使用手写的解析器。只需搜索递归下降解析器......
As you already have your BNF, the simplest way to parse this kind of input would be to use a parser generator. But due to this being homework, I'm not sure wether using a generator is allowed.
Nevertheless, you can also use a hand-written parser. Just do a search for recursive descent parsers...