0

Can someone point me in the right direction, I have a parser in lex I want to use in C#, has anyone before done something like this, or mixed c/c++ with C#?

EDIT: More specifically I would like to parse mathematical expressions and XML.

mihajlv
  • 2,275
  • 4
  • 36
  • 59
  • possible duplicate of [Possible to call C++ code from C#?](http://stackoverflow.com/questions/935664/possible-to-call-c-code-from-c) – Bala R Jan 13 '12 at 18:35
  • See http://stackoverflow.com/questions/540593/lex-yacc-for-c Also, you might like at C# Expression Trees, depending on what you want to parse. – 3Dave Jan 13 '12 at 18:35
  • @DavidLively I edited the question. I'll take a look at the links thanks. – mihajlv Jan 13 '12 at 18:49
  • @BalaR thanks, I'll take a look. I am guessing the same would apply for c code? – mihajlv Jan 13 '12 at 18:50

4 Answers4

2

There are a number of C# Lex processors out there. For instance:

http://www.seclab.tuwien.ac.at/projects/cuplex/lex.htm

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • @Thanks I didn't know, that would a life saver, I'll check it out. By the way have you used it, do you know how it relates performance wise to the c lex. – mihajlv Jan 13 '12 at 18:46
  • 1
    @mihajlv - no, i've not used it. But like i said, there are a number of them out there.. if it doesn't meet your needs, try another one. It's better to keep everything managed if you can, and only go unmanaged if you absolutely need to. – Erik Funkenbusch Jan 13 '12 at 18:48
1

You can make a dll of c code and use it in c# with PlatformInvoke mechanisms available.

Even you can create a solution containing two projects, one of them is a Library Project containing the c/c++ source and another one is c#.

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
1

If you will be using this module in desktop/server applications only (and not on Silverlight or Compact Framework for example) you could build your C/C++ code with clr support and use it from C#.

yms
  • 10,361
  • 3
  • 38
  • 68
1

I have used a C++ version of flex with great success.

You can get the windows pre-built binaries from

http://www.kohsuke.org/flex++bison++/

Specifically, I have used this to parse an EBNF grammer of more than a dozen rules ( too many for boost::spirit ) which take as input user scripts with statements like

  IF a > 10 AND b < -22 THEN OUTPUT X

So I think it would do fine for the mathematical expressions you mention.

Parsing XML is a different story. There are so many purpose built libraries for parsing XML why would you want to roll your own? There is TinyXML ( http://www.grinninglizard.com/tinyxml/ ) and CMarkup ( http://www.firstobject.com/dn_markup.htm ) both of which I have used successfully on several projects.

ravenspoint
  • 19,093
  • 6
  • 57
  • 103
  • Thanks for the edit. I am just getting started with XML, I haven't done a lot of research around the subject, I know there are some sort of DOM objects which provide you with the XML tree with The XML elements as children. I'll take a look at the links. By the way is boost::spirit some sort of parsing tool? Thanks again. – mihajlv Jan 13 '12 at 23:06