During which phase of a compiler is the grammar of a program checked?

2010

During which phase of a compiler is the grammar of a program checked?

Answer: C. Syntax analysisConceptA compiler front end processes source code in successive stages. Lexical analysis forms tokens, syntax analysis parses those tokens according to a…

  1. A.

    Semantic analysis

  2. B.

    Code generation

  3. C.

    Syntax analysis

  4. D.

    Code optimization

Attempted by 175 students.

Show answer & explanation

Correct answer: C

Concept

A compiler front end processes source code in successive stages. Lexical analysis forms tokens, syntax analysis parses those tokens according to a grammar, and semantic analysis checks meaning-related constraints such as types and declarations.

A programming-language grammar consists of production rules. A parser checks whether the token sequence can be organized according to those rules and constructs a parse tree or an abstract syntax tree.

Application

The stem asks specifically about checking the grammar of a program. That operation is parsing, so it belongs to syntax analysis.

Contrast

  • Semantic analysis checks type compatibility, declarations, and other meaning-related rules after a parse structure exists.

  • Code generation converts an intermediate representation into target code.

  • Syntax analysis checks grammatical structure by parsing tokens with grammar productions.

  • Code optimization improves a representation without changing program behavior.

Therefore, the grammar of a program is checked during syntax analysis.

Explore the full course: Mppsc Assistant Professor Computer Science Paper 2

Loading lesson…