22
votes

Actually, maybe not full-blown Lex/Yacc. I'm implementing a command-interpreter front-end to administer a webapp. I'm looking for something that'll take a grammar definition and turn it into a parser that directly invokes methods on my object. Similar to how ASP.NET MVC can figure out which controller method to invoke, and how to pony up the arguments.

So, if the user types "create foo" at my command-prompt, it should transparently call a method:

private void Create(string id) { /* ... */ }

Oh, and if it could generate help text from (e.g.) attributes on those controller methods, that'd be awesome, too.

12

12 Answers

15
votes

I've done a couple of small projects with GPLEX/GPPG, which are pretty straightforward reimplementations of LEX/YACC in C#. I've not used any of the other tools above, so I can't really compare them, but these worked fine.

GPPG can be found here and GPLEX here.

That being said, I agree, a full LEX/YACC solution probably is overkill for your problem. I would suggest generating a set of bindings using IronPython: it interfaces easily with .NET code, non-programmers seem to find the basic syntax fairly usable, and it gives you a lot of flexibility/power if you choose to use it.

11
votes

I'm not sure Lex/Yacc will be of any help. You'll just need a basic tokenizer and an interpreter which are faster to write by hand. If you're still into parsing route see Irony.

As a sidenote: have you considered PowerShell and its commandlets?

11
votes

Also look at Antlr, which has C# support.

5
votes

Still early CTP so can't be used in production apps but you may be interested in Oslo/MGrammar: http://msdn.microsoft.com/en-us/oslo/

4
votes

Jison is getting a lot of traction recently. It is a Bison port to javascript. Because of it's extremely simple nature, I've ported the jison parsing/lexing template to php, and now to C#. It is still very new, but if you get a chance, take a look at it here: https://github.com/robertleeplummerjr/jison/tree/master/ports/csharp/Jison

3
votes

If you don't fear alpha software and want an alternative to Lex / Yacc for creating your own languages, you might look into Oslo. I would recommend you to sit through session recordings of sessions TL27 and TL31 from last years PDC. TL31 directly addresses the creation of Domain Specific Languages using Oslo.

3
votes

Coco/R is a compiler generator with a .NET implementation. You could try that out, but I'm not sure if getting such a library to work would be faster than writing your own tokenizer.

http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/

3
votes

I would suggest csflex - C# port of flex - most famous unix scanner generator.

3
votes

I believe that lex/yacc are in one of the SDKs already (i.e. RTM). Either Windows or .NET Framework SDK.

3
votes

Gardens Point Parser Generator here provides Yacc/Bison functionality for C#. It can be donwloaded here. A usefull example using GPPG is provided here

1
votes

As Anton said, PowerShell is probably the way to go. If you do want a lex/ yacc implementation then Malcolm Crowe has a good set.

Edit: Direct Link to the Compiler Tools

1
votes

Just for the record, implementation of lexer and LALR parser in C# for C#:

http://code.google.com/p/naive-language-tools/

It should be similar in use to Lex/Yacc, however those tools (NLT) are not generators! Thus, forget about speed.