Computational Methods

Exercise: Lexical Analysis

Objective

During this activity, students will be able to:

NOTE: This is an in-class exercise. No submission is required.

Description

Write a program in Clojure that receives the name of a text file containing arithmetic expressions and comments as input. The program should print a table with each of the lexical units (tokens) found, in the order they were encountered, indicating their category. The program must be implemented using the Regular Expression API.

Token Categories

Arithmetic expressions may only contain the following categories of tokens:

Input

Example:

b=7

a = 32.4 *(-8.6 - b)/       6.1E-8

d = a ^ b // This is a comment

Output

The previous example should produce the following output:

========================================================
Token                           Category
========================================================
b                               variable
=                               assignment
7                               integer
a                               variable
=                               assignment
32.4                            float
*                               multiplication
(                               opening-parenthesis
-8.6                            float
-                               subtraction
b                               variable
)                               closing-parenthesis
/                               division
6.1E-8                          float
d                               variable
=                               assignment
a                               variable
^                               power
b                               variable
// This is a comment            comment
========================================================

Token Formation Rules