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.

94.

The number of tokens in the following C statement is

printf("i = %d, &i = %x", i, &i);

Exam Year(s)

Tripura Public Service Commission - 2019,

Answer: C

The statement has 10 tokens.

A string literal is a single token however much it contains, so the statement splits into:

printf | ( | "i = %d, &i = %x" | , | i | , | & | i | ) | ;

That is 10 tokens. 26 and 21 come from counting the characters inside the string as separate tokens, and 3 counts only the identifiers.

The tokens fall into the usual classes: identifiers (printf, i), a string literal, punctuators ( ( ) , ; ) and the operator &.

Please log in or register to report this question.

95.

The lexical analysis for a modern computer language such as Java needs the power of which one of the following machine models in a necessary and sufficient sense ?

Exam Year(s)

Tripura Public Service Commission - 2019,

Answer: A

Lexical analysis needs finite state automata.

The tokens of a programming language, identifiers, keywords, numbers, operators and string literals, are all described by regular expressions. Every regular language is recognised by a finite automaton, so finite automata are sufficient; and since tokens form regular sets, nothing weaker would do, so they are also necessary.

Pushdown automata are needed for parsing the nested, context-free syntax, which is the next phase, and a Turing machine is far more power than lexical analysis requires.

Please log in or register to report this question.

96.

In analyzing the compilation of PL/I (Programming Language One) program, the term "Lexical analysis" is associated with

Exam Year(s)

Tripura Public Service Commission - 2019,

Answer: B

Lexical analysis is associated with recognition of basic elements and creation of uniform symbols.

In the classic model of a PL/I compiler, the lexical phase scans the source, recognises its basic elements (identifiers, literals, operators and keywords) and replaces each by a uniform symbol, entering identifiers and literals in tables for later phases.

Recognising syntactic constructs through reductions is syntax analysis, and producing optimised matrices or assembly code belongs to the optimisation and code generation phases.

Please log in or register to report this question.

97.

In a compiler, keywords of a language are recognized during

Exam Year(s)

Tripura Public Service Commission - 2025,

Answer: C

Keywords are recognised during the lexical analysis of the program.

The lexical analyser (scanner) reads the character stream and groups it into tokens such as identifiers, numbers, operators and keywords. Keywords are usually recognised there, often by first matching an identifier and then looking it up in a table of reserved words.

The parser works on the tokens the scanner has already produced, and code generation and dataflow analysis come much later in the compiler.

Please log in or register to report this question.

98.

Which of the following is not a white space character ?

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: C

The character that is not white space is the SEMICOLON.

White space characters are those that produce blank space and separate tokens: the blank (space), horizontal tab, newline, and also vertical tab, form feed and carriage return. In C the function isspace() returns true for exactly these.

The semicolon is a printing punctuation character. In C and C++ it is the statement terminator, a token in its own right, not white space.

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