Similar questions 1 more →
Lexical Analysis

Answer & solution
Official answer: C
Givens and goal
Three declarations are considered: S1 begins char *str1 = "Hello; without a closing double quote; S2 is char *str2 = "Hello;"; and S3 is int *str3 = "Hello";. The options classify S1/S2 as lexical or syntactic errors and S3 as syntactic or semantic.
Approach
Check whether valid tokens can be formed first, then whether the token sequence is grammatical, then whether the types are compatible.
Step-by-step solution
1. S1 opens a string literal but does not close it before the end of the line. Tokenization cannot form a valid string-literal token. In the compiler-phase classification used by the question, this is a lexical error.
2. S2 contains a properly closed string. Its semicolon inside the quotation marks is part of the string data, while the semicolon outside terminates the declaration. There is no lexical or syntactic error merely because both semicolons appear.
3. S3 has the shape of a grammatical declaration, but initializes an int pointer with a string literal, whose ordinary expression conversion produces a char pointer. The incompatible pointer types violate the C initialization constraints. This is a type/semantic error in the question's classification.
4. Therefore C correctly pairs the lexical error in S1 with the semantic error in S3. A and B misclassify S2, and D assigns the wrong compiler phase to S1.
Precision caveat: Real compilers may emit several cascading diagnostics after the malformed string. This solution classifies the primary defect, rather than treating every emitted diagnostic as a separate intended answer. In C, accepting a program after a warning or as an extension does not make a constraint violation valid standard C.
Phase distinction: lexical analysis groups characters into tokens, syntax checks token arrangements against the grammar, and semantic analysis checks constraints such as types. A semicolon character inside a valid string token has no role as a declaration terminator. That is why S2’s internal semicolon cannot by itself create a syntax error.