views:

514

answers:

4

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 ...

+2  A: 

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)

Aiden Bell
Thank you I have got myself a copy already *^o^*!!
Cetin Sert
+1  A: 

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

Don Stewart
I think that the semantics of the language in which the actions are written to build the AST will heavily impact the way they are written. I would not suggest copying verbatim into Haskell from C. Doing so will result in inefficiencies and confusion. The best approach would be to study the ins-and-outs of ASTs/parsers/compilers and then apply that in Haskell directly without looking too much at existing production actions in C.
Aiden Bell
Cetin Sert
+2  A: 

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.

oggy
+1 for following the link :)
Aiden Bell
Thank you I have got myself a copy of the book already! As for the Stanford papers, I think I'll check them later once I've time *^o^*!!
Cetin Sert
+1  A: 

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

Cetin Sert
Any public source for your work? I am wanting to build exactly the same thing so that I can use some fortran code for work.
leon