Reputation: 159
I created syntax analyzer. If it find syntax error, program show error message and stop running. I need to show also line and column of symbol what lead to error.
Problem is in analyzer algorithm. It takes text from file. Split it into list of syntax constructions (for example: int, bool, for) and single symbols. Like:
"int[] foo = new int[5];"
"int" -> "[" -> "]" -> "f" -> "o" -> "o" -> "=" ->
-> "new" -> "int" -> "[" -> "5" -> "]" -> ";"
Analyzer take elements of this list one by one before it find syntax error or chech whole text.
So, what is simple way to define where in source text if point of found sybtax error?
For example:
int[] foo = new int[5];
string[) foo; //syntax error in 2 line 8 column
I can just read source text symbol by symbol and save position of each element. But this mean pretty serious modification of analyzer code.
Maybe here is some better solution?
If it's matter, i use c#.
Upvotes: 1
Views: 106
Reputation:
You probably have a loop on the lines, then you can easily maintain a line counter variable.
And for the column, you can add the lengths of the tokens in the current line up to the detected error.
Upvotes: 1