Download presentation
Presentation is loading. Please wait.
Published byNora McDaniel Modified over 9 years ago
1
Errors in Top-Down Parsing Teoría de Autómatas y Lenguajes Formales M. Luisa González Díaz Universidad de Valladolid, 2005
2
PPT integercharnumarray^[of]$ type rule 1 rule 3rule 2 simple rule 4rule 5rule 6 type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num
3
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 A correct input: array [ char ] of integer
4
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 char ]ofinteger simple integer $ $
5
Empty entries
6
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $
7
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [^ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $
8
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [^ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 Error $ char
9
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [^ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 char ]ofinteger simple integer $ $
10
Other kind of empty entries
11
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $
12
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $ char ]of$ Error type
13
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 Unexpected token: Mark error. Try again with the next token Unexpected end of string: Mark error. Recognize we can’t expand non terminal Panic mode
14
Code (panic mode) procedure type; 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
15
Match errors
16
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 char ]ofinteger simple integer $ $ char Error
17
Other kind of match errors
18
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftype[simple array Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $ $ Error
19
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
20
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
21
type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftype[simple array Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $ $ Error [ simple ] Error of Error type
22
Main program Begin lookahead = nexttoken; type; if lookahead = $ then input finished (errors, if any, were marked) else unexpected input after end of type End.
24
PPT integercharnumarray^..[of]$ type rule 1 rule 3rule 2 simple rule 4rule 5rule 6er type → simple | ^ simple | array [simple] of type simple → 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
25
Code (panic+phrase-level mode) procedure simple; 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 = ptpt then writeln (‘Error: forgotten num’); match(ptpt); match(num); else (* lookahead = $ *) writeln (‘Error: I couldn’t find simple’) normal errors
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.