0
0
Compiler Designknowledge~10 mins

Lex/Flex tool overview in Compiler Design - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the section where definitions are placed in a Lex/Flex file.

Compiler Design
%[1]%
Drag options to blanks, or click blank then click option'
Arules
Bdefinitions
Cuser code
Doptions
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing the definitions section with the rules section.
2fill in blank
medium

Complete the code to write a rule that matches the word 'if' in Lex/Flex.

Compiler Design
"[1]"    { return IF; }
Drag options to blanks, or click blank then click option'
Awhile
Belse
Cif
Dfor
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes.
Writing the keyword without quotes.
3fill in blank
hard

Fix the error in the Lex/Flex rule to correctly match an identifier (a letter followed by letters or digits).

Compiler Design
[1]    { return IDENTIFIER; }
Drag options to blanks, or click blank then click option'
A[a-zA-Z][a-zA-Z0-9]*
B[0-9]+
C"[a-zA-Z]+"
D[^a-zA-Z0-9]
Attempts:
3 left
💡 Hint
Common Mistakes
Using only digits in the pattern.
Including quotes around the pattern incorrectly.
4fill in blank
hard

Fill both blanks to create a rule that matches whitespace characters and ignores them.

Compiler Design
[1]    { [2]; }
Drag options to blanks, or click blank then click option'
A[ \t\n]+
Breturn
C/* ignore */
D/* skip */
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a token instead of skipping.
Not matching all whitespace characters.
5fill in blank
hard

Fill all three blanks to define a Lex/Flex rule that matches a number and converts it to an integer.

Compiler Design
"[1]"    { yylval = atoi(yytext); return [2]; [3] }
Drag options to blanks, or click blank then click option'
A[0-9]+
BNUMBER
C/* convert to int */
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect pattern for numbers.
Forgetting to return the token.
Not converting the text to integer.