使用 Happy (Haskell) 从 yacc 语法生成 Fortran 77 解析器

发布于 2024-07-25 19:24:49 字数 983 浏览 1 评论 0 原文

我偶然发现了以下 F77 yacc 语法: http://yaxx.cvs.sourceforge.net/viewvc/yaxx/yaxx/fortran/fortran.y?revision=1.3&view=markup

如何使用 Happy 从此文件中创建 Fortran 77 解析器?

为什么有一些C?/C++? .y 文件中的代码?

更新:感谢您的回复!

我已经使用两种新方法一段时间了:从

  1. 与标题为 参数化 Fortran
  2. BNFC

我已经可以解析简单的代码摘录了。 如果本世纪内出现一些可用的东西,我会让人们知道 ^__^" 呵呵。

P/S: 想看看我是否可以自己收集足够的动力来启动一个自动微分引擎的项目来取代我们目前仅依赖二进制文件作为初始阶段的娱乐:我正在观看 Love Shuffle 这是一部非常有趣的日剧!

I have stumbled upon the following F77 yacc grammar: http://yaxx.cvs.sourceforge.net/viewvc/yaxx/yaxx/fortran/fortran.y?revision=1.3&view=markup.

How can I make a Fortran 77 parser out of this file using Happy?

Why is there some C?/C++? code in that .y file?

UPDATE: Thank you for your replies!

I've been playing with two fresh approaches for a while now:

  1. extracting and modifiying the parser from the source code package bundled with a paper titled Parametric Fortran,
  2. writing a grammar from scratch with the help of BNFC.

I've got both to parse simple code excerpts already. I'll keep people in the know should something usable come into existence within this century ^__^" hehe.

P/S: Want to see whether I could gather enough momentum on my own to initiate a project for an automatic differentiation engine to replace a binary-only one we depend on for the time being. For entertainment at the initial stages: I'm watching Love Shuffle! It's a very enjoyable J-Drama! Highly recommendable ...

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

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

发布评论

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

评论(4

人海汹涌 2024-08-01 19:24:49

C 是读入语法时减少堆栈的语义操作。这些操作采用 C 语言,因为该定义适用于生成 C 源文件的 Bison/Yacc。

如果您想使用 Happy,请将 BNF 移植到 Happy 定义语法并在 Haskell 中编写语义。

然而,这只是获取有用信息的冰山一角

如果您还没有副本,请购买龙之书(编译器:原理、技术和工具,作者:Aho、Lam、Sethi、Ullman - Pearson)

The C is the semantic action for reducing the stack when the syntax is read in. These actions are in C because the definition is intended for Bison/Yacc which produces a C source file.

If you want to use Happy, port the BNF to the Happy definition syntax and write your semantics in Haskell.

Just the tip of the iceberg for getting anything useful however.

If you don't have a copy already, invest in the Dragon Book (Compilers: Principles, Techniques & tools by Aho, Lam, Sethi, Ullman - Pearson)

醉南桥 2024-08-01 19:24:49

为什么其他答案在一般意义上是正确的,因为您需要编写自己的操作来执行任何有意义的操作,您链接到的 Yacc 定义实际上没有与语法规则。 它的作用是定义 yyerror 函数和一些用于根据标记类型从 yylval 中提取值的代码。

如果您不知道 yyerror/yylval 是什么,您应该阅读 bison/flex 教程。 如果你对此更认真的话,《龙》书也是一个很好的资源。 网上还流传着一些来自斯坦福大学关于编译器的课程的优秀讲义,这些讲义都是基于这本书的。

Why the other answers are true in the general sense, in that you'll need to write your own actions to do anything meaningful the Yacc definition that you linked to actually doesn't have any actions associated with the grammar rules. What it does is that it defines the yyerror function and some code for extracting values from yylval based on the token type.

If you have no clue what yyerror/yylval are about you should read a bison/flex tutorial. The Dragon book is also a good resource if you're more serious about this. There are also some excellent handouts from a Stanford course on compilers floating around the Net, which are based on the book.

痴情 2024-08-01 19:24:49

您需要构建一个 AST,该 AST 的构建方式与 Yacc 文件中的 C 片段相同。

You'll need an AST to build that can be constructed in an equivalent way to the C fragments in the Yacc file.

数理化全能战士 2024-08-01 19:24:49

使用 BNFC 并从头开始编写您自己的语法! BNFC 创造了奇迹,您可以完全按照您的意愿进行解析。

Use BNFC and write your own grammar from scratch! BNFC works wonders and you could do your parsing exactly as you desire.

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