0
0
Compiler Designknowledge~10 mins

Implementing a lexical analyzer 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 define the function that starts the lexical analysis.

Compiler Design
def [1](source_code):
Drag options to blanks, or click blank then click option'
Alexical_analyzer
Btokenize
Cparse
Dcompile
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parse' which is for syntax analysis, not lexical analysis.
Using 'compile' which is for the whole compilation process.
2fill in blank
medium

Complete the code to read the next character from the source code string.

Compiler Design
current_char = source_code[[1]]
Drag options to blanks, or click blank then click option'
A0
Bindex
Clength
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Using '0' which always reads the first character, not the current one.
Using 'token' which is unrelated to string indexing.
3fill in blank
hard

Fix the error in the condition to check if the current character is a digit.

Compiler Design
if current_char [1] '0' and current_char [2] '9':
Drag options to blanks, or click blank then click option'
A>=
B<=
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which only checks equality to one character.
Using '!=' which checks inequality, not range.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps tokens to their types for all tokens longer than 2 characters.

Compiler Design
token_types = {token: [1] for token in tokens if len(token) [2] 2}
Drag options to blanks, or click blank then click option'
A'identifier'
B>
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' which limits tokens to length exactly 2.
Using '<' which selects tokens shorter than 2 characters.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase tokens to their lengths if length is at least 3.

Compiler Design
result = { [1]: [2] for [3] in tokens if len([3]) >= 3 }
Drag options to blanks, or click blank then click option'
Atoken.upper()
Blen(token)
Ctoken
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not converting the token to uppercase for the key.