Challenge - 5 Problems
Lex/Flex Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
What is the primary purpose of Lex/Flex?
Lex/Flex is a tool used in compiler design. What is its main function?
Attempts:
2 left
💡 Hint
Think about the first step in processing source code in a compiler.
✗ Incorrect
Lex/Flex generates lexical analyzers, also called scanners, which read input text and split it into meaningful pieces called tokens. This is the first step in compiling source code.
📋 Factual
intermediate2:00remaining
Which language is used to write Lex/Flex specifications?
Lex/Flex specifications combine patterns and actions. What language are the actions written in?
Attempts:
2 left
💡 Hint
Lex/Flex was originally designed to work with a specific programming language.
✗ Incorrect
Lex and Flex generate C code for lexical analyzers, so the actions in the specification are written in C or C++.
🔍 Analysis
advanced2:00remaining
What happens if a Lex/Flex pattern matches multiple rules?
If two or more patterns in a Lex/Flex specification match the same input text, how does Lex/Flex decide which action to execute?
Attempts:
2 left
💡 Hint
Lex/Flex always tries to match as much input as possible.
✗ Incorrect
Lex/Flex uses the longest match rule: it selects the pattern that matches the longest portion of the input text. If there is a tie, it chooses the rule that appears first.
❓ Comparison
advanced2:00remaining
How does Flex differ from the original Lex tool?
Flex is a modern alternative to Lex. Which of the following is a key difference?
Attempts:
2 left
💡 Hint
Consider licensing and performance improvements.
✗ Incorrect
Flex is an open-source tool that is generally faster and more flexible than the original proprietary Lex tool.
❓ Reasoning
expert3:00remaining
What is the output of this Lex/Flex specification snippet?
Given the following Lex/Flex rules, what will be the output when the input text is "abc123"?
%{
#include
%}
%%
[a-z]+ { printf("Word\n"); }
[0-9]+ { printf("Number\n"); }
. { /* ignore other characters */ }
%%
int main() {
yylex();
return 0;
}
Compiler Design
%{
#include <stdio.h>
%}
%%
[a-z]+ { printf("Word\n"); }
[0-9]+ { printf("Number\n"); }
. { /* ignore other characters */ }
%%
int main() {
yylex();
return 0;
}Attempts:
2 left
💡 Hint
Lex/Flex matches the longest possible token at each step.
✗ Incorrect
The input "abc123" is scanned left to right. The pattern [a-z]+ matches "abc" first, printing "Word". Then [0-9]+ matches "123", printing "Number".