GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Locate errors in compiler analysis stages

2 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Check token formation, grammatical structure and type/context validity for given C-like snippets. Historical ambiguous lexical conventions excluded.

1 mark · MSQ
Report issue

Lexical and semantic errors in C

Lexical Analysis

Question 27, GATE 2026: 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.
Tap to enlarge
Select all that apply
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.

Original paper, page 21 · Answer key

1 mark · MCQ
Report issue

Semantic Analysis & Type Checking

Question 3, GATE 2021: ANSI C program: int main(){ Integer x; return 0; }. No declaration of Integer is supplied. Which conceptual seven-phase compiler stage reports the error? A lexical analysis; B syntax analysis; C semantic analysis; D machine-dependent optimization.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Our answer: C in the textbook phase model

Integer is an identifier rather than a built-in C type, and no typedef declaration introduces it. The intended seven-phase classification is an undeclared-type/name error detected by semantic analysis, C. Real C front ends can report a parse error because typedef-name classification interacts with parsing; the answer describes the conceptual phase classification rather than a guarantee about every compiler diagnostic.

Lexing can recognize Integer as a legal identifier, so its spelling is not itself a lexical error. Semantic analysis normally resolves declarations and type meanings. The conceptual answer should not be read as promising that a particular real C parser will proceed to a separately named semantic phase for this malformed declaration.

Original paper, page 56 · Answer key