Quote (tt_toby @ Sep 14 2013 09:44am)
Ty. I'm not at the point of dynmaic allocation yet because this exercise is from the first chapter of the book. Once I have I'll go back and have a look at it.
Yeah, jsp messes up tabs, best thing to do is write the code in an editor where tabs are set to be replaced by spaces automatically and then paste it into jsp.
About priority rules: Can my program be adjusted to respect them or do I pretty much have to start over?
thx for the tip =P
There actually wouldn't be much to do to adjust your code for priority rules. The first part of your loop is fine. When it's about to push to the list, you first need to verify for existing delimiters in the priority order: braces > parenthesis > brackets. Opening delimiters can be accounted for as usual. However it's preferable to maintain them all within the same list for the priority check. This is why I pointed you toward a single list array to begin with. You can save the nature of the delimiter the same way open/closed status is saved: 0x08000000 for braces, 0x04000000 for parenthesis, 0x02000000 for brackets.
Once you hit a closing delimiter things change a little. First of all you now have to keep track of properly closed couples. Dynamic allocation comes in play as you may now hit your max much sooner, since you're tracking 3 different delimiters and saving matched ones as well. You then simply stick to the priority rules:
1) With a closed brace, backtrack the list from the top until you hit an opened brace. If you hit it, mark the opened brace as matched, flagging it with 0x20000000. Don't modify any parenthesis or brackets you ran into backtraking, and make sure you don't save the closed brace in the list. If you didn't hit anything, though, save the closed brace and move on.
2) With parenthesis, backtrack again. Ignore all brackets. If you hit any type of brace (opened, closed or matched couple), this parenthesis can't be matched, move on. If you hit an opened parenthesis before any braces, mark the opened parenthesis as matches with 0x20000000. Same as before, if you hit a match, don't save the closed parenthesis, otherwise account for it.
3) With brackets, if the previous list index isn't a bracket, it can't be matched, save it. If the last item is an opened bracket, flag it, drop the closed one.
At the output, only mention elements in the list that have bit 0x20000000 unflagged. For encoding purposes, since you use the last 7 bits of the list integers for extra data, make sure atline <= 0x01FFFFFF, which is the maximum amount of lines this code can process with a 32 bits integer list.
Note that this code only renders the simpliest version of the rule: braces > parenthesis > brackets. In most languages, () pairs can be nested within []. In javascript you can see {} nested within (). If this code is intended to parse languages like php, you'll have to pay attention to <> as well. If you intent to take care of all of these and implement full nesting rules, you'll have to use your current code to maintain a full record of delimiters first, then post process, as you need to first identify all matched couples to then verify if each individual couple is nested properly. That'll be fun alright =P