user4791206
user4791206

Reputation:

Lexical error vs syntax error in C

Consider line number 3 of the following C-program.

int main() {            /*Line 1 */
int I, N;               /*Line 2 */
fro (I=0, I<N, I++);    /*Line 3 */
}

Identify the compiler’s response about this line while creating the object-module:

  1. No compilation error
  2. Only a lexical error
  3. Only syntactic errors
  4. Both lexical and syntactic errors

My attempt :

Line 3 has fro instead of for keyword of C. Since, lexical analyzer doesn't bother about wrong keyword, that job of syntax analyzer to ensure correct syntax or keyword (i.e. for() instead of fro()). Lexical analyzer does tokenization of of program.

Can you explain in formal way please?

Upvotes: 3

Views: 4183

Answers (1)

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43548

fro will not match a keyword, but the lexing will go OK and fro will be classified as an identifier.

At the parsing stage, fro (I=0, I<N, I++) will be transformed to a function call with three arguments. Note that modifying and reading the I variable within the same sequence point is undefined behaviour.

During the scope analysis the compiler will complain that no such function exists, or, in case the compiler treats unseen functions as implicitly declared, the linker will complain that there is an undefined reference to a function.

Upvotes: 6

Related Questions