aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Simple Translation Using Java" Watch "Simple Translation Using Java" New topic
Author

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.
A=X
Assigns the value of X to variable A. Remember, A can be any variable and X can be any variable OR single-digit number.

A=X+Y
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.
(X)

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:

x=5
(3)
(x)
y=x+9
(z)
(y)
a=9+2
(a)

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:

x=5
(3)
(x)
y=x+9
(z)
(y)
a=9+2
(a)


Any help would be great.
Thanks,
Hunter


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

Joined: Oct 02, 2003
Posts: 11475
    
  16

I'm guessing the problem comes on this line:

System.out.println(z);

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.

-Hunter
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

What happens when you do this?

x=1
y=2
x=x+y

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

Henry


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.



-Hunter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Translation Using Java