views:

97

answers:

2

Should we catch errors while parsing general purpose language as early as possible (in Lex) or where it is more convenient and give us more information (in Yacc)? How various languages solve this problem?

A: 

Error handling should be done during both lexical analysis and parsing. Token Error such as invalid char should be caught as early as possible by the scanner itself. But error such as syntax errors cannot be caught by scanner, so these get handled in the parser.

codaddict
+1  A: 

In general the more complex the error, the more complex the code for asserting the error. Lexers and parsers are fairly simple (in practice), and therefore catch simple errors.

  1. Lexers catch invalid sequences of characters supposed to make up a token
  2. syntax analysis tools such as Bison/Yacc catch invalid sequences of tokens that make up syntax and statements.

Complex errors usually happen elsewhere at runtime or in various translations at compile-time. Examples might include referencing a function/method that doesn't exist. Closure scopes / binding, object and reference identifiers, argument validity, overloading, and tons of other language dependent stuff.

Anything outside that very narrow token/syntax scope is (or should be handled) well outside those tools in AST analysis or intermediate code generation.

Consider:

a.b();
ab();

Both should make it through the lexer/parser for an object oriented language where both statements are valid. is there an error?

  1. Your compiler might assert this at compile time if the language is a fairly static one and identifiers can be resolved at compile time.

  2. You might replace both statements with ID resolution code to be run at runtime to produce a runtime error rather than a compiler error.

The differences between runtime and compile-time resolution and semantics can be subtle and widely different from language to language.

Errors are usually caught when they can be known to be errors and where you have the most information. This varies between languages and implementations.

Aiden Bell
There is not contact information in your profile therefore I leave this comment here:Dragon Book's right name is "Compilers: Principles, Techniques, and Tools"
DSblizzard
@DSblizzard - I know, I own two copies ;)
Aiden Bell