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

Lexical Analysis Questions and Answers

Answer: A

Correct answer: when the corresponding pattern matches the longest available lexeme

Lex applies two rules in order: match the longest possible lexeme, and where two patterns match a lexeme of the same length, prefer the one written earlier. Only then is the action for the winning rule executed. This is why keyword patterns are written before the identifier pattern - both match if, and order decides.

Please log in or register to report this question.

42.

In a Lex action the variable holding the matched text is conventionally

Answer: B

yytext points at the matched lexeme and yyleng gives its length. yylval carries the attribute value to the parser and yylineno tracks the line, so each has a distinct role. Confusing yytext with yylval is a frequent source of bugs in hand edited scanners.

Please log in or register to report this question.

43.

Consider the following statements about scanner generators.
1. They resolve ties between equal length matches by rule order.
2. They apply the longest match rule when patterns overlap.
3. They require the token patterns to be context free grammars.

Answer: C

Correct answer: 1 and 2 only

Statements 1 and 2 are the two disambiguation rules, and they are applied in that order: longest match first, then rule order to settle a tie. Statement 3 is wrong - the patterns are regular expressions, not context free grammars, which is precisely what makes the mechanical construction of an automaton possible.

Please log in or register to report this question.

Answer: D

Correct answer: the transition function of the automaton as a two dimensional array

A table-driven scanner keeps the automaton as a table indexed by current state and input character, whose entry is the next state. The driver is then a short loop: look up, move, repeat. The approach separates the automaton from the code that runs it, which is why generated scanners take this form and why table compression matters.

Please log in or register to report this question.

Answer: A

A row for every state crossed with every character in the alphabet grows quickly. Techniques such as default rows and displacement tables cut the space greatly at a small cost in access time. The saving matters most for scanners with a full byte alphabet and many states.

Please log in or register to report this question.

Answer: B

An illegal character or an unterminated literal is a lexical error. Ordering faults are syntactic and type faults semantic, and each is caught by the corresponding later phase. Assigning each error to the earliest phase able to detect it gives the best diagnostics.

Please log in or register to report this question.

Answer: C

The first three concern the formation of individual lexemes. A missing brace leaves every lexeme valid and is detected by the parser when the token sequence no longer matches the grammar. The scanner happily produced every token; only their arrangement is at fault.

Please log in or register to report this question.

Answer: D

Correct answer: deleting characters until a well formed lexeme can be recognised

Panic mode is the simplest recovery a scanner can offer: on encountering characters that form no legal lexeme, discard them one at a time until the remaining input begins something recognisable, report the error, and carry on. It is crude but it lets the compiler find later errors instead of stopping at the first.

Please log in or register to report this question.

Answer: A

Insertions, deletions, replacements and transpositions are counted and the cheapest repair is chosen. It gives good diagnostics but is expensive, so most scanners use simpler heuristics. The extra cost is hard to justify when a plain message names the offending character.

Please log in or register to report this question.

Answer: B

Correct answer: it simplifies the design and lets each use its most suitable notation

Separating the two lets the scanner use regular expressions, which are simple and fast, for the parts of the language that are regular, and lets the parser use a context free grammar for the parts that are not. It also confines the handling of white space, comments and buffering to one place, and it makes both halves easier to test and to reuse.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Lexical Analysis Questions and Answers || Lexical Analysis MCQ Questions and Answers || Lexical Analysis GK Questions and Answers || Lexical Analysis GK MCQ Questions || Lexical Analysis Multiple Choice Questions and Answers || Lexical Analysis GK || GK on Lexical Analysis || Compiler Design Questions and Answers || Compiler Design MCQ Questions and Answers || Compiler Design GK Questions and Answers || GK on Compiler Design