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

Lexical Analysis Questions and Answers

91.

Consider the following definition of a lexical token id for an identifier in a programming language, using extended regular expressions:

letter \(\to\) [A-Za-z]
digit \(\to\) [0-9]
id \(\to\) letter (letter | digit)\(^{*}\)

Which one of the following Non-deterministic Finite-state Automata with \(\epsilon\)-transitions accepts the set of valid identifiers? (A double-circle denotes a final state)

Exam Year(s)

GATE CS - 2023,

Answer: C

The correct automaton is (C).

In (C), a letter takes the start state to the final state. From there, \(\epsilon\)-moves lead into two branches, one reading a single letter and one reading a single digit, and both rejoin a state with an \(\epsilon\)-move back to the final state. So after the first letter the machine can read any number of letters and digits in any order: exactly letter (letter | digit)*.

(A) has an \(\epsilon\)-move from the start state straight to the final state, so it also accepts the empty string and strings starting with a digit. (B) commits to one branch after the first letter, so it accepts a1 and ab but not a1b, which mixes letters and digits. (D) loops only on the pair 'letter digit', so it rejects ab.

Please log in or register to report this question.

92.

The grouping of characters into tokens are carried through :

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: C

Characters are grouped into tokens by the scanner.

The scanner, or lexical analyser, is the first phase of a compiler. It reads the source program character by character and groups the characters into tokens such as keywords, identifiers, constants and operators, removing white space and comments.

The parser takes the tokens and checks that they form valid syntax. Code optimisation improves intermediate code, and the code generator produces target code; both come much later than token formation.

Please log in or register to report this question.

93.

Which global variable in LEX points to matched string and which global variable contains the length of the matched string ?

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: C

The variables are yytext, yyleng.

In a LEX-generated scanner, yytext points to the text of the lexeme just matched, and yyleng holds its length. Actions in the rules use them, for example to copy the matched identifier or number.

yylex is the scanning function itself, which returns the next token, and yylval is the variable used to pass a token's value to a YACC parser. lextext, lexval and lexyy are not LEX's global variables (lex.yy.c is the name of the generated file).

Please log in or register to report this question.

Loading…
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