Ruby/Python - generating and parsing C/C++ code - python

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.

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.

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.)

Haven't used it myself, but CAST might be worth a look:
http://cast.rubyforge.org/

Related

Making Python coding locale specific

I figured out:
Python is a great programming language.
Many intelligent kids out there don't know English, but still want to write code.
Does Python support a locale wrapper over its English syntax? Such that all keywords like import, print, etc. are translated into another human language. Consecutively, translating Python code back-and-forth from one human language to another. Reducing English to be just one of several locale-specific human languages. Kids can then write code in their language (given their IDE supports Unicode), which will boil down to same compiled code.
If not, where do I get started to include this feature in Python through an open source project?
Chinese Python already exists.
A problem with your idea is in libraries – a lot of Python's appeal is in the fact that there's a lot of libraries for it. And virtually all of them use English. Translating all of them is pretty much impossible.
Plus, you don't need much English for Python – it's just individual words like import, and print. They aren't that hard to learn. After all, kids can easily remember words like lumos and wingardium leviosa, which aren't in their native language either.
It's much more important to translate manuals and documentation.
Another bad problem with your idea is that if you teach programmers to write in their native language, the rest of the world won't understand them. As for your question, I think the Chinese Python should have some info on how to translate Python. Look at their repository to see the changes.
If you really want to make it speaking in your native language, think about using something like GvR (The Guido van Robot Programming Language). You can define the actions using your native language. There also was some internationalization effort. For example, it was completely translated into my mother language (including the control commands).
You can use the various modules in the Python language services to parse and compile your DSL.

How to decide when to wrap/port/write-from-scratch

There is a project I'm about to build in Smalltalk (Pharo). And there is a python library which I intend to use for the same. Now, there are 3 options:
Smalltalk wrapper for those python libraries
Porting the python library to Smalltalk
Write the library from scratch (in Smalltalk) for use in my project
The following are my queries:
What are 'basic' differences in porting/wrapping (No satisfactory explanation found anywhere yet)
How to know when to use which (of all the three)?
Any resources or pointers where I can get further help/some kick-start into my project.
Thanks!
Wrapper
Write functions in the native language whose sole purpose is to call the functions in the external library. The goal is to do as little as possible in the native language. For example, translating data types from the native language to the external library language, etc.
Wrappers make sense when the external library is:
written in a more efficient language than the native code (eg, a C++ library called from Python)
large/complex and would be time-consuming or error prone to translate
regularly updated; in a well-maintained library the interfaces (what your wrapper is concerned with) will change less often than the implementation of the features; so if you have wrappers around the functionality, updating to a new version of the library should be fairly straightforward
Porting
A port is simply a translation from one language to another. In general, the same logic is maintained as much as possible.
Porting makes sense when:
the native language is more efficient than the external library
the library is simple and one wants to save on the overhead involved with wrapping
one intends to make and maintain changes to the ported code in the native language
there are no plans to use the external library in its own language
one wants to learn one or both of the languages involved
Re-Write
Think of a Re-Write as a Port with a lot of refactoring. The goal is to take advantage of features of the native language to improve the library in some way (efficiency, readability, etc.)
Re-Writing makes sense in all of the same scenarios as porting. Deciding whether to do a simple port or a full re-write usually comes down to one question:
Is there a better way to implement the features of the external library in the native language?

Lexing and Parsing Utilities

I'm looking for lexical analysis and parser-generating utilities that are not Flex or Bison. Requirements:
Parser is specified using a context-free LL(*) or GLR grammar. I would also consider PEGs.
Integrates tightly with a programming language that could be used for both scripting and application development. Language should also have facilities for easily interfacing with C. Good examples are Python, Ruby, and Guile. No C, Java, or Perl please. I want the language to be homogeneous; I want the parser generator to output code in the same language.
Well-documented and production-quality.
Open source. Free is also desirable (although not required).
Compatible with Linux distributions or one of the open source BSDs. I would consider OpenSolaris.
Rapid development is a considerably greater concern than efficiency.
Suited to parsing natural language as well as formal languages. Natural language parsing is limited to short, simple sentences with very little ambiguity.
I have my eye on ANTLR, although I have never used it. Comments to that effect are appreciated. Let me know what your favorite utilities are that meet these requirements, and why you would recommend them.
There is a list of modern Packrat parsers here.
NL text tends have to lots of ambiguity. If you want to parse natural langauge, I don't think any of the classic compiler-type parser generators (LALR, LL [including ANTLR]) will help you much, and compiler type parser generators typically don't handle this at all.
A GLR parser, which does handle ambiguity, may be of some use; bison offers this as an option.
Guile 2.0 (to be released in about a few days) has an LALR(1) parsing library.

Is there some lispy language that seamlessly integrates with Python?

Is there a language based on S-expressions with powerful macros that allows as seamless integration with Python as Clojure with JVM?
I want to try using such syntax and features while having access to all usual python libraries (including PyQt).
I've been working a project to do this: psil. I have a series of blog posts talking about what I've done. Here's the short manifesto:
Psil is a new general-purpose programming language in the Lisp family of languages. Psil is implemented on top of Python, allowing easy access to existing Python libraries.
Best features from Lisp and Scheme
Complete language in its own right
Built upon the Python standard libraries
Strong interoperability with Python code
The reality hasn't quite caught up to the vision; for example I don't think there is a way to declare new classes in Psil code that can be used from Python. But at least for functions, it's mostly there.
Note that Psil is built completely on Python 3, and there is no Python 2 version. I don't know whether there is a PyQt for Python 3.
While these aren't exactly what you're looking for, check:
CLPython - an implementation of Python in Common Lisp
(An ((Even Better) Lisp) Interpreter (in Python))
Check out Boo; it's a python-inspired language that runs on the CLR, with built-in support for full macros. If that's what you're missing from Lisp, give it a shot. A friend swears by it.

Mini-languages in Python

I'm after creating a simple mini-language parser in Python, programming close to the problem domain and all that.
Anyway, I was wondering how the people on here would go around doing that - what are the preferred ways of doing this kind of thing in Python?
I'm not going to give specific details of what I'm after because at the moment I'm just investigating how easy this whole field is in Python.
Pyparsing is handy for writing "little languages". I gave a presentation at PyCon'06 on writing a simple adventure game engine, in which the language being parsed and interpreted was the game command set ("inventory", "take sword", "drop book", etc.). (Source code here.)
You can also find links to other pyparsing articles at the pyparsing wiki.
I have limited but positive experience with PLY (Python Lex-Yacc). It combines Lex and Yacc functionality in a single Python class. You may want to check it out.
Fellow Stackoverflow'er Ned Batchelder has a nice overview of available tools on his website. There's also an overview on the Python website itself.
I would recommend funcparserlib. It was written especially for parsing little languages and DSLs and it is faster and smaller than pyparsing (see stats on its homepage). Minimalists and functional programmers should like funcparserlib.
Edit: By the way, I'm the author of this library, so my opinion may be biased.
Python is such a wonderfully simple and extensible language that I'd suggest merely creating a comprehensive python module, and coding against that.
I see that while I typed up the above, PLY has already been mentioned.
If you ask me this now, I would try the textx library for python. You can very easily create a dsl in that with python! Advantages are that it creates an AST for you, and lexing and parsing is combined.
http://igordejanovic.net/textX/
In order to be productive, I'd always use a parser generator like CocoPy (Tutorial) to have your grammar transformed into a (correct) parser (unless you want to implement the parser manually for the sake of learning).
The rest is writing the actual interpreter/compiler (Create stack-based byte code or memory AST to be interpreted and then evaluate it).

Categories