如何使用 EBNF 语法解析注释
在定义语言解析器的语法时,如何处理文本中可能出现的注释(例如 /* .... */)等内容?
当事物结构化时,从标签内的标签构建语法似乎效果很好,但注释似乎会抛出一切。
您只需分两步解析文本吗?首先删除这些项目,然后再拆开代码的实际结构?
谢谢
When defining the grammar for a language parser, how do you deal with things like comments (eg /* .... */) that can occur at any point in the text?
Building up your grammar from tags within tags seems to work great when things are structured, but comments seem to throw everything.
Do you just have to parse your text in two steps? First to remove these items, then to pick apart the actual structure of the code?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,词法分析器在主语法范围之外处理注释。实际上,它们(通常)被视为空白。
Normally, comments are treated by the lexical analyzer outside the scope of the main grammar. In effect, they are (usually) treated as if they were blanks.
一种方法是使用单独的词法分析器。另一种更灵活的方法是使用对当前上下文有效的隐式空格前缀修改所有类似标记的条目(关键字、词汇元素等)。这就是大多数现代 Packrat 解析器处理空格的方式。
One approach is to use a separate lexer. Another, much more flexible way, is to amend all your token-like entries (keywords, lexical elements, etc.) with an implicit whitespace prefix, valid for the current context. This is how most of the modern Packrat parsers are dealing with whitespaces.