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

Phases of a Compiler Questions and Answers

41.

Consider the following statements about compiler phases.
1. The optimisation phase is optional in a working compiler.
2. The symbol table manager interacts with almost every phase.
3. Semantic analysis precedes syntax analysis in the usual order.

Answer: A

Correct answer: 1 and 2 only

The first two are correct. Optimisation is optional - a compiler that omits it produces correct if slower code, which is why compilers offer optimisation levels. And the symbol table manager interacts with almost every phase.

The third reverses the order. Syntax analysis comes first and builds the tree; semantic analysis then walks that tree checking types and scopes. There is no tree for semantic analysis to work on until the parser has produced one.

Please log in or register to report this question.

Answer: B

An optimisation that changes observable behaviour is a bug, not an improvement. Every transformation must be shown to be semantics preserving for all inputs, which is why the safe set of optimisations is more limited than it first appears.

Please log in or register to report this question.

Answer: C

If both operands are known at compile time the compiler can compute the result and substitute it. The runtime cost of the operation disappears entirely, and further optimisations often become possible once the constant is visible.

Please log in or register to report this question.

Answer: D

Correct answer: the operands of the repeated expression have not changed in between

Reusing a computed value is safe only if the value is still the same, so the compiler must establish that nothing between the two occurrences assigned to any operand.

That is a data-flow question, and an assignment through a pointer or a call to a function that might modify a variable will defeat it - which is why aliasing makes the analysis conservative. The number of occurrences is irrelevant, constants are the domain of folding, and register pressure affects whether the saving is worth taking rather than whether it is legal.

Please log in or register to report this question.

Answer: A

Correct answer: code whose results are never used or that cannot be reached

Dead code is of two kinds and the option names both: unreachable code, which no path can arrive at, and code whose computed results are never subsequently used.

Commented-out text never reaches the compiler at all, having been removed by the scanner. Code after a return within a block is one instance of unreachability rather than the whole of the definition, and a procedure called once is a candidate for inlining, not for removal.

Please log in or register to report this question.

46.

State whether the following is true or false.
An optimising compiler always produces the fastest possible target program for a given source.

Answer: B

Correct answer: False

Optimal code generation is undecidable in general, and the individual subproblems - register allocation and instruction scheduling among them - are NP-hard. Compilers therefore use heuristics that usually improve the program without any guarantee of optimality.

Optimisations also interact: one may block another, and an apparently local improvement can worsen cache or branch behaviour. "Optimising" describes the attempt rather than the outcome, which is why compilers offer several levels rather than one correct answer.

Please log in or register to report this question.

Answer: C

Correct answer: lexical analysis

Scanning and lexical analysis are the same activity under two names, which is why the component is called either a scanner or a lexer. It reads the character stream and produces tokens.

The names reflect the mechanism: it scans the input left to right, matching the longest lexeme against each token's pattern. Parsing is the term for syntax analysis, and semantic analysis and linking are quite separate stages later in the chain.

Please log in or register to report this question.

Answer: D

Correct answer: the actual sequence of characters that forms an instance of a token

A lexeme is the concrete text in the source - count, 42, while. The token is the category it belongs to, such as identifier or number.

So count and total are two lexemes of the same token. The pattern is the rule describing which lexemes a token admits, and the attribute is the extra information carried along, typically a symbol table pointer. Four terms for four distinct things, and confusing lexeme with token is the usual slip.

Please log in or register to report this question.

49.

Match each term with its correct description.
List I
A. token
B. lexeme
C. pattern
D. attribute
List II
1. the rule describing what lexemes a token matches
2. a category such as identifier or number
3. extra information such as a symbol table pointer
4. the actual characters matched in the source

Answer: A

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

The four terms describe one relationship from four sides. A token is the category - identifier, number, keyword. A lexeme is the actual characters matched in the source.

A pattern is the rule, usually a regular expression, describing which lexemes a token admits. An attribute is the extra information carried with the token, such as a symbol table pointer or a literal's value. Category, instance, rule and payload - keeping those four ideas apart is the whole of the matching.

Please log in or register to report this question.

Answer: B

Correct answer: omits the nodes that carry no meaning beyond the grammar itself

A parse tree records every production applied, including the chains of single-child nodes that grammars need to express precedence and associativity. Most of those nodes convey nothing about the program.

An abstract syntax tree keeps only what matters - operators and operands - so it has fewer nodes, not more, and is the form later phases actually walk. Types are added by semantic analysis afterwards, and both trees are built by or from the parser.

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