Python Forum

Full Version: parsing Python-like code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i would like to parse some text string like Python code. it is an assignment expression. the idea is to break it apart just like it would be done for Python code. in particular i want to break apart literal strings into one string item and support [ and ] for a list. it would return a list of items. is there a tool suitable for this?
Have you looked at how the Python interpreter works? Obviously it must do things like that.

Also, if you're writing a language, perhaps you need to get your hands on a copy of the dragon book or something similar.
it's not a language intended for public. it's just an embedded language for a program which needs to add a few new statements which in most cases will be "compiled" to a function call.

> Have you looked at how the Python interpreter works?

yes, but not recently. i don't want to re-implement Python.

> Also, if you're writing a language, perhaps you need to get your hands on a copy of the dragon book or something similar.

i wrote a small compiler when i was in school for my compiler class. it was a lot of work and i don't want to do that again. but compiling to Python would be simpler. compile Python to Python. that's trivial. now add the Python 2 print command so you have both the command and the function. you just convert each command to a function call. easy. i just want to keep this simple.
Maybe use the ast module?

>>> import ast
>>> ast.parse('thing[4] = "banana"')
<_ast.Module object at 0x000002619ECBB1D0>
>>> pp(_)
ast.Module(
    body=[
        ast.Assign(
            targets=[
                ast.Subscript(
                    value=ast.Name(id='thing', ctx=ast.Load()),
                    slice=ast.Index(value=ast.Num(n=4)),
                    ctx=ast.Store()
                )
            ],
            value=ast.Str(s='banana')
        )
    ]
)
i don't understand what you are getting, but it does not seem to be an iterator of strings which is what i would expect.