270 views
3 3 votes

Consider the following snippet of C code

int main() { /* Line 1 */
    int a = 5, b = 10; /* Line 2 */
    if (a < b) then { /* Line 3 */
        a = a + 1; /* Line 4 */
    }
}


Identify the compiler's primary response during the compilation process:

  1. NO COMPILATION ERROR
     
  2. A LEXICAL ERROR
     
  3. A SYNTACTIC ERROR
     
  4. A SEMANTIC ERROR

3 Answers

3 3 votes

LEXICAL ANALYSIS: The scanner looks at the word $\verb|then|$. In many languages (like Pascal or Basic), $\verb|then|$ is a valid keyword. In C, the scanner simply sees the characters $\verb|t-h-e-n|$ and identifies them as a valid identifier (like a variable name). Since it is a valid sequence of characters, there is no Lexical Error.

Syntax ANALYSIS: $\verb|if(a < b) then }|$ is not a valid syntax

Either $\verb|{ }|$ should come immediately after if or there should be semicolon after then

Hence it will generate syntactic error

Correct option: C

0 0 votes

in Line 3 , then will be interpreted by lexical analysis as indentifiers
so it pass lexical analysis
in syntax analysis , it used  CFG to make it make no sense there so it throws error 
hence it is syntax error

Answer:
Position:
Show:

Related questions

4 4 votes
3 3 answers
287
287 views
GO Classes asked Dec 30, 2025
287 views
Consider the following snippet of C codeint main() { /* Line 1 */ int x = 10, y = 5; /* Line 2 */ int result; /* Line 3 */ reslt = x + y; /* Line 4 */ }Identi...
1 1 vote
2 2 answers
396
396 views
GO Classes asked Dec 30, 2025
396 views
Consider the following C program fragment processed by a compiler that follows the standard phases of compilation. Focus only on lexical analysis (token generation).int m...
2 2 votes
2 2 answers
333
333 views
GO Classes asked Dec 30, 2025
333 views
Consider the following expression:int x = 5, y = 2; int z = x+++y; /* Line 2 */How does the Lexical Analyzer generate tokens for the expression $\verb|x+++y|$ in Line $2$...
3 3 votes
3 3 answers
301
301 views
GO Classes asked Dec 30, 2025
301 views
Which of the following is typically NOT a task performed by the compiler during the token generation phase?IDENTIFYING KEYWORDS AND IDENTIFIERS CHECKING IF AN EXPRESSION ...