CS 3721 Programming Languages
|
References:
With these changes, I was able to get all six of the previous integer programs to run and produce exactly the same output as before. (I had to replace the '/' operator with '#'.
In addition I tried out some new functions that that did interesting floating point calculations. The first calculates the "harmonic" series: 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + ... + 1/n.
Sample Input 7: Harmonic series | Output |
---|---|
n = 1; s = 0; { n - 9 *2 ? s = s + 1/n; < n; < T; < s; < N; n = n + 1; } $ % javac TinyCompiler.java % java TinyCompiler < w2.tiny > w2.s % spim -file w2.s | 1 1 2 1.5 3 1.83333333333333326 4 2.08333333333333304 5 2.28333333333333321 6 2.44999999999999973 7 2.59285714285714253 8 2.71785714285714253 9 2.82896825396825369 10 2.92896825396825378 11 3.0198773448773446 12 3.10321067821067809 13 3.18013375513375518 14 3.2515623265623268 15 3.31822899322899367 16 3.38072899322899367 17 3.43955252264075817 |
The next calculates pi using the advanced series:
n Sum (1/16^i)(4/(8*i+1) - 2/(8*i+4) - 1/(8*i+5) - 1/(8*i+6)) i=0
Sample Input 8: Calculate Pi | Output |
---|---|
i = 0; s = 0; > n; { i - n ? t = 4/(8*i+1) - 2/(8*i+4) - 1/(8*i+5) - 1/(8*i+6); k = 0; { i - k ? t = t/(4*4); k = k + 1; } s = s + t; i = i + 1; < t; < T; < s; < N; } < s; < N; $ | % javac TinyCompiler.java % java TinyCompiler < pi.tiny > pi.s % spim -file w2.s 12 3.1333333333333333 3.1333333333333333 0.00808913308913308989 3.14142246642246636 0.000164923924115100557 3.14158739034658163 5.0672208538587869e-06 3.14159245756743566 1.87892900937720114e-07 3.14159264546033645 7.7677512151773586e-09 3.14159265322808778 3.44793293050862315e-10 3.14159265357288087 1.6091877155536991e-11 3.14159265358897288 7.79570295400101777e-13 3.14159265358975226 3.88711525990974833e-14 3.14159265358979134 1.98322539359813e-15 3.14159265358979312 1.03097121697888662e-16 3.14159265358979312 3.14159265358979312 3.141592653589793238462643383279502884 ... (exact value for comparison) |
Just for reference, here is the translated MIPS code for this example: pi.s.
The Grammar: Consider the following expanded version of this language:
Grammar for Extended Tiny® Language |
---|
M ---> { ( S | D ) } '#' S ---> I | W | A | P | C | G D ---> '(' id '(' [ id { ',' id } ] ')' { S } ')' I ---> '[' E '?' { S } ':' { S } ']' | '[' E '?' { S } ']' W ---> '{' E '?' { S } '}' A ---> id '=' E ';' P ---> '<' E ';' G ---> '>' id ';' C ---> '<' ( 'B' | 'T' | 'N' ) ';' E ---> Q [ ('&' | '|') Q ] Q ---> R [ ('<' | '>' | '<=' | '>=' | '==' | '!=' ) R ] R ---> T { ('+' | '-') T } T ---> U { ('*' | '/' | '%') U } U ---> F '^' U | F F ---> ['+' | '-' | '!'] ('(' E ')' | id | num | id '(' [ E { ',' E } ] ')' ) id ---> letter { letter | digit } num ---> digit { digit } |
For a more colorful grammar that might be easier to understand, see here.
The terminal "letter" stands for a single lower-case letter, and "digit" stands for a single digit.
Just to help with understanding, here is the intuitive meaning of each of the above non-terminals:
Symbol | Meaning |
---|---|
M | Main Program |
S | Statement |
D | Function Definition |
I | If-Then-[Else] Statement |
W | While Statement |
A | Assignment Statement |
P | Put or Print (integer) |
C | Print Character |
G | Get (integer) |
E | Expression (logical or arith) |
Q | Relational Expression (without | or &) |
R | Arithmetic Expression (without relational ops) |
T | Term (without + or -) |
U | Ugly Term (without * or / or % either) |
F | Factor (parethsized, with unary + or -) |