In this project, you will implement a translator for a language called MatLang that will translate MatLang code to C language code. The C language code generated can then be compiled by a C compiler to produce an executable file.
MatLang language statements will be as follows:
- Matlang programs will have two sections: The first will be a variable definitions section,
followed by the executable statements section.
- Variable definitions section will allow scalar, one-dimensional, two dimensional number
variable definitions. For example:
scalar x vector y[4] matrix z[3,4]
Assume all array dimensions are given as integer constants. In the above example x is a scalar variable, y is a vector (i.e. matrix of size 4×1) and z is a 3×4 matrix. Assume that each line contains a single definition. A definition cannot be written on more than one line.
- Executable statements will consist of one line statements and for loop compound statements. Note that no nested for loops are allowed.
- One line statements are either assignment statements or print statements which print the value(s) of a scalar, vector, or a matrix variable or a separator.
- A vector or a matrix varieble can be assigned values in curly brackets. Note that such assignment should fit on line. For the vector y[4] and z[3,4], the following example
assignment can be made:
y={1234}
z ={1234532221 01} Note that array indices start with 1. - There are no if statements in the language.
- As operations in expressions, you are required to implement only multiplication, addition
and subtraction: *,+,- . These are binary operand operations. Unary minus operation is not supported. Note that these are to be interpreted as either matrix or scalar operations depending on the context (i.e. type of operands). When a scalar expression multiplies a matrix or a vector, its meaning is multiplication of each individual component of a matrix or vector.
- A function tr(expr) is also available which transposes a scalar, vector or a matrix.
- A function sqrt(expr) is also available which takes square root of a scalar expression.
- A function choose(expr1,expr2,expr3,expr4) which returns expr2 if expr1 is equal
to 0, returns expr3 if expr1 is positive and returns expr4 if expr1 is negative.
Note that expr1, expr2, expr3 and expr4 are expressions that evaluate to a scalar.
- On a line, everything after the # sign are considered as comments.
- For loop will have the following formats:
for (id in expr1:expr2:expr3) { …..
….. }
Here,idisa variable,expr1isstartingvalueofid,expr2istheboundonthevalueofid during the loop iteration and expr3 is the added value to id at each iteration.
For loop can also have the following syntax:
for (id1,id2 in expr1:expr2:expr3, expr4:expr5: expr6) { …..
….. }
This will be equivalent to doubly nested loops in languages like C/Java. You can assume that the values of ids id1,id2 and expressions expr1,expr2,expr3,expr4,expr5
and expr6 cannot be changed inside the for loop body. You can also assume that expr1 < expr2 and expr4 < expr5 and that expr3 and expr6 evaluate to a
positive value.
- print(id) statement, prints the value of variable id.
- printsep() statement, prints a separator line “———-“
15. Please note that the C code generated must compute the MatLang outputs. You should not
generate C code that just prints MatLang program outputs.
Some example programs in the MatLang language are given below. Note that MatLang language programs have .mat extension.
|
ex1.mat |
Output when compiled and executed |
|
# this program computes fibonacci # numbers # variable definitions scalar i vector y[2] matrix A[2,2] matrix B[2,2] # statements for(i in 1:n:1) { B = A*B y = B*x print(y[1]) } |
1 1 2 3 5 8 13 21 34 55 89 144 |
|
ex2.mat |
Output when compiled |
|
# variable definitions vector z[3] matrix B[2,3] z = A*B*y |
Error (Line 6): matrix dimensions in expression do not match. |
|
ex3.mat |
Output when compiled and executed |
|
# simple pageranking # algorithm matrix A[3,3] matrix T[1,1] vector x[3] vector y[3] scalar r scalar i |
0.7071068 0.6123724 0.4677072 0.3852759 0.3093592 0.2509747 0.2028243 0.1641555 0.1327838 0.1074308 |
|
A={0.5 00.5000.50.510} x={111} y = A*x } printsep() print(x) |
———— 1.2148438 0.6240234 1.1611328 |
|
ex4.mat |
Output when compiled and executed |
|
matrix A[4,4] matrix T[1,1] vector x[4] vector xy2[4] scalar s A = {0 1 2 3 4 5 6 7 8 9 1 1 1 2 3 4 } x = {1 1 1 1 } print(s) T = tr(x)*A*xy2 s = T[1,1] |
94 |
|
ex5.mat |
Output when compiled and executed |
|
# count how many elements are # greater than or equal to 4 matrix A[4,4] scalar incr scalar i scalar j A = {0 1 2 3 4 5 6 7 8 9 1 1 1 2 3 4} count = 0 incr = choose(A[i,j]-4,1,1,0) count = count + incr } print(count) |
7 |
Your project will be graded according to the following criteria:
Documentation (written document describing 12% how you implemented your project)
Comments in your code 8% Implementation and tests 80%





