Jump to content

How to parse nested statements????


Recommended Posts

Hi all,

This is hard to explain so bear with me.

Consider the following code.

for $x = 0 to 20 step 1
    $f += 1
Next

If I was writing an interpreter to parse that, to process FOR loops I would just look for the FOR keyword and the NEXT keyword, and process normally everything in the middle.

But if I had this...

for $x = 0 to 20 step 1
    for $e = 0 to 10 step 1
        ConsoleWrite( "Rebecca Black dies on Friday")
    Next
Next

It has a nested for loop, so now when I go to interpret it, the interpreter will find the first FOR, and the first NEXT, but then process the wrong things and eventually get to the point where it was trying to process a FOR or NEXT statment that wasnt matched by its partner FOR or NEXT statement.

How can I write an interpreter that can deal with a unlimited number of nested statements??? I just cant get my head around how to do it.

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Link to comment
Share on other sites

I've never dealt with anything like that really, but couldn't you just call a parsing function recursively when you hit the "FOR" statement, and then parse forward until you come across the "NEXT" and then unwind the recursion (then re-enter the recursive call to parse the next value for $x).

Hopefully that makes sense.

Link to comment
Share on other sites

I've never dealt with anything like that really, but couldn't you just call a parsing function recursively when you hit the "FOR" statement, and then parse forward until you come across the "NEXT" and then unwind the recursion (then re-enter the recursive call to parse the next value for $x).

Hopefully that makes sense.

Do you mean...

Func Parse($linenum)
  For $linenum in $linecount step 1
    if $firstwordofline = "Next" then return $linenum
    if $firstwordofline = "For" then 
      ;repeat for loop
        $linenum = Parse($linenum)
      ;end repeat
      continueloop
    Endif
  Next
Endfunc
Edited by hyperzap

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Link to comment
Share on other sites

Yeah that looks about like what I was thinking :)

Basically it doesn't worry about finding the matching "NEXT" as it encounters each "FOR", it just chugs along until it can determine that one is missing. Like at the end of the script, or at the end of a function.

Link to comment
Share on other sites

Time to hunt for a copy of one of the dragon books. Even the first edition '77 will be good!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Ok thanks for the help guys; my interpreters are working!!!

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...