Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Translation Using Java

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21195
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic