-1

I am a little confused about this question. Someone please clarify, "how does the handling of syntax errors involves the use of parser and the lexical analyzer?" A syntax error is basically handled at the parser phase of the compiler, so how does then a lexical analyzer help the parser in handling the syntax error.

  • 1
    It doesn't. The lexical analyser has nothing to do with it. The parser detects the syntax error and recovers from it by discarding tokens and state until it can find a plausible resumption point. Of course it would be possible to design it so that the lexical analyser co-operated somehow, but it's simply not necessary. – user207421 Jun 17 '20 at 06:44
  • Can't we say that when a syntax error is found, the parser asks for more tokens from the lexical analyzer, in this case it involves the use of a lexical analyzer.. It is still confusing me... Please elaborate the question a little more for my understanding. – MohsinPak Jun 27 '20 at 04:49
  • Or at least if you explain the handling process of Syntax errors, is there any use of lexical analyzer... – MohsinPak Jun 27 '20 at 04:51

1 Answers1

1

I think you may be a little confused about what the lexer's function is. The lexer converts a program into tokens:

while (1)

becomes

<TOK_WHILE> <TOK_LPAREN> <TOK_NUM, 1> <TOK_RPAREN>

Of course, the lexical can detect errors (like unexpected characters (a character is not in the set of characters recognized)), but it can't detect syntactic errors unless the design was different from traditional lexical analyzers (as @Marquis of Lorne mentioned). In short, all the lexical analyzer does (in most cases) is converting a stream of characters (a program) into tokens from the parser to check for syntactical errors (and in most cases build a parse tree, etc.).

xilpex
  • 3,097
  • 2
  • 14
  • 45
  • Does the parser take any help from the lexical analyzer in handling the syntax error,,,, I am still confused, refer me to any article or video.. I have studied the working of lexical analyzer, parser in detail, as well the error handling strategies... but still confused.. – MohsinPak Jun 27 '20 at 05:06
  • @MohsinPak -- Most of the time the parser does not take just about *any* help from the lexer. – xilpex Jun 27 '20 at 17:41