23

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.

kristianp
  • 5,496
  • 37
  • 56
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380

12 Answers12

15

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.

Rangad
  • 2,110
  • 23
  • 29
Andy Mortimer
  • 3,619
  • 20
  • 14
  • 4
    Just as a breadcrumb, GPPG can now be found at http://gppg.codeplex.com/ and GPLEX is http://gplex.codeplex.com/ – Bklyn Jun 03 '13 at 19:06
  • gppg is now available on GitHub https://github.com/ernstc/gppg and published to Nuget as YaccLexTools.Gppg (https://www.nuget.org/packages/YaccLexTools.Gppg) – Paul Wheeler Feb 17 '23 at 02:45
11

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?

Emyr
  • 2,351
  • 18
  • 38
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
11

Also look at Antlr, which has C# support.

Kris Erickson
  • 33,454
  • 26
  • 120
  • 175
5

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/

KristoferA
  • 12,287
  • 1
  • 40
  • 62
4

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

Robert Plummer
  • 634
  • 1
  • 5
  • 13
3

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

Tamas Ionut
  • 4,240
  • 5
  • 36
  • 59
  • As for now, it is buggy -- it translates `noassoc` to `right` (so in short it accept the input which should be incorrect according to grammar). – greenoldman Dec 12 '12 at 20:38
3

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.

Magnus Akselvoll
  • 1,257
  • 1
  • 11
  • 16
3

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/

rix0rrr
  • 9,856
  • 5
  • 45
  • 48
3

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

Bartek Szabat
  • 2,904
  • 1
  • 20
  • 13
3

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

Jonathan Parker
  • 6,705
  • 3
  • 43
  • 54
1

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.

greenoldman
  • 16,895
  • 26
  • 119
  • 185
1

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

Kieron
  • 26,748
  • 16
  • 78
  • 122