views:

191

answers:

3

What I'm especially interested in is the ability to define the grammar in the code as ordinary code without any unnecessary cruft.

I'm aware I could use IronPython. I don't want to.

UPDATE:

To further explain what I'm looking for, I'm including some sample pyparsing code. This is an incomplete parser to convert emacs shortcut keys to more conventional notation. This example is, of course, small enough that string functions would suffice, but it's just to show the cleanness and conciseness of pyparsing.

from pyparsing import Literal, OneOrMore, Optional, Word, printables, replaceWith

CTRL_MODIFIER = Literal('C').setParseAction(replaceWith('Ctrl'))
META_MODIFIER = Literal('M').setParseAction(replaceWith('Alt'))
MODIFIER = CTRL_MODIFIER | META_MODIFIER # Note operator overloading

SEPARATOR = Literal('-').setParseAction(replaceWith('+'))

MODIFIER_LIST = OneOrMore(MODIFIER + SEPARATOR)

KEY = Word(printables) # This is a "word" composed of any number of printable characters.

# The lambda functions here just join the tokens with the literal string 
# on which .join is called.
STROKE = (Optional(MODIFIER_LIST) + KEY).setParseAction(
    lambda tokens: ' '.join([str(token) for token in tokens]))
BINDING = OneOrMore(STROKE).setParseAction(
    lambda tokens: ', '.join([str(token) for token in tokens]))

# Example usage:
# >>> BINDING.transformString('M-/')
# Alt + /
# >>> BINDING.transformString('C-x C-f')
# Ctrl + x, Ctrl + f
# >>> BINDING.transformString('C-x f')
# Ctrl + x, f
# >>> BINDING.transformString('C-x M-c M-butterfly')
# Ctrl + x, Alt + c, Alt + butterfly

I would like to be able to write grammars in .NET with as much ease in as few lines.

+1  A: 

Project OSLO, which won't be released for another few years, and is going to be an over-engineered version of pyparsing.

Matt Briggs
haha I can come up with answers like this as well ;^)
Toad
+2  A: 

Take a look at: Irony It allows you to define your grammar in your c# code

Toad
This is definitely helpful. It's quite a bit more complicated than pyparsing, but still pretty cool.
Daniel Straight
What this really lacks is an easy way to specify parse actions. The grammar specification is almost as simple as in pyparsing, but there's a lot of complication involved in telling it what to do after that.
Daniel Straight
it is still a work in progress from the creator. I'm sure he'd welcome any cool insights you might have
Toad
+2  A: 

You could try NParsec, but it seems not to be actively developed any more.

Alexey Romanov