383 views
2 2 votes

Consider the following lexical specification for a scanner:

  1. $\mathrm{KEYWORD\_IF: \verb|if|}$
     
  2. $\mathrm{ID: [a-z][0-9]*}$
     
  3. $\mathrm{NUM: [0-9]+}$
     

The scanner follows the Longest Match rule. In case of a tie in length, the token defined first in the specification takes priority. How many tokens will be generated for the input string "$\verb|if20if|$"?

3 Answers

1 1 vote
Since $[a-z][0-9]^*$ allows only one letter followed by digits, $\texttt{if20}$ is not an $\texttt{ID}$.

Hence, $\texttt{if20if} \Rightarrow \texttt{if} \;|\; \texttt{20} \;|\; \texttt{if}$

So, the number of tokens generated is $\boxed{3}$.
• edited by
0 0 votes

Answer : 3 Tokens

                 if20if :                           KEYWORD_IF   ----->   if

                                                     while, ID -----> i

                   According to Longest Match Rule KEYWORD_IF generated as first token

 

                20if:                                NUM -----> 20 

                                       NUM generated as second token 

 

and for      if :                     KEYWORD_IF -----> if

                            Again,KEYWORD_IF generated as thrd token

Hence,  Total 3 tokens generated

Answer:
Position:
Show:

Related questions

1 1 vote
2 2 answers
312
312 views
GO Classes asked Jan 27
312 views
Consider the following Syntax-Directed Translation (SDT) scheme with the grammar rules and actions provided below:$$\begin{aligned}& S \rightarrow S_1 \# T\left\{S . \mat...
0 0 votes
1 1 answer
359
359 views
GO Classes asked Jan 27
359 views
Consider the following sequence of three-address code (TAC) instructions:1. i = 1 2. j = 1 3. t1 = 5 * i 4. t2 = t1 + j 5. a[t2] = 0 6. j = j + 1 7. if j <= 5 goto (3) 8....
2 2 votes
2 2 answers
348
348 views
GO Classes asked Jan 27
348 views
Consider the following grammar $G$ with start symbol $S$ :$$\begin{aligned}& S \rightarrow L=R \\& S \rightarrow R \\& L \rightarrow * R \\& L \rightarrow i d . \\& R \ri...
1 1 vote
1 1 answer
322
322 views
GO Classes asked Jan 27
322 views
Consider the following basic block of Three-Address Code:1. a = 1 2. b = a + 1 3. c = a + b 4. a = c + b 5. return aA variable is live at a point if its current value wil...