Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parsing Python-like code
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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')
        )
    ]
)
Reply
#5
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  An article of GvR about the parsing of python Gribouillis 0 2,321 Jan-18-2020, 11:28 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020