[Parsing] How do I process loops with PLY?

Discussion in 'Python' started by F. GEIGER, Dec 25, 2004.

  1. F. GEIGER

    F. GEIGER Guest

    I'm rather new to high level parsing with lex/yacc (and realy impressed so
    far).

    I'd like to parse *and execute* files like this:

    A=12
    B=23
    G X=A Y=B Z=34 # Move to X, Y, Z
    G Z=0

    etc.

    No problems so far.

    But when loops enter the game, things seem to become more difficult:

    A=12
    B=23
    C=45
    D=56
    DX=0

    FOR C # Exec the following lines C times
    G X=A+DX Y=B Z=34 # Move to X, Y, Z

    # more statements...

    DX=DX+67
    NEXT

    How can I tell PLY to go back to the beginning of the FOR-loop's body to
    execute it C-1 more times? A syntax checker would not need to do that, but
    an interpreter (which actually I am building) has to.

    Any hints are welcome.

    Kind regards
    Franz GEIGER
     
    F. GEIGER, Dec 25, 2004
    #1
    1. Advertising

  2. F. GEIGER

    Edwin Young Guest

    "F. GEIGER" <> writes:

    > But when loops enter the game, things seem to become more difficult:
    >
    > A=12
    > B=23
    > C=45
    > D=56
    > DX=0
    >
    > FOR C # Exec the following lines C times
    > G X=A+DX Y=B Z=34 # Move to X, Y, Z
    >
    > # more statements...
    >
    > DX=DX+67
    > NEXT
    >
    > How can I tell PLY to go back to the beginning of the FOR-loop's
    > body to execute it C-1 more times? A syntax checker would not need
    > to do that, but an interpreter (which actually I am building) has
    > to.


    Typically, you would separate parsing from executing the code. Use PLY
    to parse the whole thing once and produce a data structure
    representing the code (usually called an 'abstract syntax tree') then
    write an interpreter which uses that tree as its input.

    Regards,
    --
    Edwin
     
    Edwin Young, Dec 26, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maurice LING

    Using PLY

    Maurice LING, Sep 17, 2004, in forum: Python
    Replies:
    9
    Views:
    462
    Maurice LING
    Sep 20, 2004
  2. Tran Tuan Anh

    SPARK v.s. PLY

    Tran Tuan Anh, Nov 18, 2004, in forum: Python
    Replies:
    9
    Views:
    624
    Jean Brouwers
    Nov 20, 2004
  3. =?ISO-8859-1?Q?=C5smund_Grammeltvedt?=

    Ply(LALR) and Yacc behaving differently

    =?ISO-8859-1?Q?=C5smund_Grammeltvedt?=, Apr 7, 2005, in forum: Python
    Replies:
    0
    Views:
    358
    =?ISO-8859-1?Q?=C5smund_Grammeltvedt?=
    Apr 7, 2005
  4. Any help with PLY?

    , Nov 17, 2005, in forum: Python
    Replies:
    1
    Views:
    271
    Paul McGuire
    Nov 17, 2005
  5. glomde
    Replies:
    3
    Views:
    293
    glomde
    May 9, 2006
Loading...

Share This Page