A token is a basic unit of meaning in source code, identified by the compiler during lexical analysis. It represents a category like a keyword, identifier, operator, or symbol.
Click to reveal answer
beginner
Define lexeme with an example.
A lexeme is the actual sequence of characters in the source code that matches a pattern for a token. For example, in the code int x = 5;, int is a lexeme representing the token 'keyword'.
Click to reveal answer
intermediate
What is a pattern in the context of tokens?
A pattern is a rule or description (often a regular expression) that defines the set of lexemes that form a particular token. For example, the pattern for an identifier might be letters followed by letters or digits.
Click to reveal answer
intermediate
How do tokens, patterns, and lexemes relate to each other?
Patterns describe the rules for tokens. Lexemes are the actual text in the source code that match these patterns. Tokens are the categories assigned to these lexemes during lexical analysis.
Click to reveal answer
beginner
Why is lexical analysis important in compiling source code?
Lexical analysis breaks source code into tokens by matching lexemes to patterns. This simplifies parsing and helps detect errors early, making the compilation process efficient.
Click to reveal answer
Which of the following best describes a lexeme?
AThe category or type of a token
BThe actual sequence of characters in source code matching a token
CA rule describing a token
DA compiler optimization technique
✗ Incorrect
A lexeme is the exact sequence of characters in the source code that matches a token's pattern.
What does a pattern define in lexical analysis?
AThe rule that describes which lexemes form a token
BThe syntax of the programming language
CThe meaning of a token
DThe output of the compiler
✗ Incorrect
A pattern defines the rule or regular expression that lexemes must match to be recognized as a token.
Which of these is NOT a token category?
AKeyword
BIdentifier
CSyntax tree
DOperator
✗ Incorrect
A syntax tree is a data structure used later in compilation, not a token category.
During lexical analysis, what is the main task?
AParsing source code into statements
BOptimizing the compiled program
CConverting tokens into machine code
DBreaking source code into tokens by matching lexemes to patterns
✗ Incorrect
Lexical analysis breaks source code into tokens by matching lexemes to their patterns.
If the source code contains the word while, what is the token and lexeme respectively?
AToken: keyword, Lexeme: while
BToken: identifier, Lexeme: while
CToken: operator, Lexeme: while
DToken: keyword, Lexeme: loop
✗ Incorrect
The word 'while' is a lexeme that matches the pattern for the token category 'keyword'.
Explain in your own words the difference between a token, a pattern, and a lexeme.
Think about how a dictionary word (lexeme) fits a definition (pattern) and belongs to a part of speech (token).
You got /3 concepts.
Why does a compiler need to identify tokens using patterns and lexemes before parsing?
Consider how breaking a sentence into words helps understand its meaning.
You got /4 concepts.
Practice
(1/5)
1. What is a token in compiler design?
easy
A. The exact sequence of characters in the source code
B. A category or label for a group of characters
C. A rule describing valid sequences of characters
D. A syntax error found during compilation
Solution
Step 1: Understand the role of tokens
Tokens act like words in a sentence, grouping characters into meaningful categories.
Step 2: Differentiate tokens from lexemes and patterns
Lexemes are the actual text, patterns are rules, tokens are the labels for those texts.
Final Answer:
A category or label for a group of characters -> Option B
Quick Check:
Token = label/category [OK]
Hint: Tokens are like word types, not the words themselves [OK]
Common Mistakes:
Confusing tokens with lexemes
Thinking tokens are the exact text
Mixing tokens with syntax errors
2. Which of the following correctly describes a pattern in lexical analysis?
easy
A. The exact characters matched in the source code
B. A syntax error detected during compilation
C. The output of the parser after syntax analysis
D. A rule or regular expression defining valid tokens
Solution
Step 1: Define what a pattern is
Patterns describe the rules or formats that valid tokens must follow, often using regular expressions.
Step 2: Eliminate incorrect options
Exact characters are lexemes, parser output is unrelated, syntax errors are not patterns.
Final Answer:
A rule or regular expression defining valid tokens -> Option D
Quick Check:
Pattern = rule for tokens [OK]
Hint: Patterns are rules, not actual text [OK]
Common Mistakes:
Confusing patterns with lexemes
Thinking patterns are output of parsing
Mixing patterns with errors
3. Given the source code snippet: int count = 10; Which of the following is a correct lexeme for the token identifier?
medium
A. count
B. =
C. 10
D. int
Solution
Step 1: Identify the token 'identifier'
An identifier token represents names like variables or functions.
Step 2: Match lexemes to token types
In the snippet, 'count' is the variable name, so it is the lexeme for the identifier token.
Final Answer:
count -> Option A
Quick Check:
Identifier lexeme = count [OK]
Hint: Identifiers are names, not keywords or symbols [OK]
Common Mistakes:
Choosing keywords like 'int' as identifiers
Selecting numbers or operators as identifiers
Confusing lexemes with token types
4. Identify the error in this tokenization process for the input var_1 = 25;: Tokens identified are var (identifier), _1 (identifier), = (assignment), 25 (number), ; (semicolon).
medium
A. The underscore and digit should be part of the same identifier token
B. The assignment operator should be tokenized as two separate tokens
C. The number 25 should be split into two tokens '2' and '5'
D. The semicolon should not be a token
Solution
Step 1: Understand identifier token rules
Identifiers can include letters, digits, and underscores as one token, not split.
Step 2: Analyze the given tokenization
Splitting 'var_1' into 'var' and '_1' is incorrect; it should be one identifier token.
Final Answer:
The underscore and digit should be part of the same identifier token -> Option A
Quick Check:
Identifier tokens include underscores and digits [OK]
Hint: Keep underscores and digits inside one identifier token [OK]
Common Mistakes:
Splitting identifiers incorrectly
Misinterpreting assignment operator tokenization
Ignoring semicolon as a token
5. You want to design a lexical analyzer that recognizes tokens for integers and identifiers. Which pattern correctly matches an identifier but not an integer?
hard
A. [a-zA-Z0-9]+
B. [0-9]+
C. [a-zA-Z_][a-zA-Z0-9_]*
D. [0-9]*[a-zA-Z]+
Solution
Step 1: Understand identifier and integer patterns
Identifiers start with a letter or underscore, followed by letters, digits, or underscores. Integers are digits only.
Step 2: Eliminate incorrect options
A matches integers like '123'; B matches only integers; D fails to match identifiers ending in digits like 'var1'.
Final Answer:
[a-zA-Z_][a-zA-Z0-9_]* -> Option C
Quick Check:
Identifier pattern starts with letter/underscore [OK]
Hint: Identifiers start with letter/underscore, not digit [OK]
Common Mistakes:
Allowing digits as first character in identifiers
Confusing integer patterns with identifiers
Using patterns that match both integers and identifiers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.