0
0
Compiler Designknowledge~20 mins

Regular expressions for token patterns in Compiler Design - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Regex Token Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the role of regular expressions in token patterns

Which of the following best describes the primary purpose of using regular expressions in defining token patterns during lexical analysis?

ATo specify the exact sequence of characters that form valid tokens in the source code
BTo optimize the runtime performance of the compiled program
CTo execute the source code directly without compilation
DTo translate high-level code into machine code
Attempts:
2 left
💡 Hint

Think about how lexical analyzers identify meaningful units in code.

📋 Factual
intermediate
2:00remaining
Identifying token pattern for integer literals

Which regular expression correctly matches a decimal integer literal consisting of one or more digits?

A"[0-9]?"
B"[a-zA-Z]+"
C"[0-9]+"
D"[0-9]*"
Attempts:
2 left
💡 Hint

Consider that the integer must have at least one digit.

🔍 Analysis
advanced
2:00remaining
Determining the output of a regex-based token matcher

Given the regular expression "a(b|c)?d", which of the following strings will be matched by this pattern?

A"abcd"
B"ad"
C"abbd"
D"acbd"
Attempts:
2 left
💡 Hint

Analyze the pattern step-by-step: it starts with 'a', followed by zero or one 'b' or 'c', and ends with 'd'.

Comparison
advanced
2:00remaining
Comparing token patterns for identifiers

Which regular expression correctly matches identifiers that start with a letter and are followed by any combination of letters and digits?

A"[a-zA-Z][a-zA-Z0-9]*"
B"[0-9][a-zA-Z]*"
C"[a-zA-Z0-9]+"
D"[a-zA-Z]*[0-9]+"
Attempts:
2 left
💡 Hint

Remember that identifiers cannot start with digits.

Reasoning
expert
2:00remaining
Analyzing ambiguity in token patterns

Consider two token patterns defined by regular expressions: ID = "[a-zA-Z][a-zA-Z0-9]*" and KEYWORD = "if|int|for". When scanning the input string "int", which token should the lexer produce and why?

ABoth tokens simultaneously, because "int" matches both patterns
BID token, because "int" matches the identifier pattern
CNo token, because the input is ambiguous
DKEYWORD token, because keywords have higher priority than identifiers
Attempts:
2 left
💡 Hint

Think about how lexers resolve conflicts when multiple patterns match the same input.