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:

  1. The variable name can only contain letters.
  2. The variable name can only contain the lowercase alphabet.
  3. The variable can only be assigned to a number value.
  4. There should be space between each of the following parts of the variable declaration statement:
    1. var keyword
    2. Variable name
    3. Assignment operator
    4. 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 1400+ tech skills courses.