GATE Computer ScienceVery hardGATE CS (PYQ pattern)
A lexer uses longest match, breaking ties in favour of the rule listed first, with rules: KW = if | then; ID = [a-z][a-z0-9]*; NUM = [0-9]+(\.[0-9]+)?; REL = < | <= | <> | = | > | >=. White space separates tokens and is discarded. How many tokens does it return for the input "if1 if x<=y>=3.5<>1if=12"?
- A. 11
- B. 9
- C. 12
- D. 14
Correct answer: C. 12
Tokens: if1 if x <= y >= 3.5 <> 1 if = 12. 'if1' is one ID (longer than KW 'if'), 'if' ties and goes to KW, and '1if' splits into NUM 1 and KW if: 12 tokens.
More Compiler Design questions
- G: S → id | id [ E ] | id ( E ); E → id. G′ is obtained from G by left factoring S. Which statements are true? I. G is LL(1). II. G′ is…
- Construct the SLR(1) parsing table of S → a B | b A; A → a | a S | b A A; B → b | b S | a B B (augmented). In how many states of the…
- An NFA over {a, b} has states 0 to 4, start state 0, and these moves (a missing move means no transition): δ(0,a)={1,2}; δ(0,b)={0,1,2}…
- Build the SLR(1) table for S → ( L ) | x; L → S | L , S (augmented with S′ → S). The ACTION part has a column for each of (, ), x, , and…
- int x = 1; void p() { x = x + 10; } void r() { print(x); } void q() { int x = 5; p(); r(); } main() { q(); r(); print(x); } What is…
- Each assignment is a definition named by its statement number. In 1: s = 0; 2: i = 0; 3: L1: if i >= n goto L4; 4: if a > 0 goto L2; 5: s…