Syntax Checking in a Recursive-Descent Parser


In expression parsing, a syntax error is simply a situation in which the input expression does not conform to the strict rules required by the parser. Most of the time, this is caused by human error—usually typing mistakes. For example, the following expressions are not valid for the parsers in this chapter:

 10 ** 8 ((10  5) * 9 /8

The first contains two operators in a row, the second has unbalanced parentheses, and the last has a division sign at the start of an expression. None of these conditions is allowed by the parser. Because syntax errors can cause the parser to give erroneous results, you need to guard against them.

As you studied the parser code, you probably noticed the SyntaxErr( ) method, which is called under certain situations. The recursive-descent method of parsing makes syntax checking easy because, for the most part, it occurs in Atom( ), FindVar( ), or EvalExp6( ), where parentheses are checked.

When SyntaxErr( ) is called, it throws a ParserException that contains a description of the error. As Parser is currently written, this exception is caught in Evaluate( ). Thus, the parser immediately stops when an error is encountered. You can, of course, change this behavior to suit your own needs.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net