使用 flex/bison 构建类似 Lisp/Scheme 的解析树

发布于 2024-09-07 13:46:53 字数 403 浏览 2 评论 0原文

我试图解析简单的 Lisp/scheme 代码

E.g. (func a (b c d) )

并从中构建一棵树, 我可以在 C 中进行解析而不使用 bison (即,仅使用 flex 返回标记并通过递归构建树)。 但是,使用 bison 语法,我不确定在哪里添加代码 构建列表(即将哪个规则与累积终端关联 符号以及将构建列表链接到父节点的位置)。

我的语法与这里的类似: yacc 中的 Lisp 语法 语法正确,可以识别该代码。

I was trying to parse simple Lisp/scheme-like code

E.g. (func a (b c d) )

and build a tree from it,
I could do the parsing in C without using bison (i.e, using only
flex to return tokens and building the tree with recursion).
But, with bison grammar, I am not sure where to add the code to
build the list (i.e, which rule to associate with accumulating terminal
symbols and where to link a built list to parent node).

My grammar is similar to the one here:
Lisp grammar in yacc
the grammar is correct and can recognize the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百合的盛世恋 2024-09-14 13:46:53

您是否尝试过放置代码以将元素添加到每个原子中的当前列表,并放置代码以在处理括号时管理列表树?这似乎是最简单的方法,除非遇到其他问题:

listend: members ')'        { cur = cur->parent; }
       | ')'                { cur = cur->parent; }
       ;

list: '(' listend           { cur = newList(cur);}
    ;

atom: ID                    { appendAtom(cur, "ID"); }
    | NUM                   { appendAtom(cur, "NUM");}
    | STR                   { appendAtom(cur, "STR");}
    ;

这假设您在每个列表结构中保留一个父点。

Have you tried placing the code to add an element to the current list in each atom, and code to manage a tree of lists when you process the brackets? It seems the easiest way unless you run into other problems:

listend: members ')'        { cur = cur->parent; }
       | ')'                { cur = cur->parent; }
       ;

list: '(' listend           { cur = newList(cur);}
    ;

atom: ID                    { appendAtom(cur, "ID"); }
    | NUM                   { appendAtom(cur, "NUM");}
    | STR                   { appendAtom(cur, "STR");}
    ;

This assumes that you are keep a parent point in each list struct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文