以功能纯方式生成不可变的具体语法树的适当数据结构或算法是什么?
给定一个LL(1)语法,以功能纯方式生成不可变的具体语法树的适当数据结构或算法是什么?请随意用您喜欢的任何语言编写示例代码。
我的想法
symbol : either a token or a node result : success or failure token : a lexical token from source text value -> string : the value of the token type -> integer : the named type code of the token next -> token : reads the next token and keeps position of the previous token back -> token : moves back to the previous position and re-reads the token node : a node in the syntax tree type -> integer : the named type code of the node symbols -> linkedList : the symbols found at this node append -> symbol -> node : appends the new symbol to a new copy of the node
这是我一直在思考的一个想法。这里的主要问题是处理语法错误。 我的意思是我可以在第一个错误处停止,但这似乎不对。
let program token =
sourceElements (node nodeType.program) token
let sourceElements node token =
let (n, r) = sourceElement (node.append (node nodeType.sourceElements)) token
match r with
| success -> (n, r)
| failure -> // ???
let sourceElement node token =
match token.value with
| "function" ->
functionDeclaration (node.append (node nodeType.sourceElement)) token
| _ ->
statement (node.append (node nodeType.sourceElement)) token
请注意
我将为最佳答案提供丰厚的赏金,所以不要感到匆忙。与显示代码或包含详细解释的答案相比,仅发布链接的答案的权重较小。
最后说明
我对这类东西真的很陌生,所以不要害怕称我为傻瓜。
Given a LL(1) grammar what is an appropriate data structure or algorithm for producing an immutable concrete syntax tree in a functionally pure manner? Please feel free to write example code in whatever language you prefer.
My Idea
symbol : either a token or a node result : success or failure token : a lexical token from source text value -> string : the value of the token type -> integer : the named type code of the token next -> token : reads the next token and keeps position of the previous token back -> token : moves back to the previous position and re-reads the token node : a node in the syntax tree type -> integer : the named type code of the node symbols -> linkedList : the symbols found at this node append -> symbol -> node : appends the new symbol to a new copy of the node
Here is an idea I have been thinking about. The main issue here is handling syntax errors.
I mean I could stop at the first error but that doesn't seem right.
let program token =
sourceElements (node nodeType.program) token
let sourceElements node token =
let (n, r) = sourceElement (node.append (node nodeType.sourceElements)) token
match r with
| success -> (n, r)
| failure -> // ???
let sourceElement node token =
match token.value with
| "function" ->
functionDeclaration (node.append (node nodeType.sourceElement)) token
| _ ->
statement (node.append (node nodeType.sourceElement)) token
Please Note
I will be offering up a nice bounty to the best answer so don't feel rushed. Answers that simply post a link will have less weight over answers that show code or contain detailed explanations.
Final Note
I am really new to this kind of stuff so don't be afraid to call me a dimwit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要将某些内容解析为抽象语法树。
在纯函数式编程语言 Haskell 中,您可以使用解析器组合器来表达您的语法。这里有一个解析小型表达式语言的示例:
编辑使用单子样式来匹配 Graham Hutton 的书
这里有一个示例运行:
要了解有关解析器组合器的更多信息,请参阅 Graham Hutton 的书“Programming in Haskell” 或 “真实世界 Haskell”第 16 章。
许多解析器组合器库可以按照您的意愿与不同的标记类型一起使用。令牌流通常表示为令牌列表
[Token]
。You want to parse something into an abstract syntax tree.
In the purely functional programming language Haskell, you can use parser combinators to express your grammar. Here an example that parses a tiny expression language:
EDIT Use monadic style to match Graham Hutton's book
Here an example run:
To learn more about parser combinators, see for example chapter 8 of Graham Hutton's book "Programming in Haskell" or chapter 16 of "Real World Haskell".
Many parser combinator library can be used with different token types, as you intend to do. Token streams are usually represented as lists of tokens
[Token]
.一定要检查一元解析器组合器方法;我已经在博客中介绍了它 C# 和 F# 中。
Definitely check out the monadic parser combinator approach; I've blogged about it in C# and in F#.
Eric Lippert 的 博客系列不可变的二叉树可能会有所帮助。显然,您需要一棵非二叉树,但它会给您总体思路。
Eric Lippert's blog series on immutable binary trees may be helpful. Obviously, you need a tree which is not binary, but it will give you the general idea.