File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Data storage for interpreter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Data storage for interpreter" Watch "Data storage for interpreter" New topic
Author

Data storage for interpreter

Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

I've been thinking on the issue since I started building this interpreter, and right now I'm working on having it support String and integer variables. I'm wondering how to store the variables and values once they're declared. Can I simply return the values from the classes and update the original ArrayList the data being parsed is in? It's eithr that or create another ArrayList just for storing data, but somehow I don't think that's necessary as the language is strictly linear and is interpreted. Thoughts?


"How do you define fool?"
"I don't attempt it. I wait for demonstrations. They inevitably surpass my imagination."
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

So, given a line like

you're asking how to store the fact that variable x has value 5?

As a first pass, the simplest approach is probably a Map, where the key is the variable name and the value is the variable value.

Though I admit I have no idea what you mean by "return the values from the classes and update the original ArrayList the data being parsed is in? It's eithr that or create another ArrayList just for storing data, but somehow I don't think that's necessary as the language is strictly linear"
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Well the data I need to stare would be things like yup, LET X=5 and String myString, int myInt, and so on. I already have the ArrayList where all the data being parsed is stored in; can I get away with updating that with the values or should I implement another Map?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Noam Ingalls wrote:Well the data I need to stare would be things like yup, LET X=5


Meaning you need to store "LET X=5"? Or meaning you need to store the semantics or that statement--namely that variable X has received the value 5?

and String myString, int myInt, and so on


Meaning you need to store the fact that variable "myString" is of type String?

Okay, so, maybe you want a class called Variable that holds name, type, and value, and a Map from name to Variable. Again, though, I'm just grabbing the obvious, low-hanging fruit without knowing a lot about your requriements, but assuming the language will be pretty simple.

. I already have the ArrayList where all the data being parsed is stored in


This doesn't really mean anything, since it's not clear what "data" you're talking about, what that data means, or how you're storing it in the List.

It seems like you're taking the approach, "I want to read the source file, so what code should I write? Okay, now I want to store the value when the command is LET, so what code should I write for that? Okay, now I want to execute X = Y + Z, so what code should I write for that?" This is a sure path to pain. I think you should take a step back and look at the bigger picture, think about what you'll be representing, how you'll be accessing, using, and manipulating it, how the pieces will interact, and how to model that in an abstract sense, separate from any specific Java code.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
If I've understood your design correctly from your other posts, you are going to have classes that implement your Directive interface for each command. Any data related to each command should be stored in the respective class.
For instance for your LET command you will need something like

So when your command is LET X=5 the execute method will need to parse the command line and set variableName to "X" and variableValue to 5.

Obviously this will only work with integer variables. I'll leave it up to you how to handle variables of other types.

Joanne
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

I need to store the semantics of the statement, that variable x of type int has been declared and that its value is 5. In this language, all variables must be declared before hand with their type, before using a LET statement to assign a value to said variable. Strings are first declared as empty strings, and then later on a LET statement will assign the string some value, passed as arguments enclosed in double quotes. Same thing for integers. The LET statement is always in the form of LET x=y, so the second token in that is always an equals operator. CALCULATE statements allow for numeric calculations on values or variables, and assigns the result to a variable which MUST have been declared earlier, using the standard mathematical operators, no modulus. Working off an example my lecturer provided, I made a class for variables called TPLVariable which has the variable's name, which is then subclassed into TPLInteger (with name and value)and TPLString (with name and String value). Not too sure how to go about implementing the rest of what I need though.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
First thing I'd do is create a TPLVariables class which would hold all your variables


So in your main program you create an instance of this class and then for each declaration you need to call the addVariable method.

Start with that and let us know how you get on.

I'd also make your TPLVariable class abstract.


Edited to correct Map declaration
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

For this line:

I'm not sure what the question mark is meant to represent; from the context I assume it's supposed to be a class right? But I was thinking that strings and integers should be in separate subclasses of TPLVariable--so how would that work since you can't have multiple classes being declared as the value? Or do I have to combine both Strings and int classes somehow? Sorry if this question is dumb, but I've not had to do anything like this before and lots of it is still pretty much Ancient Egyptian hieroglyphs to me.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
It means any class that extends TPLVariable. So you will be able to add TPLInt and TPLString instances (plus instances of any other classes you create that extend TPLVariable) to the map

Actually ignore that - I really shouldn't do things from memory on a Friday afternoon.

will work fine.
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Oh I see, thanks! Didn't know that was possible... You guys are a LOT more helpful than my classes-- definitely learned more here than I have from sitting in lectures for 2 hours a week.
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Joanne Neal wrote:First thing I'd do is create a TPLVariables class which would hold all your variables


So in your main program you create an instance of this class and then for each declaration you need to call the addVariable method.

Start with that and let us know how you get on.

I'd also make your TPLVariable class abstract.


Right, back to coding this thing after taking a much needed pause to study for the other exam of doom...

Question referring to the addVariable class: I need to write a method for that to add the TPLVariable to the map right? So would something like variables.put("myStringName", new TPLVariable()) work?Though since that calls for manual additions to the map I'm not sure how the map would auto-update itself: do I need a loop or something, or am I right in thinking that after I write a (working) method for addVariable(), every time I put in a call to the method inside the class doing the processing it should auto-populate the data storage hashmap on its own without me having to do anything more to it, as was the idea?

Anyway, tried this tentatively for a class to instantiate a TPLString variable: got a "constructor cannot be applied to given types since it seems to require parameters (String, String) and it only got one String... I'm not sure what other String parameter I was supposed to pass to it though. Would someone kindly give me a hint? Thanks!



Forgive me if I'm beginning to sound silly or the above question is stupid, it's been a long 3 days of bombed papers and frantic cramming, and honestly I've kinda lost track of where I was in this project and am trying to get back into the programming zone... I think I was sleepwriting this morning in the exam hall because I have no idea what I wrote on the paper anymore LOL.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
If you write a constructor taking one String parameter, you must pass it one String parameter. If you want to pass two String parameters, you would need a two-parameter constructor.

You should provide constructors in the class depending on the requirements of that class, not on wishes of classes using it. If you need one String, that is what you need. If you need two Strings, you need two Strings. Provide as few non-private constructors as you can.
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Hmm, technically it WOULD need 2 Strings, one for the variable name, another for the value of the string itself-- or wait. Huh. I think I can get away with one string parameter, because I'd need to split it again anyway. Is it possible if using a HashMap to first input the key and then later another function would provide the value? Because I think that's what I need or this to work. >.<
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
If you need two strings you need two Strings. You would then have to get ridof the one-argument constructor. A constructor does not only allow you to create an instance; it also allows you to restrict them means of creating instances so you only create the sort of instance you want. So you want as few constructors as possible, but every class ought to have at least one constructor written.
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Question about String.split()-- If I have a string that's just one token long, like say "myString", will String.split work on it if I set it to making two substrings e.g. String[] array=listString.trim().split("[\\s]+",2)? If so, then array[0] presumably would hold "myString", but what would be at array[1]? Null? Can I make it so that array[1] if empty will be an empty string? And then I need to send that to a HashMap, with the information in array[0] being the key-- but the value would not be there until later in the program since any variable must first be instantiated before it can be assigned a value. What do I do?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You are going to have to try the split() method. I am pretty sure that . . . will return something identical to. . . ie a one-element array. That won’t work because you need two Strings.
Yes, you can put the first String as a “K” in a Map<String, String> and pass a reference to the Map, too.
Yes, you can validate the String with a regular expression, or by splitting it and verifying you get a two-element array. There are all sorts of things you can do . . .


. . . to make life difficult for yourself.

Or you can pass two Strings. But even that can be improved on, if there is any way you can get two classes which better match your requirements.
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Ok, trying this, and I'm getting a slew of "cannot find symbol" problems: Why is this so? I'm thinking I'm supposed to be putting in an object (the String or integer necessary) but it doesn't seem to want to work-- says it can't find the class TPLString which IS defined. Help?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data storage for interpreter