Exercise: A Simple Interpreter
Challenge yourself to write a regular expression for this fun exercise!
We'll cover the following
Let’s make a simple interpreter!
Problem statement
Write the regular expression which accepts the code that will allow us to declare a variable. For simplicity, the variable can only store the number data type.
This regular expression should be able to match code that starts from var
, then the variable name, then the assignment operator, =
, followed by a value (number).
For example:
var myvariable = 2
Conditions
Here are some conditions that you should keep in mind for this exercise:
- The variable name can only contain letters.
- The variable name can only contain the lowercase alphabet.
- The variable can only be assigned to a number value.
- There should be space between each of the following parts of the variable declaration statement:
var
keyword- Variable name
- Assignment operator
- The value of number data type
All test cases should pass in order for the solution to be considered correct.
Good luck!
Get hands-on with 1200+ tech skills courses.