+ All Categories
Home > Documents > Errors in Top-Down Parsing

Errors in Top-Down Parsing

Date post: 10-Feb-2016
Category:
Upload: sinjin
View: 29 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
25
Errors in Top-Down Parsing Teoría de Autómatas y Lenguajes Formales M. Luisa González Díaz Universidad de Valladolid, 2005
Transcript
Page 1: Errors in Top-Down Parsing

Errors in Top-Down Parsing

Teoría de Autómatas y Lenguajes FormalesM. Luisa González Díaz

Universidad de Valladolid, 2005

Page 2: Errors in Top-Down Parsing

PPT integer char num array ^ [ of ] $

type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

Page 3: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

A correct input:

array [ char ] of integer

Page 4: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

array

type

] of typearray [ simple

[ Lex. An.

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

char

char

] of integer

simple

integer

$

$

Page 5: Errors in Top-Down Parsing

Empty entries

Page 6: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

array

type

] of typearray [ simple

Lex. An.

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

$

Page 7: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

array

type

] of typearray [ simple

[ ^ Lex. An.

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

$

Page 8: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

array

type

] of typearray [ simple

[ ^ Lex. An.

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

Error

$

char

Page 9: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

array

type

] of typearray [ simple

[ ^ Lex. An.

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

char

char

] of integer

simple

integer

$

$

Page 10: Errors in Top-Down Parsing

Other kind of empty entries

Page 11: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

array

type

] of typearray [ simple

Lex. An.

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

$

Page 12: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

array

type

] of typearray [ simple

[ Lex. An.

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

$

char

char

] of $

Error

type

Page 13: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 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

Page 14: Errors in Top-Down Parsing

Code (panic mode)procedure type;

if lookahead = array thenmatch(array); match (‘[‘);simple; match(‘]‘); match(of); type

else if lookahead = ‘^’ thenmatch(‘^’); 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

Page 15: Errors in Top-Down Parsing

Match errors

Page 16: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

array

type

] of typearray [ simple

[ Lex. An.

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

char

char

] of integer

simple

integer

$

$

char

Error

Page 17: Errors in Top-Down Parsing

Other kind of match errors

Page 18: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

array

type

] of type[ simplearray

Lex. An.

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

$

$

Error

Page 19: Errors in Top-Down Parsing

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

Page 20: Errors in Top-Down Parsing

Code of match (panic mode)procedure match (t: token);(*Pre: t <> $ * Never try to match end of string)begin

if lookahead = t thenlookahead := nexttoken (lexical analyzer)else if lookahead <> $ thenwriteln (‘Unexpected’, lookahead);lookahead := nexttoken;match (t)else (* lookahead = $ *)writeln (‘Unexpected end of string’)

end

Page 21: Errors in Top-Down Parsing

type → simple | ^ simple | array [simple] of typesimple → integer | char | num ptpt num

array

type

] of type[ simplearray

Lex. An.

PPT integer char num array ^ [ of ] $type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6

$

$

Error

[

Error

simple ]

Error

of

Error Error

type

Page 22: Errors in Top-Down Parsing

Main program

Beginlookahead = nexttoken;type; if lookahead = $ theninput finished (errors, if any, were marked)elseunexpected input after end of type

End.

Page 23: Errors in Top-Down Parsing
Page 24: Errors in Top-Down Parsing

PPT integer char num array ^ .. [ of ] $

type rule 1 rule 1 rule 1 rule 3 rule 2

simple rule 4 rule 5 rule 6 er

type → simple | ^ simple | array [simple] of typesimple → 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

Page 25: Errors in Top-Down Parsing

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 thenwriteln (‘Error: forgotten num’);match(ptpt); match(num);

else (* lookahead = $ *)writeln (‘Error: I couldn’t find simple’)

normal

errors


Recommended