?? ss5
字號:
.SHSection 5: Error Handling.PPError handling is an extremely difficult area, and many of the problems are semantic ones.When an error is found, for example, it may be necessary to reclaim parse tree storage,delete or alter symbol table entries, and, typically, set switches to avoid putting out any further output..PPIt is generally not acceptable to stop all processing when an error is found; we wish to continuescanning the input to find any further syntax errors.This leads to the problem of getting the parser ``restarted'' after an error.The general class of algorithms to do this involves reading ahead and discarding a number of tokensfrom the input string, and attempting to adjust the parser so that input can continue..PPTo allow the user some control over this process,Yacc provides a simple, but reasonably general, feature.The token name ``error'' is reserved for error handling.This name can be used in grammar rules;in effect, it suggests places where errors are expected, and recovery might take place.The parser attempts to find the last time in the input when the specialtoken ``error'' is permitted.The parser then behaves as though it saw the token name ``error'' as an input token,and attempts to parse according to the rule encountered.The token at which the error was detected remains the next input token after this error token is processed.If no special error rules have been specified, the processing effectively halts when an error is detected..PPIn order to prevent a cascade of error messages, the parser assumes that, afterdetecting an error, it remains in error state until three tokens have been successfullyread and shifted.If an error is detected when the parser is already in error state,no error message is given, and the input token is quietly deleted..PPAs a common example, the user might include a rule of the form.DSstatement : error ;.DEin his specification.This would, in effect, mean that on a syntax error the parser would attempt to skip over the statementin which the error was seen.(Notice, however, that it may be difficult or impossible to tell the end of a statement,depending on the other grammar rules).More precisely, the parser willscan ahead, looking for three tokens that might legally followa statement, and start processing at the first of these; ifthe beginnings of statements are not sufficiently distinctive, it may make afalse start in the middle of a statement, and end up reporting asecond error where there is in fact no error..PPThe user may supply actions after these special grammar rules,just as after the other grammar rules.These actions might attempt to reinitialize tables, reclaim symbol table space, etc..PPThe above form of grammar rule is very general, butsomewhat difficult to control.Somewhat easier to deal with are rules of the form.DSstatement : error \';\' ;.DEHere, when there is an error, the parser will again attempt to skip over the statement, but inthis case will do so by skipping to the next ``;''.All tokens after the error and before the next ``;'' give syntax errors, and are discarded.When the ``;'' is seen, this rule will be reduced, and any ``cleanup''action associated with it will be performed..PPStill another form of error rule arises in interactive applications, wherewe may wish to prompt the user who has incorrectly input a line, and allowhim to reenter the line.In C we might write:.DSinputline: error \'\en\' prompt inputline = { $$ = $4; };prompt: /* matches no input */ = { printf( "Reenter last line: " ); };.DEThere is one difficulty with this approach;the parser must correctly process three input tokens before it is prepared toadmit that it has correctly resynchronized after the error.Thus, if the reentered line contains errorsin the first two tokens, the parser will simply delete the offending tokens,and give no message; this is clearly unacceptable.For this reason, there is a mechanism in both C and Ratfor whichcan be used to force the parserto believe that resynchronization has taken place.One need only include a statement of the form.DSyyerrok ;.DEin his actionafter such a grammar rule, and the desired effect will take place;this name will be expanded, using the ``# define'' mechanism of C orthe ``define'' mechanism of Ratfor, into an appropriate code sequence.For example, in the situation discussed above where wewant to prompt the user to produce input,we probably want to consider that theoriginal error has been recoveredwhen we have thrown away the previous line, including the newline.In this case,we can reset the error state before putting out the prompt message.The grammar rule for the nonterminal symbol prompt becomes:.DSprompt: /* matches no input */ = { yyerrok; printf( "Reenter last line: " ); } ;.DE.PPThere is another special feature which the user maywish to use in error recovery.As mentioned above, the token seen immediatelyafter the ``error'' symbol is the input token at which theerror was discovered.Sometimes, this is seen to be inappropriate; for example, anerror recovery action mighttake upon itself the job of finding the correct place to resume input.In this case,the user wishes a way of clearing the previous input tokenheld in the parser.One need only include astatement of the form.DSyyclearin ;.DEin his action; again, this expands, in both C and Ratfor, to the appropriatecode sequence.For example, suppose the action after errorwere to call some sophisticated resynchronization routine,supplied by the user, which attempted to advance the input to thebeginning of the next valid statement.After this routine was called, the next token returned by yylex would presumablybe the first token in a legal statement; we wish to throw away theold, illegal token, and reset the error state.We might do this by the sequence:.DSstatement : error = { resynch( ); yyerrok ; yyclearin ; } ;.DE.PPThese mechanisms are admittedly crude, but do allow for a simple, fairly effective recovery of the parserfrom many errors, and have the virtue that the user can get ``handles'' by which he can deal withthe error actions required by the lexical and output portions of the system.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -