Lecture
When parsing expressions, a syntax error is simply a situation in which the input expression does not conform to the strict rules of the analyzer. In most cases, this is due to a human error - usually due to typos. For example, the following expressions are not correct from the point of view of the analyzers discussed in this chapter:
10 ** 8 (10 - 5) * 9 /eight
In the first of these, there are two operators in a row, in the second, the brackets are not balanced, and the latter begins with a division sign. None of these sequences is allowed by the analyzers considered. Since, in the presence of syntax errors, the analyzer may produce an incorrect result, it is necessary to ensure that there are no such errors.
While studying the analyzer code, you probably noticed the serror () function, which is called in certain situations. This function reports errors. Unlike many other types of analyzers, recursive descent facilitates syntax checking, since in most cases it occurs in the atom () , find_var () and eval_exp6 () functions, where the correct placement of brackets is checked. The only problem associated with the detection of syntax errors is that when an error is detected, the analysis of the expression does not stop. This may result in multiple error messages.
The best way to implement the serror () function is to force it to do something like restore the correct state of the analyzer. For example, all modern compilers come with a pair of auxiliary functions setjmp () and longjmp () . These functions allow the program to transfer control from one function to another . For example, the serror () function could perform a long jump with the help of longjmp () to a safe point of the program outside the analyzer.
If you leave the parser code unchanged, several syntax error messages can be displayed at once. Of course, in some situations it may interfere, but in others it can be very useful, since it is possible to identify several errors at once. Nevertheless, in the general case, before using the analyzer in commercial programs, it is necessary to refine its block of syntactic control.
Comments
To leave a comment
Algorithms
Terms: Algorithms