Is there any way I can execute a block of code in a loop, like so:

[ some code goes here ]

Where the delimiters are "[" and "]", and also allowing for nested blocks, i.e.:

[the user can create another loop[]]

I am creating a parser for a language that implements such semantics, and it reads the code one byte/character at a time since the commands are all single characters and because the syntax is supposed to be very minimal (which is fairly important), and I am currently stuck in this loop thing.

I have tried matching the byte (since I am only reading 1 character at a time) with a "[" character and then setting a global variable across the instance, but this seems to break when the statements are nested, and doesn't seem to work.

Any help (in Python, possibly) is appreciated!

EDIT: Conforming to what @mattnz commented, the question & post has been further clarified.

  • As it currently stands this is likely to be closed as "no clear what is being asked". Suggestions for improvement are Why are single characters import, whats the termination condition for the loop, what have you tried so far? – mattnz Aug 26 '13 at 7:29
  • I've tried detecting for the "[" character, and then setting a global variable across the parser/lexer instance- but this will not work as it results in a never ending loop. The reason single characters are important is because the syntax for the code is meant to be very minimal, i.e. ~++. – knight Aug 26 '13 at 7:40
  • 2
    "also allowing for nested blocks" and "setting a global variable when [ is detected" absolutely can't work together. You really must use a proper parser to implement something like this. Go ahead, read some introductory parsing literature - it doesn't bite (much). – Kilian Foth Aug 26 '13 at 7:45
  • Are parsers (Python ones anyway) happy with single character tokens? Especially if the tokens can be joined with other tokens to have different meanings, i.e. ~++ and @~. – knight Aug 26 '13 at 7:51

Indeed, you can use a variable which stores the current state or the current context. If you have an issue with nested blocks, it's because you are storing only one state at a time. Using a stack can help.

For more information, look at "parenthesis matching algorithm" in Google. You'll find plenty of information about how to match nested brackets.

Also, Kilian Foth's comment to your question is particularly valuable: if you're already stuck at the very beginning with a problem such as parenthesis matching, it is a good sign that you may want to learn a bit more about parsing before you venture into building a parser yourself.

| improve this answer | |
  • Using a stack, which means I will have to keep a "memo" or dict of the currently open brackets? – knight Aug 26 '13 at 7:55
  • See Using Lists as Stacks. – Arseni Mourzenko Aug 26 '13 at 7:56
  • I don't really see how that may help, but I've read somewhere ([here] (forums.codeguru.com/…)) that you can use a counter decrement + increment method where you increment the counter by 1 every time you spot a bracket and decrement it when you spot a closing bracket. – knight Aug 26 '13 at 8:12
  • 1
    @knight, you can use a counter if you just want to make sure the count of opening and closing brackets matches. If you actually need to know the position of the matching bracket to do something useful with it, you need to use a stack. – Karl Bielefeldt Aug 26 '13 at 13:24
  • Oh yes, I used a function to generate a dictionary before parsing where the opening bracket's position is stored and the closing bracket's position is also stored. – knight Aug 26 '13 at 23:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.