如何解析 lambda 项
我想解析 lambda 演算。我不知道如何解析该术语并尊重括号优先级。例如:
(lx ly (x(xy)))(lx ly xxxy)
我找不到好的方法来做到这一点。我只是看不到适应的算法。 术语由具有类型(应用程序、抽象、变量)的结构表示,并且 “结构术语”类型的左右组件。
知道怎么做吗?
编辑
很抱歉再次打扰您,但我真的很想理解。你能检查一下函数“表达式()”让我知道我是否正确。
Term* expression(){
if(current==LINKER){
Term* t = create_node(ABSTRACTION);
get_next_symbol();
t->right = create_node_variable();
get_next_symbol();
t->left = expression();
}
else if(current==OPEN_PARENTHESIS){
application();
get_next_symbol();
if(current != CLOSE_PARENTHESIS){
printf("Error\n");
exit(1);
}
}
else if(current==VARIABLE){
return create_node_variable();
}
else if(current==END_OF_TERM)
{
printf("Error");
exit(1);
}
}
谢谢
I would like to parse a lambda calculus. I dont know how to parse the term and respect parenthesis priority. Ex:
(lx ly (x(xy)))(lx ly xxxy)
I don't manage to find the good way to do this. I just can't see the adapted algorithm.
A term is represented by a structure that have a type (APPLICATION, ABSTRACTION, VARIABLE) and
a right and left component of type "struc term".
Any idea how to do this ?
EDIT
Sorry to disturb you again, but I really want to understand. Can you check the function "expression()" to let me know if I am right.
Term* expression(){
if(current==LINKER){
Term* t = create_node(ABSTRACTION);
get_next_symbol();
t->right = create_node_variable();
get_next_symbol();
t->left = expression();
}
else if(current==OPEN_PARENTHESIS){
application();
get_next_symbol();
if(current != CLOSE_PARENTHESIS){
printf("Error\n");
exit(1);
}
}
else if(current==VARIABLE){
return create_node_variable();
}
else if(current==END_OF_TERM)
{
printf("Error");
exit(1);
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以通过将应用程序与其他表达式分开来简化:
与您的方法的唯一区别是应用程序表示为表达式列表,因为
abcd
可以隐式读取为((( ab)c)d)
因此在解析时您可能会将其存储为abcd
。基于这个语法,可以使用单个前瞻字符创建一个简单的递归下降解析器:
当然,根符号是 APPL。作为解析后步骤,您可以将 APPL = EXPR 列表转换为应用程序树。递归下降非常简单,如果您愿意,您可以轻松地将其转变为具有显式堆栈的命令式解决方案。
The can be simplified by separating the application from other expressions:
The only difference with your approach is that the application is represented as a list of expressions, because
abcd
can be implicitly read as(((ab)c)d)
so you might at well store it asabcd
while parsing.Based on this grammar, a simple recursive descent parser can be created with a single character of lookahead:
The root symbol is APPL, of course. As a post-parsing step, you can turn your APPL = list of EXPR into a tree of applications. The recursive descent is so simple that you can easily turn into an imperative solution with an explicit stack if you wish.