In some programming language, an identifier is permitted to be a letter followed by any number of letters or digits. If and denote the sets of letters and digits respectively, which of the following expressions defines an identifier?
View worked solution
Answer: C
According to the problem definition, an identifier must satisfy the following rules:
- It must start with a letter ().
- The starting letter must be followed by any number of letters or digits.
Let's translate these rules into a regular expression:
- The set of letters is denoted by .
- The set of digits is denoted by .
- "Letters or digits" represents the union of the two sets, which is written as .
- "Any number of" means zero or more occurrences, which is represented by the Kleene star (). Thus, "any number of letters or digits" is .
Combining these two parts, we get the regular expression for an identifier:
Let's evaluate the given options:
- Option A: – This allows an identifier to start with a digit, which is invalid.
- Option B: – This only allows alternating sequences of letters and digits, and also allows the empty string, which is invalid.
- Option C: – This correctly matches a letter followed by any sequence of letters and/or digits.
- Option D: – This restricts the characters following the first letter to only alternating letter-digit pairs.
Therefore, the correct option is C.
Free practice · Your answers on this page are not saved.
