[Parsing] How do I process loops with PLY?

F

F. GEIGER

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
 
E

Edwin Young

F. GEIGER said:
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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top