0
0
Compiler Designknowledge~20 mins

Lex/Flex tool overview in Compiler Design - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lex/Flex Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the primary purpose of Lex/Flex?
Lex/Flex is a tool used in compiler design. What is its main function?
ATo parse tokens into a syntax tree
BTo optimize machine code for faster execution
CTo generate lexical analyzers that break input text into tokens
DTo generate executable binary files from source code
Attempts:
2 left
💡 Hint
Think about the first step in processing source code in a compiler.
📋 Factual
intermediate
2:00remaining
Which language is used to write Lex/Flex specifications?
Lex/Flex specifications combine patterns and actions. What language are the actions written in?
AC or C++
BJava
CPython
DAssembly
Attempts:
2 left
💡 Hint
Lex/Flex was originally designed to work with a specific programming language.
🔍 Analysis
advanced
2: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?
AIt chooses the rule that appears first in the specification
BIt raises a syntax error and stops
CIt chooses the rule with the shortest pattern
DIt chooses the rule that matches the longest input text
Attempts:
2 left
💡 Hint
Lex/Flex always tries to match as much input as possible.
Comparison
advanced
2: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?
AFlex is open source and faster than the original Lex
BFlex only supports Java actions, Lex supports C
CFlex cannot generate scanners for multiple languages
DFlex requires manual memory management unlike Lex
Attempts:
2 left
💡 Hint
Consider licensing and performance improvements.
Reasoning
expert
3: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;
}
A
Number
Number
Word
B
Word
Number
C
Number
Word
D
Word
Word
Number
Attempts:
2 left
💡 Hint
Lex/Flex matches the longest possible token at each step.