473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Compilers, parsing, etc...

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<_ff;
}

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
Dec 5 '06 #1
3 3439
"Jon Slaughter" <Jo***********@Hotmail.comwrote:
>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.)

--
Lucian
Dec 5 '06 #2
I don't know if it will help, but you can get a good start on a parser in C#
by consulting http://www.frontiernet.net/~fredm/dps/Contents.htm , chapter
3. That contains the main logic for a parser, in about 200 statements.
Chapter 4 will show you how to build up the parse tree (with details left to
the reader :-) ).

"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:0r*****************@newssvr27.news.prodigy.ne t...
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<_ff;
}

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

Dec 5 '06 #3
Jon Slaughter wrote:
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<_ff;
}

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
Dec 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
6
by: Hrvoje Blazevic | last post by:
Are there any good books on Interpreter/Compiler construction, using Python as a defining language out there? Something like Essentials of Programming Languages 2e ? I would appreciate some...
10
by: Bill Davidson | last post by:
Hi there, Please forgive me for posting this article on multiple groups. Being new in the newsgroups, I was not sure which group would have been appropriate for my question. Sorry. My...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
13
by: Derek | last post by:
As I understand it there is a good amount of link compatibility among C compilers. For example, I can compile main.c with GCC and func.c with Sun One and link the objects using either linker (GNU...
8
by: pransri2006 | last post by:
Hi guys! I think all of u know about the designing of compilers. Can any body tell me about the designing of the compilers. And also tell me the difference between the compilers and Interpreter...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
0
by: JosAH | last post by:
Greetings, welcome back at the sequel of the parsers article chapter. This part is dedicated to the ExpressionParser, the largest parser class for our little language. This class parses a...
0
by: JosAH | last post by:
Greetings, this week's article part discusses the parsers used for our little language. We will implement the parsers according to the grammar rules we defined in the second part of this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.