568 views
2 2 votes

A lexical analyzer is designed for a new language with the following rules for token generation:

  1. $\mathrm{KEYWORD1}: \verb|if|$
     
  2. $\mathrm{KEYWORD2}: \verb|iff|$
     
  3. $\mathrm{IDENTIFIER}: \verb|[a-z]+|$ (One or more lowercase letters)
     
  4. $\mathrm{OPERATOR}: \verb|+| , \verb|++|$

The scanner follows the Longest Match Rule (Maximal Munch) and breaks ties using Rule Priority (the order listed above).

Consider the input string$: \verb|ifffff++|$

Which of the following statement(s) is/are TRUE regarding the tokens generated?

  1. THE SCANNER GENERATES A TOTAL OF $3$ TOKENS. 
     
  2. THE FIRST TOKEN GENERATED IS $\verb|iff| ~\mathrm{(KEYWORD2)}$. 
     
  3. THE STRING IS PRE-PROCESSED INTO $\verb|iff|$, $\verb|ffff|$, AND $\verb|++|$. 
     
  4. IF THE INPUT WAS $\verb|if+|$, THE SCANNER WOULD GENERATE $\verb|if| ~\mathrm{(KEYWORD1)}$ AND $\verb|+| ~\mathrm{(OPERATOR)}$.

3 Answers

5 5 votes

Correct answer is D.

  • From the start (i):

    • if → length 2

    • iff → length 3

    • [a–z]+ → matches the whole letter run ifffff → length 6

    • Longest is length 6 ⇒ IDENTIFIER("ifffff")

  • Remaining: ++

    • ++ vs + ⇒ longest is ++ ⇒ OPERATOR("++")

So tokens are:

  1. IDENTIFIER ifffff

  2. OPERATOR ++

Total = 2 tokens.

A. 3 tokens → False (there are 2).
B. First token is iff → False (it’s an identifier ifffff).
C. Pre-processed into iff, ffff, ++ → False (lexer doesn’t pre-split; maximal munch gives ifffff).
D. For input if+:

  • if matches KEYWORD1 and IDENTIFIER with same length (2).

  • Tie → rule priority ⇒ KEYWORD1.

  • Then + ⇒ OPERATOR.

Hence, D is true.

0 0 votes
start with i

i
keyword 1 (in the match)
keyword 2 (in the match)
keyword 3 (in the match)
keyword 4 ( out of match)

if
keyword 1 (in the match)
keyword 2 (in the match)
keyword 3 (in the match)

iff
keyword 1 (in the match)
keyword 2 (in the match)
keyword 3 (in the match)

ifff
keyword 1 (out of  match)
keyword 2 (out of  match)
keyword 3 (in the match)

iffff
keyword 3 (in the match)

ifffff
keyword 3 (in the match)

ifffff+

keyword 3 (out of match)
so ifffff ->Keyword 3

+
keyword 1  ( out of match)
keyword 2  ( out of match)
keyword 3  ( out of match)

keyword 4 (in the match)

++

keyword 1  ( out of match)
keyword 2  ( out of match)
keyword 3  ( out of match)

keyword 4 (in the match)

so ++ ->operator
2 tokens generated

let check options
A incorrect

b incorrect

c incorrect

d correct:
 i
keyword 1 (in the match)
keyword 2 (in the match)
keyword 3 (in the match)
keyword 4 ( out of match)

if

keyword 1 (in the match)
keyword 2 (in the match)
keyword 3 (in the match)
keyword 4 ( out of match)

due to priority: KEYWORD 1 has selected due to higher priority

+

keyword 1  ( out of match)
keyword 2  ( out of match)
keyword 3  ( out of match)

keyword 4 (in the match)
Answer:
Position:
Show:

Related questions

2 2 votes
2 2 answers
319
319 views
GO Classes asked Feb 4
319 views
Which of the following statement(s) is/are TRUE regarding Lexical Analysis and Regular Expressions?The number of states in a minimal Deterministic Finite Automaton (DFA) ...
2 2 votes
5 5 answers
573
573 views
GO Classes asked Feb 4
573 views
Consider the following arammar $G$ :$$\begin{aligned}& S \rightarrow(L) \mid a \\& L \rightarrow L, S \mid S\end{aligned}$$Which of the following statement(s) is/are TRUE...
1 1 vote
2 2 answers
312
312 views
GO Classes asked Feb 4
312 views
Consider the following Syntax Directed Translation (SDT) scheme where $S$ is the start symbol and $id.\mathrm{val}$ represents the numerical value of an identifier:$$\beg...
5 5 votes
2 2 answers
309
309 views
GO Classes asked Feb 4
309 views
Consider a Bottom-Up parser for a grammar $G$. During the parsing of an input string $w$, the parser reaches a configuration where the stack contains the prefix $\alpha$ ...