250 likes | 258 Views
Errors in Top-Down Parsing. Teoría de Autómatas y Lenguajes Formales M. Luisa González Díaz Universidad de Valladolid, 2005. type → simple | ^ simple | array [ simple ] of type simple → integer | char | num ptpt num.
E N D
Errors in Top-Down Parsing Teoría de Autómatas y Lenguajes Formales M. Luisa González Díaz Universidad de Valladolid, 2005
type → simple | ^ simple | array [simple] oftypesimple → integer | char | num ptpt num
type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num A correct input: array [ char ] of integer
array [ simple ] of type simple char integer type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num type $ array [ char ] of integer $ Lex. An.
type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num type $ array [ simple ] of type array Lex. An.
type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num type $ array [ simple ] of type array [ ^ Lex. An.
type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num type $ array [ simple ] of type array [ ^ char Lex. An. Error
simple char integer type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num type $ array [ simple ] of type array [ ^ char ] of integer $ Lex. An.
type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num type $ array [ simple ] of type array Lex. An.
char type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num type $ array [ simple ] of type array [ type char ] of $ Lex. An. Error
type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num Panic mode Unexpected token: Mark error. Try again with the next token Unexpected end of string: Mark error. Recognize we can’t expand non terminal
Code (panic mode) proceduretype; if lookahead = array then match(array); match (‘[‘);simple; match(‘]‘); match(of); type else if lookahead = ‘^’ then match(‘^’); simple else if lookahead in {char, integer, num} then simple else if lookahead in {‘[‘,of, ‘]’} then writeln (‘Error:unexpected’, lookahead); match (lookahead); type else (* lookahead = $ *) writeln (‘Error: I couldn’t find type’) normal errors
array [ simple ] of type simple char integer type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num type $ array [ char char ] of integer $ Lex. An. Error
array type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num type $ [ simple ] of type array $ Lex. An. Error
match: panic mode • lookahead = expected token (t) • OK: read next token into lookahead and return • lookahead <> expected token (t) • mark error :unexpected token (t), but • lookahead <> $ • try again with the next token into lookahead • lookahead = $ • don’t try again, don’t read anymore, just return
Code of match (panic mode) procedure match (t: token); (*Pre: t <> $ * Never try to match end of string) begin if lookahead = t then lookahead := nexttoken (lexical analyzer) else if lookahead <> $ then writeln (‘Unexpected’, lookahead); lookahead := nexttoken; match (t) else (* lookahead = $ *) writeln (‘Unexpected end of string’) end
array type → simple | ^ simple | array [simple] oftype simple → integer | char | num ptpt num type $ [ simple ] of type array [ simple ] of type $ Lex. An. Error Error Error Error Error
Main program Begin lookahead = nexttoken; type; if lookahead = $ then input finished (errors, if any, were marked) else unexpected input after end of type End.
type → simple | ^ simple | array [simple] oftypesimple → integer | char | num ptpt num An example of phrase-level error-recovery strategy Suposse it’s quite usual forgeting first number in num ptpt num Error action : mark it was forgotten
normal errors Code (panic+phrase-level mode) proceduresimple; if lookahead = integer then match(integer); else if lookahead = char then match(char); else if lookahead = num then match(num); match(ptpt); match(num); else if lookahead in {array, ‘^’, ‘[‘, of, ‘]’} then writeln (‘Error:unexpected’, lookahead); match (lookahead); simple else if lookahead = ptptthen writeln (‘Error: forgotten num’); match(ptpt); match(num); else (* lookahead = $ *) writeln (‘Error: I couldn’t find simple’)