CS Atlas GATE 2027
Compiler Design course
0 / 90 complete

Compiler Design/Phase 1 · Lexical Analysis

Checkpoint CP1 · Item 20 of 90

Lexical analysis

Read at your own pace. Try the questions before opening the answers.

On this page

⏱️

15 questions · 20 marks · 45 minutes. Negative marking on MCQ only: −1/3 mark for a wrong 1-mark MCQ, −2/3 for a wrong 2-mark MCQ. No negative marking for MSQ or NAT.

Q1NAT1 markToken count

How many tokens are in x = a+++++b;?

Numerical answer: ________

Q2NAT2 marksToken count

How many tokens are in this fragment?

main() {
  int i, n = 10; /* count */
  for (i = 0; i < n; i++)
    printf("%d\n", i * i);
}

Numerical answer: ________

Q3MCQ1 markTerms

Which is a token name rather than a lexeme?

  • (A) count
  • (B) id
  • (C) >=
  • (D) 42

Q4MSQ1 markLexer jobs

Which does the lexical analyzer do?

  • (A) strip comments
  • (B) build the grouping structure of statements
  • (C) install identifiers in the symbol table
  • (D) return tokens when the parser asks

Q5MCQ1 markLongest match

How is x---y split?

  • (A) x - -- y
  • (B) x -- - y
  • (C) x - - - y
  • (D) x --- y

Q6MCQ1 markPatterns

Which lexeme is not matched by [A-Za-z_][A-Za-z0-9_]*?

  • (A) _x
  • (B) x_1
  • (C) 1_x
  • (D) X

Q7MSQ2 marksNumber pattern

Under digits (. digits)? (E [+-]? digits)?, which are numbers?

  • (A) 007
  • (B) 7.5E+2
  • (C) 7.E2
  • (D) 75E2

Q8MCQ2 marksLongest match

Patterns in priority order: T1 = a(a|b)*, T2 = b+, T3 = (a|b)*c. Output for bbacab?

  • (A) T3(bbac) T1(ab)
  • (B) T2(bb) T1(acab)
  • (C) T2(bb) T3(ac) T1(ab)
  • (D) T3(bbacab)

Q9MCQ1 markRules

The priority rule is applied when:

  • (A) two patterns match prefixes of different lengths
  • (B) two patterns match the same longest prefix
  • (C) no pattern matches
  • (D) the input is empty

Q10NAT1 markRetract

Tokenizing a=b with the relop/assign diagram: how many characters does the = branch retract?

Numerical answer: ________

Q11MCQ2 marksLex

A Lex file lists, in order: int|float → KW; [a-z][a-z0-9]* → ID; [0-9]+(\.[0-9]+)? → NUM; ==|[=+*] → OP; blanks skipped. How many tokens are produced for int intx == 3.5 + float2?

  • (A) 5
  • (B) 6
  • (C) 7
  • (D) 8

Q12MCQ1 markErrors

Which error is detected by the lexical analyzer?

  • (A) x = y +;
  • (B) int x; x = "a" * 2;
  • (C) x = 3 @ 4;
  • (D) return; in a function that must return int

Q13MSQ1 markLex

Which are true?

  • (A) yytext holds the lexeme just matched.
  • (B) Lex generates lex.yy.c.
  • (C) Lex uses the shortest match.
  • (D) An action can return a token to the parser.

Q14NAT2 marksToken count

How many tokens are in a = b ? c : d->e; plus while (i = 10) i++; together?

Numerical answer: ________

Q15MCQ1 markAttributes

The attribute of the token for <= in the relop design is:

  • (A) the characters <=
  • (B) LE
  • (C) a symbol table pointer
  • (D) none

Answer Vault

Try the questions first. Open when you are ready to check.

Q1 Answer: 81 mark

x · = · a · ++ · ++ · + · b · ; = 8.

Re-read: Lesson 1.1.2 — Counting tokens in C code: whitespace, comments, string literals, ++ and >=

Q2 Answer: 342 marks

Comments vanish; the format string is one token. Line by line: 4 + 7 + 13 + 9 + 1 = 34.

Re-read: Lesson 1.1.2 — Counting tokens in C code: whitespace, comments, string literals, ++ and >=

Q3 Answer: (B)1 mark

id names a kind of token; the others are actual character sequences.

  • (A) is wrong: A lexeme of token id.
  • (C) is wrong: A lexeme of token relop.
  • (D) is wrong: A lexeme of token num.

Re-read: Lesson 1.1.1 — Tokens, lexemes, patterns and attributes; how the lexer works with the parser and symbol table

Q4 Answer: (A) (C) (D)1 mark

Grouping tokens is the parser’s job.

Re-read: Lesson 1.1.1 — Tokens, lexemes, patterns and attributes; how the lexer works with the parser and symbol table

Q5 Answer: (B)1 mark

Longest match: -- first, then -.

  • (A) is wrong: The first two - form --.
  • (C) is wrong: Ignores the -- operator.
  • (D) is wrong: No --- operator.

Re-read: Lesson 1.1.2 — Counting tokens in C code: whitespace, comments, string literals, ++ and >=

Q6 Answer: (C)1 mark

Must start with a letter or underscore.

  • (A) is wrong: Starts with _.
  • (B) is wrong: Starts with a letter.
  • (D) is wrong: A single letter.

Re-read: Lesson 1.2.1 — Writing token definitions as regular expressions

Q7 Answer: (A) (B) (D)2 marks

7.E2 has a dot without following digits.

Re-read: Lesson 1.2.1 — Writing token definitions as regular expressions

Q8 Answer: (A)2 marks

At position 1, T3 takes bbac (4) vs T2’s bb (2). Then T1 takes ab: T3(bbac) T1(ab).

  • (B) is wrong: T1 cannot contain c.
  • (C) is wrong: T3 is longer at position 1.
  • (D) is wrong: T3 must end at the c.

Re-read: Lesson 1.2.2 — Transition diagrams; longest match and priority rules; lookahead

Q9 Answer: (B)1 mark

Priority breaks ties of equal length.

  • (A) is wrong: Longest match decides that.
  • (C) is wrong: That is an error.
  • (D) is wrong: Nothing to decide.

Re-read: Lesson 1.2.2 — Transition diagrams; longest match and priority rules; lookahead

Q10 Answer: 11 mark

After =, the next character b is “other”: state 6*, retract 1.

Re-read: Lesson 1.2.2 — Transition diagrams; longest match and priority rules; lookahead

Q11 Answer: (B)2 marks

KW(int) ID(intx) OP(==) NUM(3.5) OP(+) ID(float2) = 6. Note intx and float2 are IDs by longest match, and == is one OP.

  • (A) is wrong: Count again: int, intx, ==, 3.5, +, float2.
  • (C) is wrong: == is one token by longest match.
  • (D) is wrong: 3.5 is one NUM and float2 one ID.

Re-read: Lesson 1.3 — Lexical errors and recovery; how a lexer generator (Lex/Flex) is structured

Q12 Answer: (C)1 mark

@ fits no C token pattern.

  • (A) is wrong: Syntax.
  • (B) is wrong: Semantic (type).
  • (D) is wrong: Semantic.

Re-read: Lesson 1.3 — Lexical errors and recovery; how a lexer generator (Lex/Flex) is structured

Q13 Answer: (A) (B) (D)1 mark

Lex uses the longest match.

Re-read: Lesson 1.3 — Lexical errors and recovery; how a lexer generator (Lex/Flex) is structured

Q14 Answer: 192 marks

10 + 9 = 19.

Re-read: Lesson 1.1.2 — Counting tokens in C code: whitespace, comments, string literals, ++ and >=

Q15 Answer: (B)1 mark

relop tokens carry which operator: LT, LE, EQ, NE, GT, GE.

  • (A) is wrong: That is the lexeme.
  • (C) is wrong: Operators are not stored in the symbol table.
  • (D) is wrong: The parser and later phases need to know which relop.

Re-read: Lesson 1.1.1 — Tokens, lexemes, patterns and attributes; how the lexer works with the parser and symbol table

Wrong on Q → re-read

QuestionTopicRe-read
Q1Token countLesson 1.1.2
Q2Token countLesson 1.1.2
Q3TermsLesson 1.1.1
Q4Lexer jobsLesson 1.1.1
Q5Longest matchLesson 1.1.2
Q6PatternsLesson 1.2.1
Q7Number patternLesson 1.2.1
Q8Longest matchLesson 1.2.2
Q9RulesLesson 1.2.2
Q10RetractLesson 1.2.2
Q11LexLesson 1.3
Q12ErrorsLesson 1.3
Q13LexLesson 1.3
Q14Token countLesson 1.1.2
Q15AttributesLesson 1.1.1

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.