📖 Scenario: You are learning about how computers read and understand text. Lex and Flex are tools that help break text into pieces called tokens. This is useful when making programs that read languages or commands.
🎯 Goal: Build a simple Lex/Flex file that recognizes a few words and numbers. This will show how Lex/Flex works to identify parts of text.
📋 What You'll Learn
Create a Lex/Flex file with basic token definitions
Add rules to recognize keywords and numbers
Include a section to print recognized tokens
Complete the Lex/Flex file structure correctly
💡 Why This Matters
🌍 Real World
Lex/Flex tools are used to create programs that read and understand text, such as compilers and interpreters.
💼 Career
Understanding Lex/Flex helps in jobs related to programming language development, software tools, and text processing.
Progress0 / 4 steps
1
Create the Lex/Flex file header
Start by writing the first section of the Lex/Flex file. Create a section enclosed in %{ and %} where you include the stdio.h header. This section sets up the environment for your Lex/Flex program.
Compiler Design
Hint
Use %{ and %} to start and end the header section. Inside, write #include <stdio.h>.
2
Add definitions for tokens
Add a definitions section after the header. Define two tokens: NUMBER as a pattern matching one or more digits ([0-9]+), and KEYWORD as the word if. Write these definitions using %option or simple pattern names.
Compiler Design
Hint
Write NUMBER [0-9]+ and KEYWORD if after the header section.
3
Write rules to recognize tokens
Create the rules section by writing patterns and actions. For the if keyword, write a rule that matches if and prints Keyword: if. For numbers, write a rule that matches digits and prints Number: followed by the matched text. Use printf and yytext to print matched text.
Compiler Design
Hint
Use %% to start and end the rules section. Write rules for if and {NUMBER} patterns with printf.
4
Add the main function to run the lexer
Add the final section with a main function. Inside main, call yylex() to start the lexer. This completes the Lex/Flex file so it can be compiled and run.
Compiler Design
Hint
Write int main() with yylex(); inside to run the lexer.
Practice
(1/5)
1. What is the primary purpose of the Lex/Flex tool in compiler design?
easy
A. To optimize the compiled program
B. To generate machine code from source code
C. To break input text into meaningful tokens
D. To manage memory allocation during execution
Solution
Step 1: Understand Lex/Flex role
Lex/Flex is a tool used to scan input text and split it into tokens based on patterns.
Step 2: Differentiate from other compiler parts
Generating machine code and optimization are done by other compiler stages, not Lex/Flex.
Final Answer:
To break input text into meaningful tokens -> Option C
Quick Check:
Lex/Flex = Tokenizer [OK]
Hint: Lex/Flex splits text into tokens, not code generation [OK]
Common Mistakes:
Confusing Lex/Flex with code generator
Thinking Lex/Flex optimizes code
Assuming Lex/Flex manages memory
2. Which of the following is the correct way to define a pattern in a Lex/Flex file?
easy
A. action { pattern }
B. action : pattern
C. pattern -> action
D. pattern { action }
Solution
Step 1: Recall Lex/Flex syntax
In Lex/Flex, patterns are written first, followed by actions enclosed in braces.
Step 2: Verify option formats
Only pattern { action } matches the correct syntax: pattern { action }
Final Answer:
pattern { action } -> Option D
Quick Check:
Pattern before action with braces [OK]
Hint: Pattern comes first, then action in braces [OK]
Common Mistakes:
Reversing pattern and action order
Using colons or arrows incorrectly
Placing action outside braces
3. Given the Lex/Flex rule: [0-9]+ { printf("Number: %s\n", yytext); } What will be printed when the input is 123abc?
medium
A. Number: 123abc
B. Number: 123
C. Number: abc
D. No output
Solution
Step 1: Understand the pattern
The pattern [0-9]+ matches one or more digits only, so it matches '123' at the start.
Step 2: Analyze input processing
Lex/Flex matches '123' and executes the action printing 'Number: 123'. The 'abc' part does not match this pattern.
Final Answer:
Number: 123 -> Option B
Quick Check:
Digits matched only = Number: 123 [OK]
Hint: Pattern matches digits only, stops before letters [OK]
Common Mistakes:
Assuming whole input matches pattern
Including letters in digit pattern
Expecting no output for partial matches
4. Identify the error in this Lex/Flex rule: [a-z]+ printf("Word: %s\n", yytext);
medium
A. Missing braces around the action
B. Pattern syntax is incorrect
C. Missing semicolon after pattern
D. yytext is undefined
Solution
Step 1: Check rule syntax
Lex/Flex requires actions to be enclosed in braces { } after the pattern.
Step 2: Identify missing braces
The given rule lacks braces around the printf action, causing a syntax error.
Final Answer:
Missing braces around the action -> Option A
Quick Check:
Actions need braces { } [OK]
Hint: Always put actions inside braces { } [OK]
Common Mistakes:
Omitting braces for single actions
Confusing pattern syntax with action syntax
Assuming semicolon needed after pattern
5. How can you modify a Lex/Flex rule to ignore whitespace characters (spaces, tabs) while scanning input?
hard
A. [ \t\n]+ { /* do nothing to skip whitespace */ }
B. [ \t\n]+ { printf("Whitespace found\n"); }
C. [a-z]+ { /* skip whitespace here */ }
D. Whitespace cannot be ignored in Lex/Flex
Solution
Step 1: Understand whitespace handling
To ignore whitespace, define a pattern matching spaces, tabs, newlines with an empty action.
Step 2: Check options for correct ignoring
[ \t\n]+ { /* do nothing to skip whitespace */ } matches whitespace characters and has an empty action, effectively skipping them.
Final Answer:
[ \t\n]+ { /* do nothing to skip whitespace */ } -> Option A
Quick Check:
Empty action skips matched whitespace [OK]
Hint: Use empty action for whitespace pattern to skip [OK]
Common Mistakes:
Printing message instead of skipping
Using wrong pattern for whitespace
Believing whitespace cannot be ignored
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.