Here's a brainbuster, for anybody who likes a challenge. Although I am not exactly an experienced PHP coder, I do understand PHP and I learn really fast so now I am working on a small PHP script that verifies the syntax of some plain text files I'm using with another application. The basic syntax is:
if
endif
At this point, the script reads the file, breaks it into lines (so I can get the line number and get a detailed report about which line has an error - if any) and then goes further breaking the lines into words. If I have a file that has this code in it:
if
if
endif
and I run it through the script, it will print out an error saying there's an ENDIF missing on line 3 (I know it should say line 2 but like any other syntax checker, the script only spots an error when it runs into something that isn't right). I achieved this by setting an "endif_required" flag whenever an IF is detected so if an ENDIF is NOT detected before another IF, the error is reported. Simple, easy and works like a charm. But here comes the part that's driving me nuts: SOME of the files I'm supposed to check may contain nested events like this:
if
if
endif
endif
Of course, the script will print out an error saying there's an ENDIF missing on line 2 which is WRONG. Now, I could easily get rid of this by simply counting the IFs and ENDIFs and compare the total count, but then I wouldn't have the line number report. So my question is (although probably more related to logic than PHP): how should I rewrite/re-think the script so it won't print out an error in case of nested events AND keep the line number report ?
Any help is more than welcome and I thank you in advance !