3

I need to generate C structs and arrays from data stored in a db table, and alternately parse similar info. I use both ruby and python for this task, and was wondering if anyone heard of a module/lib that handles this for either/both languages? I could do this on my own with some string processing, but wanted to check if there's a known and tested parser out there that people know of. thanks.

3
  • 4
    Good luck. C is already a language not designed for ease of parsing, but C++ is much worse - assuming you want to parse real code, not just the code you generated youself. Commented Oct 3, 2010 at 9:11
  • Why would you want to do this? Commented Oct 3, 2010 at 13:47
  • I agree, you're unlikely to find a full C or C++ parser that just so happens to be coded up for Ruby or Python, there's little incentive for anyone to have built this. So, you'l likely need to consider parsing it yourself. If all you want to do is to process legal, basic structs, on scalar data types, writing a grammar for this subset of the C language is pretty straightforward. If you want process arbitrary structs (with unions, bit offsets, definition/use of typedefs, ... ) especially those taken from real code, then you'll have a lot more work to do. Commented Oct 3, 2010 at 19:17

3 Answers 3

3

Check out open-source software tool SWIG (Simplified Wrapper and Interface Generator). First sentence of the intro on the webpage:

SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of languages including common scripting languages such as Perl, PHP, Python, Tcl and Ruby.

Very mature (initial release - February 1996 according to Wikipedia) and there are lots of tutorials, documentation, and help.

Sign up to request clarification or add additional context in comments.

Comments

1

There is a basic C struct parser here on the pyparsing wiki. Pyparsing is a Python module for creating parsers by assembling separate parsing building blocks together. (No help on the Ruby part of the question, though.)

Comments

1

Haven't used it myself, but CAST might be worth a look:

http://cast.rubyforge.org/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.