9

I'm looking for a tool that will be able to build a parser (in C#) if I give it a BNF grammar (eg. http://savage.net.au/SQL/sql-2003-2.bnf)

Does such a generator exist?

ilitirit
  • 16,016
  • 18
  • 72
  • 111

5 Answers5

13

Normally BNF grammars are too ambiguous. ANTLR will be probably good for what you are looking for.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • 2
    It seems you are right. I just read this somewhere on the net: "If-then-else cannot be specified unambiguously in a context-free, priority/predicate-free grammar, such as EBNF" – ilitirit Sep 30 '08 at 15:40
12

You will have to tweak the BNF a bit, but TinyPG is a great tool.

Jonathan C Dickinson
  • 7,181
  • 4
  • 35
  • 46
12

The Visual Studio SDK actually ships with lexer and parser generation tools. These are called MPPG and MPLex and are part of the Managed Babel package. While the intention of bundling them with the SDK is to develop language extensions for Visual Studio, they are perfectly usable for creating general AST-emitting parsers.

MPLex and MPPG are based on GPLEX and GPPG (projects of Queensland University of Technology) and are used in a similar fashion to Lex and Yacc. The SDK also contains MSBuild actions for making the parser generation a part of the regular build process.

Here's a screencast showing MPLex and MPPG in action:
http://msdn.microsoft.com/en-us/vstudio/cc837016.aspx

Tormod Fjeldskår
  • 5,952
  • 1
  • 29
  • 47
  • The link seems to be broken. – Kirill Kobelev Dec 06 '16 at 18:49
  • Seems like Microsoft discontinued Managed Babel quite a while ago. At this point, I would probably go with FsLex and FsYacc (http://fsprojects.github.io/FsLexYacc/). It's based on F#, but it compiles to a regular .NET assembly and can be consumed by a C# project if you wish. – Tormod Fjeldskår Dec 07 '16 at 11:58
  • 1
    I would recommend you to investigate this discontinuation and deeply rework your answer, saying that from this to that year VS SDK contained this and now this and that is happening. People will look at your answer multiple times in the coming years. – Kirill Kobelev Dec 07 '16 at 16:41
2

Also take a look at Irony:

http://irony.codeplex.com/

seems very promising

Gian Marco
  • 22,140
  • 8
  • 55
  • 44
1

IronMeta is a C# implementation of Alex Warth's OMeta; it's a packrat PEG (parsing expression grammar; uses biased choice), so grammars can be cleaner than when using a yacc-like LALR system.