COMPILER ASSIG
COMPILER ASSIG
(KIOT)
SCHOOL OF ELECTRICAL AND
COMPUTRE ENGINEERING
COMPILR DESIGN ASSIGNEMENT
NAME ID
1.GETACHEW ADINO 4616/12
2.MILKESA LELISA 1806/13
3.MILIAN MOHAMMED 4615/13
Error recovery in syntax analysis
Error recovery in syntax analysis is crucial for compilers and interpreters to handle
syntax errors gracefully and continue processing input. Here are some common strategies used
for error recovery during syntax analysis:
❖ How It Works: When the parser encounters an error, it enters a "panic mode" where it
discards tokens until it finds a specific set of tokens that are considered safe to resume
parsing. These tokens are often chosen based on the grammar's structure, such as
statement terminators (e.g., semicolons) or block delimiters (e.g., braces).
❖ Description: The parser attempts to recover from an error by making local corrections
to the input. This can involve inserting, deleting, or replacing tokens to create a valid
phrase.
❖ Advantages: Can produce more meaningful error messages and maintain more context
than panic mode.
❖ Disadvantages: More complex to implement and may still lead to further errors if the
recovery is not handled carefully.
❖ Example: If a parser expects a semicolon after a statement and encounters an
unexpected token, it might skip tokens until it finds a semicolon or a closing brace.
➢ How It Works: This strategy involves making localized corrections to the input when an
error is detected. The parser can insert, delete, or replace tokens to create a valid
phrase that fits the expected grammar.
➢ Description: The parser attempts to recover from an error by making local corrections
to the input. This can involve inserting, deleting, or replacing tokens to create a valid
phrase.
➢ Advantages: Can produce more meaningful error messages and maintain more context
than panic mode.
➢ Disadvantages: More complex to implement and may still lead to further errors if the
recovery is not handled carefully.
➢ Example: If the parser sees an unexpected token where it expects an identifier, it might
suggest that the user intended to write an identifier and could insert one or replace the
unexpected token with a valid identifier.
3.Error Productions
✓ How It Works: The grammar is extended with additional productions that account for
common errors. These productions can provide alternative parsing paths when an error
is detected
✓ Description: The grammar is augmented with specific productions that can handle
common errors. These productions can allow for alternative parsing paths when an
error is detected.
✓ Advantages: Provides a structured way to handle specific errors and can lead to more
informative error messages.
✓ Disadvantages: Increases the complexity of the grammar and may not cover all possible
errors.
✓ Example: If a parser expects an expression but encounters an error, it might have a
production that allows for an optional expression or a placeholder for an expected
token.
4. Backtracking
• How It Works: The parser explores multiple parsing paths and, upon encountering an
error, can backtrack to a previous state to try a different path. This is often
implemented using recursive descent parsers.
• Description: The parser explores multiple parsing paths and backtracks when it
encounters an error, trying alternative paths to find a valid parse.
• Advantages: Can find valid parses even in the presence of errors and can provide
detailed error information.
• Disadvantages: Can be computationally expensive and may lead to performance issues,
especially in ambiguous grammars.
• Example: If a parser is trying to parse a list of expressions and encounters an error, it
might backtrack to the last valid state and try to parse the next expression differently.
5.Error Reporting
▪ How It Works: Alongside recovery strategies, effective error reporting is crucial. This
involves providing clear and informative messages about the nature of the error,
including the type of error and its location in the source code.
▪ Description: Alongside recovery strategies, effective error reporting is essential. This
includes providing clear messages about the nature of the error and the location in the
source code.
▪ Advantages: Helps users understand what went wrong and how to fix it, improving the
overall user experience.
▪ Disadvantages: Requires careful design to ensure that messages are informative
without being overwhelming.
▪ Example: If a user forgets a semicolon, the parser might report: "Error: Missing
semicolon at line 10, column 5."
6.Global Recovery
❖ How It Works: After encountering an error, the parser analyzes the entire input to find a
point where it can resume parsing. This often involves looking for complete statements
or blocks that can be parsed independently of the surrounding context.
❖ Description: After encountering an error, the parser attempts to analyze the entire
input to find a point where it can resume parsing, often by looking for a complete
statement or block.
❖ Advantages: Can provide a more holistic view of the input and may lead to better
recovery in complex scenarios.
❖ Disadvantages: Can be more complex to implement and may not be efficient for large
inputs.
❖ Example: If a parser encounters an error in the middle of a function definition, it might
look for the next function definition or a closing brace to resume parsing.