Compilers, parsing, etc...

J

Jon Slaughter

Is there anything like yacc or spirit for C#? What is the "standard" method
for parsing grammars in C#?

The last few days I have been looking at boost spirit and really like how it
works. What I was hoping for is a library that could take an ABNF grammar
and output C# code that represents that grammar. (A tree of classes that
contained the syntatic structure of the grammar which)

Right now a friend of mine is working on an parser and I helped him
implement the grammar in Spirit.

What I essentially did was write the grammar in spirit notation, write
classes of classes that had there fields filled with semantic actions
connected to the rules. There seems to be a duplicate of information though
and there seems to be some generalizations that could be made.

i.e.,

suppose we have the grammar

start ::= a | b | c
a ::= d | *f
b ::= *(c d)
c ::= f | *(f d)

where d and f are terminals

then the programmtic structure could be represented by

class start
{
_a a;
_b b;
_c c;
}

class _a
{
_d d;
list<_f> f;
}

class _b
{
list<{_c, _d}> cd
}

class _c
{
_f f;
list<{_f, _d}> fd
}


where by {_c, _d} I mean two classes that are "combined" in some sense. ONe
could have

struct _cd
{
_c c;
_d d;
}

typedef [type of d] _d;
typedef [type of f] _f;

to represent that.


Anyways. The point is that it would seem that one could generate a
"programmatic tree" from the grammar that would contain the data and one
then would just need a parser for it.

The problem that I'm having with bit spirit is that I'm having to code all
the actions that just stick in information into a recurively defined tree of
structures that essentially outline the grammar in the first place.

I'm not sure if one can have a "programmtic tree" like above for all
classes(it would seem any type of looping might cause problems).


I think all I want is some programmtic representation of the parsed data to
work with. While one probably can't represent all the needed information
using ABNF one probably could extend the it to handle most of the issues.
(such working with enums, etc...).

Maybe one can even represnt the rules as classes in a similar fashion I have
given above? I think this would end up following the lines of boost spirit
though except using classes/generics instead of templates.

Anyways, are there any libraries in C# that do this sorta thing?

Thanks,
Jon
 
L

Lucian Wischik

Jon Slaughter said:
Is there anything like yacc or spirit for C#? What is the "standard" method
for parsing grammars in C#?

Antlr. You effectively end up using it like spirit when producing
ASTs. But it has a pre-processing step first.

(Actually, what I found most enjoyable was writing my parser in F#
with F#lex and F#yacc. F# is a .net language so it interops fine with
C#. And functional languages like F# just seem infintely more suited
to manipulating ASTs than imperative/OO languages.)
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Jon said:
Is there anything like yacc or spirit for C#? What is the "standard" method
for parsing grammars in C#?

The last few days I have been looking at boost spirit and really like how it
works. What I was hoping for is a library that could take an ABNF grammar
and output C# code that represents that grammar. (A tree of classes that
contained the syntatic structure of the grammar which)

Right now a friend of mine is working on an parser and I helped him
implement the grammar in Spirit.

What I essentially did was write the grammar in spirit notation, write
classes of classes that had there fields filled with semantic actions
connected to the rules. There seems to be a duplicate of information though
and there seems to be some generalizations that could be made.

i.e.,

suppose we have the grammar

start ::= a | b | c
a ::= d | *f
b ::= *(c d)
c ::= f | *(f d)

where d and f are terminals

then the programmtic structure could be represented by

class start
{
_a a;
_b b;
_c c;
}

class _a
{
_d d;
list<_f> f;
}

class _b
{
list<{_c, _d}> cd
}

class _c
{
_f f;
list<{_f, _d}> fd
}


where by {_c, _d} I mean two classes that are "combined" in some sense. ONe
could have

struct _cd
{
_c c;
_d d;
}

typedef [type of d] _d;
typedef [type of f] _f;

to represent that.


Anyways. The point is that it would seem that one could generate a
"programmatic tree" from the grammar that would contain the data and one
then would just need a parser for it.

The problem that I'm having with bit spirit is that I'm having to code all
the actions that just stick in information into a recurively defined tree of
structures that essentially outline the grammar in the first place.

I'm not sure if one can have a "programmtic tree" like above for all
classes(it would seem any type of looping might cause problems).


I think all I want is some programmtic representation of the parsed data to
work with. While one probably can't represent all the needed information
using ABNF one probably could extend the it to handle most of the issues.
(such working with enums, etc...).

Maybe one can even represnt the rules as classes in a similar fashion I have
given above? I think this would end up following the lines of boost spirit
though except using classes/generics instead of templates.

Anyways, are there any libraries in C# that do this sorta thing?

http://www.antlr.org/
http://grammatica.percederberg.net/index.html
http://www.ssw.uni-linz.ac.at/Coco/
http://home.earthlink.net/~slkpg/

etc.etc.

Pick a blue, red or yellow based on what you like.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top