Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Mathematica allows you to pattern match based on type and operate on itself because of it's list nature.
For example:

a[b] /. a -> d returns d[b]

How would I go about creating a function that compile programming languages(Pyhton in this example) into a format that can be pattern matched by mathematica?

Python Example:

x=CompilePythonToList["def a(b):
    return c(b);"]

then pattern match against the lists and compile back to Python

x /. c(x_) -> d(x)
x=CompileListToPython[x];

x now contains

def a(b):
  return d(b);

I am have traditionally used regex to search and replace code but I have run into similar issues as described here. I believe it would require implementing a lexer of some kind, but was wondering if there was any advice in terms of implementing a system that works across a entire range of languages.

Maybe I could adapt the code that is used to syntax highlight different programming languages to support multiple programming languages?

I feel certain someone has created something similar before.

share|improve this question
1  
You could have a look at ANTLR. It's a parser generator that has explicit support for "code rewriting", i.e. you define transformation rules on the AST and ANTLR will try to generate source code that's as close to the original code (regarding whitespaces, comments) as possible. IIRC Python lets you access the AST, too, but I don't know if you can modify it. – nikie Feb 7 at 11:03

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.