Skip to main content

GATE CSE 2026Compiler Design › Lexical Analysis, Compilation Phases & LinkingVerified

GATE CSE 2026 PYQ: Consider the following C statements: char * str1 = "Hello; /* Statement S1 */ char *str2 = " Hello ; "; /*…

Consider the following C statements:

  1. char *str1 = "Hello; /* Statement S1 */
  2. char *str2 = "Hello;"; /* Statement S2 */
  3. int *str3 = "Hello"; /* Statement S3 */

Which of the following options is/are correct?

  1. A
    S1\text{S1} and S2\text{S2} have syntactic errors
  2. B
    S2\text{S2} has a lexical error and S3\text{S3} has a syntactic error
  3. C
    S1\text{S1} has a lexical error and S3\text{S3} has a semantic error
    Correct answer
  4. D
    S1\text{S1} has a syntactic error and S3\text{S3} has a semantic error

Answer

(C) S1 \text{S1} S1 has a lexical error and S3 \text{S3} S3 has a semantic error

Approach

Classify each C statement by which compiler phase would flag it: a token that cannot even be formed (e.g. an unterminated string) is a lexical error; a well-formed but grammatically illegal construction is a syntactic error; a well-formed, grammatically legal statement that violates type rules is a semantic error. Then match the resulting classification against each option.

Statement S1 contains an unterminated string literal, which the lexical analyzer cannot tokenize, giving a lexical error. Statement S2 is a valid assignment of the string "Hello;" to a char pointer and has no error. Statement S3 is grammatically correct but assigns a char* literal to an int* variable, a type mismatch detected during semantic analysis. Hence S1 has a lexical error and S3 has a semantic error, matching option C.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.