471,091 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,091 software developers and data experts.

[Parsing] How do I process loops with PLY?

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
Jul 18 '05 #1
1 1586
"F. GEIGER" <f.******@vol.at> 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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Fuzzyman | last post: by
77 posts views Thread by Charles Law | last post: by
8 posts views Thread by Darius Fatakia | last post: by
2 posts views Thread by Wade G | last post: by
22 posts views Thread by gene.ariani | last post: by
3 posts views Thread by =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.