Discuss different types of common programming errors.

Types of Common Programming Errors

When a program is written, errors are often unavoidable, no matter how careful the programmer is. These errors may occur during:

  • Lexical analysis
  • Syntax analysis
  • Semantic analysis
  • Or even during execution

Below are the four main types of programming errors typically encountered in compiler design:

1. Lexical Errors

These are errors in the spelling or format of tokens during lexical analysis.

Examples:

  • Misspelling a keyword or identifier:
    • Typing elipseSize instead of ellipseSize
  • Using invalid or missing characters:
    • "Hello (missing closing quote for a string)
    • $value (invalid character in variable name)

Detected by:

Lexical analyzer

2. Syntactic Errors

These errors violate the grammar rules of the programming language.

Examples:

  • Missing or extra semicolons, brackets, braces:
  • case used without a switch:

3. Semantic Errors

These are meaning-related errors – the code is syntactically correct but makes no sense logically.

Examples:

  • Type mismatch:
  • Returning a value in a void function:

Detected by:

Semantic analyzer

4. Logical Errors

These are hardest to detect because the code is syntactically and semantically valid, but it doesn’t do what the programmer intended.

Examples:

  • Using = (assignment) instead of == (comparison):
  • Incorrect use of loops or conditions that cause wrong output or infinite loops.

Detected by:

Usually caught only during testing or debugging

Error Detection & Recovery Goals

The compiler’s error handler aims to:

  1. Clearly report the location and type of error
  2. Recover quickly so that it can detect more errors later
  3. Add minimal overhead to the compilation of correct code

Summary Table

Type of ErrorDescriptionExampleDetected By
Lexical ErrorMistakes in spelling of tokenselipseSize, "HelloLexical Analyzer
Syntax ErrorStructure/grammar violationsMissing ;, case without switchParser
Semantic ErrorType mismatches, invalid returnsreturn 5; in void methodSemantic Analyzer
Logical ErrorCode runs, but logic is flawedif (x = 5) instead of x == 5Testing/Debugging

Leave a Reply

Your email address will not be published. Required fields are marked *