fsyacc:允许用语言定义运算符

发布于 2024-11-09 16:19:04 字数 611 浏览 0 评论 0 原文

fsyacc 是否有某种方法来处理解析时引入的运算符?我正在尝试为 Kaleidscope 构建一个解析器,这是一种玩具语言,用作 LLVM 教程。万花筒允许定义运算符和优先级。例如:

# Logical unary not.
def unary!(v)
  if v then
    0
  else
    1;

# Define > with the same precedence as <.
def binary> 10 (LHS RHS)
  RHS < LHS;

# Binary "logical or", (note that it does not "short circuit")
def binary| 5 (LHS RHS)
  if LHS then
    1
  else if RHS then
    1
  else
    0;

# Define = with slightly lower precedence than relationals.
def binary= 9 (LHS RHS)
  !(LHS < RHS | LHS > RHS);

Does fsyacc have some way to deal with operators that are introduced at parse time? I'm trying to build a parser for Kaleidoscope which is a toy language used as an example for the LLVM tutorial. Kaleidoscope allows operators to be defined along with precedence levels. Eg:

# Logical unary not.
def unary!(v)
  if v then
    0
  else
    1;

# Define > with the same precedence as <.
def binary> 10 (LHS RHS)
  RHS < LHS;

# Binary "logical or", (note that it does not "short circuit")
def binary| 5 (LHS RHS)
  if LHS then
    1
  else if RHS then
    1
  else
    0;

# Define = with slightly lower precedence than relationals.
def binary= 9 (LHS RHS)
  !(LHS < RHS | LHS > RHS);

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

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

发布评论

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

评论(1

雅心素梦 2024-11-16 16:19:04

fsyacc 没有魔法来帮助动态优先级(这在大多数解析工具中很少见),但这里描述的策略相同

http://llvm.org/docs/tutorial/LangImpl2.html#parserbinops

是你所需要的,我想(我只看了一眼)。

There is no magic in fsyacc to help with the dynamic precedence (this is rare in most parsing tools), but the same strategy described here

http://llvm.org/docs/tutorial/LangImpl2.html#parserbinops

is what you need, I think (I only glanced).

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