Machine-translated by Google. Questions are authored in English.
Top

Phases of a Compiler Questions and Answers

Answer: A

A compiler performs translation. It reads the whole source program, reports the errors it can detect, and emits an equivalent program in the target language. Execution of that target program is a separate step entirely.

Please log in or register to report this question.

Answer: B

Analysis breaks the source into pieces and builds an intermediate representation while recording information in the symbol table. Synthesis constructs the target program from that representation. The front end and back end correspond to this split.

Please log in or register to report this question.

Answer: C

The front end performs lexical, syntactic and semantic analysis and emits intermediate code. The back end takes that intermediate code and produces target code. Splitting the two allows one front end to serve several machines.

Please log in or register to report this question.

4.

Arrange the classical phases of a compiler in the order a source program passes through them.
1. semantic analysis
2. lexical analysis
3. intermediate code generation
4. syntax analysis

Answer: D

Characters become tokens in lexical analysis, tokens become a parse tree in syntax analysis, the tree is checked for meaning in semantic analysis, and only then is an intermediate representation emitted for the back end.

Please log in or register to report this question.

Answer: A

Correct answer: a stream of characters making up the source program

The lexical analyser is the first phase to touch the program, so its input is the raw character stream as it stands in the file.

Its output is a stream of tokens, which is what the parser consumes - the second option describes the input to the next phase rather than to this one. A syntax tree and three-address code are produced later still, by the parser and the intermediate code generator. Each phase's input is the previous phase's output, and the scanner sits at the head of the chain.

Please log in or register to report this question.

Answer: B

Correct answer: a sequence of tokens with their attribute values attached

The scanner groups characters into lexemes and classifies each as a token, attaching attributes - typically a pointer to the symbol table entry for an identifier, or the value of a numeric literal.

The attributes matter as much as the classification: the parser needs only the token type to decide the grammar, but later phases need the identity of the particular identifier or the value of the particular constant. The other three options name outputs of the parser, the semantic analyser and the back end.

Please log in or register to report this question.

7.

Match each phase with what it produces.
List I
A. lexical analysis
B. syntax analysis
C. semantic analysis
D. intermediate code generation
List II
1. an annotated syntax tree
2. a stream of tokens
3. three address code
4. a parse or syntax tree

Answer: C

Correct answer: A-2, B-4, C-1, D-3

Each phase transforms its predecessor's output. Lexical analysis produces a stream of tokens; syntax analysis groups them into a parse or syntax tree.

Semantic analysis decorates that tree with types, scopes and coercions, giving an annotated syntax tree. Intermediate code generation flattens it into three-address code, a machine-independent form on which optimisation can work. Reading the outputs as a sequence - characters, tokens, tree, annotated tree, three-address code - settles the whole matching.

Please log in or register to report this question.

8.

The phase that groups tokens into grammatical phrases is

Answer: D

Correct answer: syntax analysis, also known as parsing

Parsing takes the token stream and determines whether it forms a grammatically valid program, grouping tokens into phrases according to the grammar and building a tree that records the structure.

Lexical analysis works below this level, on characters within a single token. Semantic analysis works above it, on the tree the parser produced, checking meaning rather than form. Code generation is the back end. The division is the classical one between form and meaning.

Please log in or register to report this question.

Answer: A

Semantic analysis uses the syntax tree and the symbol table to check that the program is meaningful. Type checking, verifying that operands suit their operator, is its principal duty, along with inserting any required coercions.

Please log in or register to report this question.

Answer: B

Correct answer: the semantic analyser

A floating-point subscript is grammatically perfectly valid - the parser reduces it happily, since the grammar says a subscript is an expression and says nothing about its type.

What is wrong is meaning: an index must be of an integral type, and only type checking discovers that. That is the semantic analyser's work, using the types recorded in the symbol table. The pattern is general: the parser enforces form, and errors that are well-formed but nonsensical fall to semantic analysis.

Please log in or register to report this question.

Loading…
Tags: Phases of a Compiler Questions and Answers || Phases of a Compiler MCQ Questions and Answers || Phases of a Compiler GK Questions and Answers || Phases of a Compiler GK MCQ Questions || Phases of a Compiler Multiple Choice Questions and Answers || Phases of a Compiler GK || GK on Phases of a Compiler || Compiler Design Questions and Answers || Compiler Design MCQ Questions and Answers || Compiler Design GK Questions and Answers || GK on Compiler Design