GATE / CS ARCHIVE

GATE CSE 1998 / QUESTION 22(a)

Bounded identifier regex

Theory of Computation · Regular Expressions & Languages · Full GATE CSE 1998 paper

GATE 1998, Q22(a) Descriptive
Report issue

Regular Expressions & Languages

Question 22(a), GATE 1998: An identifier has one through six letters/digits, with a letter first. Give a regular expression.
Question 22(a), GATE 1998: An identifier has one through six letters/digits, with a letter first. Give a regular expression.
Tap to enlarge
Answer & solution

Answer: L(ε|A|A²|A³|A⁴|A⁵), A=L∪D. (no official key was published)

Let L denote the finite union of permitted letters and D the union of decimal digits. Let A=L|D. The first character must be from L. It can be followed by zero through five alphanumeric characters, giving

L(ε | A | AA | AAA | AAAA | AAAAA).

With repetition shorthand this is L A{0,5}. The expanded expression above uses only ordinary regular-expression union, concatenation and ε; braces are not essential notation.

A one-letter identifier follows the ε branch; a six-character one follows AAAAA. No branch accepts an empty identifier or a first digit, and none permits a seventh character. Conversely any permitted identifier can be decomposed into its initial letter and its remaining zero to five alphanumeric symbols, so it is accepted. The actual letter alphabet, such as upper/lowercase distinctions, is a language-design input; underscores are not included because the question permits only letters and digits.

Original paper, page 8