Java Reference
In-Depth Information
scanned as an identifier. The result would be . . .for tnight. . . , which will
cause a syntax error. Such occurrences are unavoidable.
However, a good syntactic error-repair algorithm will often make some
reasonable repair. In this case, returning a special warning token when a
lexical error occurs can be useful. The semantic value of the warning token
is the character string that is deleted to restart scanning. The warning token
warns the parser that the next token is unreliable and that error repair may
be required. The text that was deleted may be helpful in choosing the most
appropriate repair.
Certain lexical errors require special care. In particular, runaway strings
and comments should receive special error messages.
Handling Runaway Strings and Comments Using Error Tokens
In Java, strings are not allowed to cross line boundaries, so a runaway string is
detected when an end-of-line character is reached within the string body. Or-
dinary recovery heuristics are often inappropriate for this error. In particular,
deleting the first character (the double quote character) and restarting scan-
ning will almost certainly lead to a cascade of further “false” errors because
the string text is inappropriately scanned as ordinary input.
One way to catch runaway strings is to introduce an error token .Anerror
token is not a valid token; it is never returned to the parser. Rather, it is a
pattern for an error condition that needs special handling. We use an error
token to represent a string terminated by an Eol rather than a double quote.
For a valid string, in which internal double quotes and backslashes are escaped
(and no other escaped characters are allowed), we can use
)
”(Not(”
|
Eol
|\
)
|\
|\\
For a runaway string, we can use
) Eol
”(Not(”
|
Eol
|\
)
|\
|\\
When a runaway string token is recognized, a special error message should
be issued. Further, the string may be repaired and made into a correct string
by returning an ordinary string token with the opening double quote and
closing Eol stripped (just as ordinary opening and closing double quotes are
stripped). Note, however, that this repair may or may not be “correct.” If the
closing double quote is truly missing, the repair will be good. If it is present
on a succeeding line, however, a cascade of inappropriate lexical and syntactic
errors will follow until the closing double quote is finally reached.
Some PL
I compilers issue special warnings if comment delimiters appear
within a string. Although such strings are legal, they almost always result
/
 
Search WWH ::




Custom Search