Practice free →
HomeGATE CSEcomputerscienceCompiler Design › An IDENTIFIER in most C-family languages follows…

An IDENTIFIER in most C-family languages follows the regex

A$[0-9]+$, only digit sequences
B$[^\s]+$, no whitespace allowed
C$.+$, any non-empty string
D$[a-zA-Z][a-zA-Z0-9]^*$
Answer & Solution
Correct answer: D. $[a-zA-Z][a-zA-Z0-9]^*$
1. C-family identifier rules: - FIRST character: letter or underscore (NOT a digit) - SUBSEQUENT characters: letter, digit, or underscore 2. Regex: $[a-zA-Z_][a-zA-Z_0-9]^*$. 3. Example matches: `x`, `foo_bar`, `_private`, `var2`, `MyClass`. 4. Example NON-matches: `2var` (starts with digit), `foo-bar` (hyphen not allowed). 5. Why disallow leading digit? So the scanner can quickly decide if a token is a NUMBER (starts with digit) or IDENTIFIER (starts with letter). 6. Options A (digits only), C, D mismatch the language rule. _Source: Bob Nystrom, "Crafting Interpreters", Ch 4.7 (Identifiers — regex)._
Solve this in the app — GATE CSE practice & 24k+ MCQs →
Related questions