Home › GATE CSE › computerscience › Compiler 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)._
Related questions
The FOLLOW(A) set in LL parsing isThe FIRST(α) set of a string α in LL parsing isAfter the parser produces an AST, the next compiler phase is typicallyA SHIFT/REDUCE CONFLICT in an LR parser occurs whenWhich of these is a BOTTOM-UP (LR) parser feature?An LL(1) grammar requires that, given the leftmost non-terminal and ONE token of lookaheadLL parsers (recursive descent) parse from thePANIC MODE error recovery in a parser works by