wood burning stoves 2.0*
The moose likes Java in General and the fly likes Simple Translation Using Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Simple Translation Using Java" Watch "Simple Translation Using Java" New topic

Simple Translation Using Java

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Hey everyone,

I have an assignment for my computer organization class and I'm a little stuck. My professor wants us to create a java program that translates a made-up language: Tiny into a java program that runs correctly.

here is the spec for The Tiny Programming Language:

There is only one data type: integer.
A legal identifier may have one and only one lowercase letter in it (thus there are only 26 possible different identifiers).
A literal integer may have one and only one digit in it. Thus, the only literals are 0 to 9; no larger integers and no negative integers.
There are no declaration statements. All possible identifiers are implicitly delared to be integers and are initialized to 0.
All addition is done modulo 1000.
Statements must begin in column one and may contain no blank or tab characters.
There are three (and only three) kinds of statements in Tiny (in this description, the symbol A represents any legal identifier and the symbols X and Y represent any legal identifier or literal):

There are three statements types in Tiny.
Assigns the value of X to variable A. Remember, A can be any variable and X can be any variable OR single-digit number.

Assigns the sum of X and Y to variable A. Remember, A can be any identifier. X and Y can be any identifier OR single-digit number. Thus, although the largest constant is 9, variables can take on values larger than 9.

Outputs the value of X followed by a newline character. Remember, X can be any variable OR single-digit number.

MY program works almost completely, the problem i have run into is that for my input data:


the variable z is never declared, in Tiny there are no declarations everything is declared implicitly and is initialized to 0. So instead of printing 0 my translated program gives me an error because I havent declared z.

Here is my code:

And here is the translated code i get with these inputs:


Any help would be great.

"If the facts don't fit the theory, get new facts" --Albert Einstein
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11164

I'm guessing the problem comes on this line:


in the Tiny2 class.

That's because java requires you to declare any variable you use. in your Tiny2 class, you never declared what 'z' was. Simply putting

int z = 0;

somewhere before your line 10 will solve that problem.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Yes that is what I need to do to declare z, but I don't really want to hard code int z = 0; into the program its supposed to be able to translate the program no matter the inputs.

If i hard coded int z = 0; in then switched z to f then I would have to hard code for f as well, I'm looking for a more versatile solution.

Henry Wong

Joined: Sep 28, 2004
Posts: 18544

What happens when you do this?


Don't you have the another problem, in that the variable is now declared twice?


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Yes you are correct Henry that does cause another problem: I believe I have fixed both my problem and the one you brought up, its a bit clumsy but it works. Let me know what you think.

It is sorta covered in the JavaRanch Style Guide.
subject: Simple Translation Using Java
Similar Threads
Writing line returns to file
Which class is suitable ?
Help with reprinting downloaded text from FTP site onto GUI!
'else' without 'if'
Printing from threads in a particular sequence