Another Language
Another Language
Constants/Literals:
int -- as for int in C Example: 4, 1055, -23, 0
arr -- [int1, int2, …, intn] Example: [1, 2, 3], [-5, 0, 4, -1], [2], []
Variable definition:
int i; -- An int variable (just like in C/C++)
arr a; -- An arr variable (initially it is an empty array, having 0 elements)
Variable Naming:
A variable name must begin with a letter or ‘_’ optionally followed by alphanumeric (letters
or digits), up to 32 characters.
@ - Dot-product
+-*/ - Function/Associativity/Precedence as in C
: - Indexing
= - Assignment (as in C)
Expressions:
int – Same as in C/C++ for int (but without ++ / -- and without shortcuts += *= etc.)
arr expressions:
<arr variable> = <arr constant> Assign r-value values to l-value variable, erases previous
value
<arr variable> = <arr variable> Assign r-value arr to l-value variable, erases previous
values
<arr variable> = <int> Creates an array with a single value (the r-value), erases
previous value
<arr varliable> = <expression> Assign expression result to l-value variable, erases
previous value
<arr>L +-*/ <arr>R Combines 2 arr (variable or constant) to a result arr, by
carrying out the operation on every arr index:
<arr>Ei = <arr> Li +-*/ <arr> Ri
A missing index is regarded 0
<arr>L @ <arr>R Result is an <int> dot-product of the 2 arr elements:
<int>E = ∑𝑖 < 𝑎𝑟𝑟 >𝑙 𝑖 ∗< 𝑎𝑟𝑟 >𝑟 𝑖
<arr>:<expression> Resolves to the arr element at the index given by the <int
expression>.
Pseudo definition of the language grammar:
<program> ➔ <block>
<variable> ➔ <identifier>
| (<expression>)
| <variable>
| <number>
fib:0 = 0;
fib:1 = 1;
i=2;
print 0, 0; //print Fibonacci 0
print 1, 1; //print Fibonacci 1
while (i<16) do
begin
fib:i = fib:(i-2) + fib:(i-1);
print i, fib:I; //print Fibonacci numbers at 2..15
i=i+1;
end
print fib@[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ; //print Fibonacci sum of indexes 0..15
fAvg = fib@[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] /16;
print fAvg; //print Fibonacci average of first 16 values
i=2;
while (i<16) do
begin
if (fib:I > fAvg) then begin
print i, fib:I;
end
i=i+1;
end
end
Expected Output
0, 0
1, 1
2, 1
3, 2
4, 3
5, 5
6, 8
7, 13
8, 21
9, 34
10, 55
11, 89
12, 144
13, 233
14, 377
15, 610
1596
99
12, 144
13, 233
14, 377
15, 610
Implement:
A compiler from “Another” Language to ‘C’ or ‘C++’ language
Submit Materials:
1. LEX file
2. YACC file
3. Make to generate compiler (Specify for what environment: PC/MAC/Linux)
4. The compiler executable
5. An example source file in “Another” Language
6. The results C/C++ file after compiling the example file
7. The executable after compiling the C/C++ of the example file
8. A short video showing the make process of the compiler, compiling the example to
‘C’ source, compiling the ‘C’ source to executable and running the executable.