In lexical analysis, a pattern is generally described using __________.
2025
In lexical analysis, a pattern is generally described using __________.
- A.
syntax tree
- B.
semantic rules
- C.
context-free grammar
- D.
regular expression
Attempted by 58 students.
Show answer & explanation
Correct answer: D
In lexical analysis, a pattern is generally described using Regular Expressions (Regex).
How it Works
A lexical analyzer reads the source code character-by-character and groups them into meaningful units called lexemes (like keywords, identifiers, operators, or literals).
A Lexeme is the actual text string (e.g.,
125,totalCount,if).A Token is the abstract category assigned to that string (e.g.,
NUMBER,IDENTIFIER,KEYWORD).A Pattern is the descriptive rule that dictates what characters can form a given token.
These patterns are defined mathematically and practically using Regular Expressions. For example:
An identifier pattern might be written as:
[a-zA-Z_][a-zA-Z0-9_]*(starts with a letter or underscore, followed by any number of alphanumeric characters).A digit/number pattern might be written as:
[0-9]+.
During the generation of a compiler, these regular expressions are converted behind the scenes into Finite Automata (specifically Deterministic Finite Automata, or DFAs) to physically execute the matching process on the input text.