4

I'm looking for BNF grammar for C# v4 that I can feed to fsyacc or if I can't find that an EBNF based parser generator that can output F#.

EDIT: I'm not looking to write a parser for C# but an experimental parser for a very similar grammar and since there's a huge literal overlap between the two grammars I find it easier to start with the C# grammar and simply change the bits that deviates. However the only grammar for C#v4 I've found is a EBNF

an alternative answer would be a converter from EBNF to BNF

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • 3
    I don't think you'll find anything solid. The best option I can think of is to use the parser from the C# compiler in Mono. You should be able to compile that and use that from F# (or modify it to be more F# friendly). – Tomas Petricek Sep 12 '11 at 20:25
  • 1
    @Tomas: Thanks for pointing it out. Guess I'll rewrite the EBNF grammar I have and use fsyacc. I need to be able to change the grammar. I'm not writing a C# compiler but an experimental compiler for a very similar grammar. – Rune FS Sep 13 '11 at 08:07
  • @TomasPetricek If you write that as an answer I'll reward you the answer. I ended up extending the mono C# compiler instead. Was a lot less work than I would have thought – Rune FS Dec 08 '11 at 10:22
  • I'm glad that the approach worked for you! I added is as an answer (with a few more details). – Tomas Petricek Dec 08 '11 at 22:24
  • There a grammar for C# v5 in the appendices of the C# specification: https://www.microsoft.com/en-us/download/confirmation.aspx?id=7029. See also http://stackoverflow.com/q/2575044/7255. – Steve Pitchers Sep 23 '15 at 19:04

3 Answers3

2

Extending my previous comment - I don't think there's any solid grammar for C# that could be directly usable. There are some examples, but they probably won't cover all new features (like LINQ) or won't be very well tested.

The best option that I can think of is to use the parser from the C# compiler in Mono. This is written in C#, open-source, includes all recent features (and is also fairly solid). For more information, see Mono C# compiler. You should be able to compile that and use that from F# (or modify it to be more F# friendly).

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
1

I found some grammars here; they seem to include up to C# 4.0.

user541686
  • 205,094
  • 128
  • 528
  • 886
1

According to Wikipedia, Coco/R is an EBNF based parser generator. And according to their homepage, there is an F# port of Coco/R (last updated 2007-07-23, though, so I'd expect some breaking changes). If you end up checking it out, I'd be interested in your experience.

I did find a BNF grammer for C# v2: http://www.devincook.com/GOLDParser/grammars/index.htm

Stephen Swensen
  • 22,107
  • 9
  • 81
  • 136