BNF 到 Lex 到 C 语言的解析器
我正在尝试学习概念以及如何从 BNF 表示法而不是 EBNF 在 C 中创建词法分析器和解析器。我想用 C 语言学习它。
谁能向我解释一下我用来放入 C 中的词法分析器和解析器的 BNF 部分以及将它们放在哪里?比如也可以使用一个例子?我发现在解析器中,你放置了终端、非终端、标记、类型等...
地方
如果不清楚或其他什么,我很抱歉,我的头在这个谢谢你的
。 BNF 我有
<for_statement> ::= FOR <identifier>
IS <expression> BY <expression> TO <expression> DO <statement_list> ENDFOR
词法分析器代码片段
ENDP printf("keyword: ENDP\n");
DECLARATIONS printf("keyword: DECLARATIONS\n");
CODE printf("keyword: CODE\n");
"OF TYPE" printf("keyword: OF TYPE\n");
I am trying to learn the concepts and how to create a lexical analyser and parser in C from BNF notation, not EBNF. I would like to learn it in the language of C.
Can anyone explain to me the parts of the BNF i use to put in the lexical analyser and the parser in C and where to put them please? Such as an example used as well maybe? I have found that in the parser, you put terminals, non terminals, tokens, types, etc...
Sorry if thats unclear or anything, my head is all over the place in this
Thank you
ps. BNF i have
<for_statement> ::= FOR <identifier>
IS <expression> BY <expression> TO <expression> DO <statement_list> ENDFOR
Lexical analyser code snippit
ENDP printf("keyword: ENDP\n");
DECLARATIONS printf("keyword: DECLARATIONS\n");
CODE printf("keyword: CODE\n");
"OF TYPE" printf("keyword: OF TYPE\n");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
前提:我认为了解您所说的概念的更好方法可能是将可用的工具投入使用。您可以从基础知识开始,例如 flex/bison,或者更好-一些最近的免费工具,例如 GOLD。
如今,由于底层状态机的复杂性,词法分析器和解析器不是手动实现的。但是出于学习目的,正如帕斯卡“之父”Niklaus Wirth 所倡导的那样,或者“引导”SW 链工具,或者(过去)出于效率原因,词法分析器/解析器有时是手动实现的。词法分析器通常采用许多大开关的形式,并且可以使用 ungetc() 简单地实现前瞻:
最简单的解析器是自上而下的递归,这要求您的语法为 LL(1)。值得注意的是 Pascal 解析器就是这种类型(当然,Wirth 适当地设计了该语言)。我们需要为每个非终结符提供一个函数,以及 1 个标记的前瞻。解析器变成了一组相互递归的过程(这里完全是虚构的伪代码):
我们总是可以在BFN中重写EBNF,引入服务非终端。
premise: I think that a much better way to learn about the concepts you speak of could be to put the available tools to work. You could start with the basics, like flex/bison, or -better- some recent free tool, like GOLD.
Today lexers and parsers aren't implemented by hand, due to the complexity of the underlying state machines. But for learning purpose, as advocated for instance by Niklaus Wirth, the 'father' of Pascal, or to 'bootstrap' the SW chain tools, or (in the past) also for efficiency reasons, the lexer/parser was sometime implemented by hand. The lexer usually take the form of many big switch(s), and the lookahead can be naively implemented using ungetc():
The simplest parser it's the top-down recursive, that requires your grammar being LL(1). Notably the Pascal parser was of this kind (of course, Wirth designed appropriately the language). We need a function for each nonterminal, and a lookahead of 1 token. The parser becomes a set of mutually recursive procedures (totally fictious pseudocode here):
We can always rewrite EBNF in BFN, introducing service non terminals.