0
0
Compiler Designknowledge~20 mins

Tokens, patterns, and lexemes in Compiler Design - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Token Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the relationship between tokens, patterns, and lexemes

Which of the following best describes the relationship between tokens, patterns, and lexemes in lexical analysis?

AA lexeme is a category defined by a pattern, and a token is the actual string matching that pattern.
BA pattern is the actual string, a lexeme is the category, and a token is the rule to match them.
CA token is a category defined by a pattern, and a lexeme is the actual string matching that pattern.
DTokens, patterns, and lexemes all mean the same thing and are interchangeable terms.
Attempts:
2 left
💡 Hint

Think about what each term represents in the scanning process.

📋 Factual
intermediate
2:00remaining
Identifying lexemes from source code

Given the source code snippet: int count = 42;, which of the following is NOT a lexeme?

A"number"
B"count"
C"="
D"int"
Attempts:
2 left
💡 Hint

Lexemes are actual substrings from the source code.

🔍 Analysis
advanced
2:00remaining
Determining the token sequence from a code snippet

Consider the code snippet: sum = a + b * 10;. Which sequence of tokens correctly represents this snippet?

Aidentifier, assignment_operator, identifier, plus_operator, identifier, multiply_operator, number, semicolon
Bkeyword, assignment_operator, identifier, plus_operator, identifier, multiply_operator, number, semicolon
Cidentifier, equals_sign, identifier, plus_sign, identifier, multiply_sign, number, semicolon
Didentifier, assignment_operator, identifier, plus_operator, identifier, multiply_operator, number
Attempts:
2 left
💡 Hint

Focus on the categories of tokens, not the exact characters.

Comparison
advanced
2:00remaining
Comparing patterns for identifiers

Which of the following patterns correctly describes a typical identifier in many programming languages?

AContains only digits and special characters.
BStarts with a digit, followed by letters or underscores.
CStarts with a special character, followed by letters or digits.
DStarts with a letter or underscore, followed by any number of letters, digits, or underscores.
Attempts:
2 left
💡 Hint

Think about common rules for variable names.

Reasoning
expert
2:00remaining
Effect of ambiguous patterns on lexical analysis

Suppose a lexical analyzer has two patterns: one for keywords (e.g., "if", "while") and one for identifiers (any letter followed by letters or digits). If the input is "if", what will the lexical analyzer output and why?

AIt outputs both tokens "keyword" and "identifier" for the same lexeme.
BIt outputs the keyword token "if" because keyword patterns have higher priority.
CIt raises an error due to ambiguity between keyword and identifier patterns.
DIt outputs an identifier token "if" because identifiers match any letter sequence.
Attempts:
2 left
💡 Hint

Consider how lexical analyzers resolve conflicts between overlapping patterns.