Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UP Lecture 13

UP Lecture 13

Compilers
Error Handling I
(202403)

Javier Gonzalez-Sanchez

December 16, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs Rules PROGRAM

    BODY ASSIGNMENT VARIABLE WHILE IF RETURN PRINT C EXPRESSION X Y R E A B
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Grammar <PROGRAM>

    à '{' <BODY> '}’ <BODY> à {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|<WHILE>|<IF>|<RETURN>';'} <ASSIGNMENT> à identifier '=' <EXPRESSION> <VARIABLE> à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier <WHILE> à 'while' '(' <EXPRESSION> ')' <PROGRAM> <IF> à 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>] <RETURN> à 'return' <PRINT> à ’print’ ‘(‘ <EXPRESSION> ‘)’ <EXPRESSION> à <X> {'|' <X>} <X> à <Y> {'&' <Y>} <Y> à ['!'] <R> <R> à <E> {('>'|'<'|'=='|'!=') <E>} <E> à <A> {(’+'|'-’) <A>} <A> à <B> {('*'|'/') <B>} <B> à ['-'] <C> <C> à integer | octal | hexadecimal | binary | true | false | string | char | float | identifier|'(' <EXPRESSION> ')'
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs Assignment 2

    | Code public static void RULE_BODY() { while (!tokens.get(currentToken).getWord().equals(“}”)) { if (tokens.get(currentToken).getToken().equals(“identifier”)) { RULE_ASSIGNMENT(); if (tokens.get(currentToken).getWord().equals(“;”)) { currentToken++; else error(3); } else if (tokens.get(currentToken).getToken().equals(“int”) | ...) { RULE_VARIABLE(); if (tokens.get(currentToken).getWord().equals(";")) { currentToken++; else error(3); } else if (tokens.get(currentToken).getWord().equals(“while”)) { RULE_WHILE(); } else if (tokens.get(currentToken).getWord().equals(“if”)) { RULE_IF(); } else if (tokens.get(currentToken).getWord().equals(“return”)) { RULE_RETURN(); if (tokens.get(currentToken).getWord().equals(“;”)) { currentToken++; else error(3); } else error(4); } } BODY
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs 1 •Understand

    de provided source code (3 rules) 2 •Program the rules PROGRAM, BODY, EXPRESSION, X, Y, R, E, C (11 rules) 3 •Program the full set of rules in the grammar (16 rules) 4 •Report syntactical errors (one error and stop) PANIC MODE 5 •Implement error synchronization •ERROR RECOVERY What Next
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Programming Assignment

    2 Level 4 Handling Syntactical Errors (part 1): Error messages
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Grammar PROGRAM

    BODY ASSIGNMENT VARIABLE WHILE IF RETURN PRINT C EXPRESSION X Y R E A B
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Assignment 2

    { hello word } Input: Line 2: expected = Line 2: expected ; Output:
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Assignment 2

    { int x int int int x; } Input: Line 2: expected ; Line 3: expected identifier Line 3: expected ; Line 4: expected identifier Output:
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs Assignment 2

    { x = a; x = 0x36AW; x = ((((((((((y)))))))))); x = (5+(4-(3+(5+5/(2+(3+(1+(77+(1-(y)))))))))) + “hello” + ‘q’; if (a < b) {} else {} if (a < b) { if (a < b) { } else { } } } Input: Line 3: expected value, identifier or ( Output:
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs Parser |

    Error Points PROGRAM Line N: expected { Line N: expected }
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Parser |

    Error Points Line N: expected ; BODY Line N: expected identifier or keyword
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Parser |

    Error Points ASSIGNMENT Line N: expected =
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Parser |

    Error Points VARIABLE Line N: expected identifier
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Parser |

    Error Points WHILE Line N: expected ( Line N: expected )
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Parser |

    Error Points IF Line N: expected ( Line N: expected )
  16. Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Parser |

    Error Points PRINT Line N: expected ( Line N: expected )
  17. Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs Parser |

    Error Points EXPRESSION X Y R E A B
  18. Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Parser |

    Error Points C Line N: expected value, identifier or ( Line N: expected )
  19. Dr. Javier Gonzalez-Sanchez | Compilers | 25 jgs Assignment 2

    public static void error(int err) { int n = tokens.get(currentToken).getLine(); switch (err) { case 1: gui.writeConsole("Line” + n + ": expected {”); break; case 2: gui.writeConsole("Line” + n + ": expected }”); break; case 3: gui.writeConsole("Line” + n + ": expected ;”); break; case 4: gui.writeConsole("Line” +n+": expected identifier or keyword”); break; case 5: gui.writeConsole("Line” +n+": expected =”); break; case 6: gui.writeConsole("Line” +n+": expected identifier”); break; case 7: gui.writeConsole("Line” +n+": expected )”); break; case 8: gui.writeConsole("Line” +n+": expected (”); break; case 9: gui.writeConsole("Line” +n+": expected value, identifier, (”); break; } }
  20. jgs Compilers Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2024 Copyright. These

    slides can only be used as study material for the Compilers course at Universidad Panamericana. They cannot be distributed or used for another purpose.