Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lex/Yacc for C#?

Tags:

c#

parsing

yacc

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.

like image 904
Roger Lipscombe Avatar asked Feb 12 '09 09:02

Roger Lipscombe


People also ask

What is lex and yacc programs?

Lex and yacc are tools used to generate lexical analyzers and parsers. I assume you can program in C and understand data structures such as linked-lists and trees. The introduction describes the basic building blocks of a compiler and explains the interaction between lex and yacc.

What is lex and yacc commands?

This section contains example programs for the lex and yacc commands. Specifies the lex command specification file that defines the lexical analysis rules. Specifies the yacc command grammar file that defines the parsing rules, and calls the yylex subroutine created by the lex command to provide input.

What is yylex () in C?

yylex() returns a value indicating the type of token that has been obtained. If the token has an actual value, this value (or some representation of the value, for example, a pointer to a string containing the value) is returned in an external variable named yylval.


3 Answers

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.

like image 74
Andy Mortimer Avatar answered Oct 07 '22 20:10

Andy Mortimer


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?

like image 11
Anton Gogolev Avatar answered Oct 07 '22 19:10

Anton Gogolev


Also look at Antlr, which has C# support.

like image 11
Kris Erickson Avatar answered Oct 07 '22 19:10

Kris Erickson