Using the same class at 3 places concurrently - java

I'm currently working on a basic parser in Java and I have run into a problem when it comes to loops. Say I want to parse a while loop it would look something like:
a = 0
WHILE a < 10
a = a + 1
WEND
This example contains 4 expressions one of which will only need to be parsed ones the others will need to be parsed 10 times. The first one being the initial a = 0, second and third being on each side of the condition in the WHILE statement and the last one being within the WHILE block.
When I wrote the interpreted for this it loads the expression class into 4 different variables as new, the reevaluates them for each time it runs.
My problem is that even though they are initially loaded as new classes each time, there is over flow between them so when I re-parse them all of them looks like the last one:
a + 1
Any advice on how to circumvent this?
The code base is rather big so I wont post it here but its available at: http://git.life-hack.org/basic-parser/tree/src/parser
The file I'm working in is LoopBlock.java

Related

Parsing with Scanner findWithinHorizon issue

in a project I'm working right now, I need to parse escape sequences with the Scanner class (using Java in Linux). To include, for instance, the two END keys in the keyboard, I initially wrote the following code:
if(sc.findWithinHorizon("(\\G\\e\\[4~)?",0).length() > 0 || sc.findWithinHorizon("(\\G\\eOF)?",0).length() > 0 ) {
System.out.print("END"); //To see if it works
With that code, I don't get any output (the terminal just freezes). After seeing that, I separated the two condition in two different if's:
if(sc.findWithinHorizon("(\\G\\e\\[4~)?",0).length() > 0)
System.out.print("END");
else if(sc.findWithinHorizon("(\\G\\eOF)?",0).length() > 0 )
System.out.print("END");
Then it works. Does anybody know why it doesn't work with the OR operator?
Thanks in advance.
You'll want to capture what findwithinhorizon parses into a String then do your comparisons. What you're doing in the first if statement is parsing sc twice. Once doing a comparison for something then the second time doing a different comparison on a different part of the text.
I doubt this is the behavior you wanted. when you set horizon to 0 it's going to look at what is the current position then increment the position by one, so your second call is looking at a different index. If you truly have reached the end of the file I would believe this would cause some odd behavior and explain why it hangs.

How to prvent Xtend templates from printing Variables during loop

I am using the Xtend templates to write a small program. I try using the IF statement, but every time I execute it it prints the variable in the console, which I dont want it to.
«IF x==y»
The jump value is «database.get(2)»
«jump_var_letter = String.charAt(1)»
«old_jump_ahd=database.get(2) »
«ENDIF»
Here the database is an array of integers and String is an array of letters. Here I just want it to print the value found at database.get(2) i.e 5. The last two expressions befor the ENDIF is meant for assignning a few values( which need not be printed)
The jump value is 5
Instead I get
The jump value is 5
D
5
Could somebody please tell me how I could stop printing the other two values.
Thank you in advance for your help..
After looking for sometime on the net I found that you could prevent the printing of the expreesions in between by using block expressions and then returning a null expression. (Although this method is not encouraged, I found that it provides me the result I wanted). So the expression I posted could be written as:
«IF x==y»
The jump value is «database.get(2)»
«{jump_var_letter = String.charAt(1); "" }»
«{old_jump_ahd=database.get(2); ""} »
«ENDIF»
This prints
The jump value is 5.

What is the best way to implement the instruction fetching step in a MIPS program using java?

I am implementing a simple MIPS simulator using java. My problem is in the fetching instruction step where I should take one instruction and convert it to 32-bit binary code to be able to determine that the first 6 bits are the opcode and the next 5 are the rs(source register) and so on.
At first I thought I would just make an arraylist and add the 15 instructions my program is going to support and then make another arraylist and add the 32 registers available and then when the user enters his code I loop on the entered string comparing it with my instructions and registers names in the arrays, but then I realized I don't know which characters exactly I am going to compare from the users code I mean (add $s1 $s4 $s5) will be different from (addi $t8 $zero 1) so I can't just check the substring of the first 3 characters every time with the instructions array and then check the next 3 because as you see the zero register might take a larger place and so on.
My second approach is to define the instructions by their opcodes and the registers by their binary values then convert the given instruction by the user to binary and compare it. Is there any other possible or probably easier ways to do so?
If I understand you right, your question is how to get from the user's assembler code to the machine code that is interpreted by your MIPS. This is where tokenizers come in handy. Use a StringTokenizer (http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html) to convert user input into a string of byte code and feed the latter one to your MIPS exactly how you propose in your question.

JAva / Android - Adding to long having no effect

I usually use python/php but currently writing an Android app so playing with java at the moment.
I can't figure out why the following piece of my code doesn't work.
It's meant to take a long representing the time the last check occurred (last_check) and add a predefined number of minutes to it (CHECK_INTERVAL) which is currently set to 1 minute.
Log.i(this.toString(), "Last check: " + Long.toString(last_check));
long add_to_check = CHECK_INTERVAL * 60 * 1000;
long next = last_check + add_to_check;
Log.i(this.toString(), "Add to check: " + Long.toString(add_to_check));
Log.i(this.toString(), "Next check: " + Long.toString(next));
scheduleNextRun(next);
My expectation is that the first log will show the last_check time, the second log will show 60000 and the third log will show the sum of those two.
However, I am getting the same value for the first log and the third log - it's like my addition isn't working at all but I can't figure out why.
I thought I might have had issues with log vs int but have tried adding L to one of the variables and it doesn't make a difference.
FYI the second log is showing 60000 as expected.
thanks
Aaron
Is CHECK_INTERVAL 0? You wrote that "the second log is showing 60000 as expected" but perhaps CHECK_INTERVAL was 0 the first time this code ran, then initialized, then 1 on a later iteration when you're looking at that part of the log.
Are you initializing CHECK_INTERVAL to something non-zero but after this code runs? Do you have an initialization bug?
This problem will be easy to solve if you step through the code in the debugger and watch the results. If you're not using a debugger, do yourself a huge favor and get Android Studio (a wonderful tool built on IntelliJ IDEA) or Eclipse + ADT (a good tool).
Java initialization is defined in a way that's predictable, portable, and useful, but it can still be tricky. E.g. given
class Foo {
static final int A2 = A1 * 1000;
static final int A1 = 60;
}
The JVM first initializes all variables to default values, then runs the initialization expressions in order. Since A1 is 0 when A2's initializer runs, A2 will end up 0.
See Java Puzzlers for more subtle cases such as when one class's initialization code refers to a second class, causing the second class's initialization code to run, which then refers to values in the first class which haven't yet been initialized beyond their default values (0 and null). A class runs its initialization code on first demand, but nothing guarantees that it finishes initializing before those values are used.
Another tricky case happens when one class, C1, refers to a static final value from a second class, C2.A, then you edit the initialization code for A without recompiling class C1. Java has precise rules about when to cache such constants in the first class's .class file, but they aren't the ideal rules, and the compiler doesn't notice that it needs to recompile C1 for this!
BTW 1: If CHECK_INTERVAL is an int, the expression CHECK_INTERVAL * 60 * 1000 will compute an int value and wrap around within a 32-bit signed range. Still, 1 * 60 * 1000 will easily fit in an int.
BTW 2: The first arg to Log.i() is a tag. It's OK to pass in this.toString() [or toString() for short] but the idea is to pass in a constant tag like the current class name that you can use for log filtering.
[Added] Quick intro to Eclipse debugging
In the source code editor, double-click in the left margin to set a breakpoint. Then use the menus or toolbar to "debug as" a Java application rather than "run as". Eclipse will go into its "Debug perspective" (arrangement of views).
https://www.google.com/search?q=eclipse+debugger finds nice tutorials with step-by-step pictures (I checked the first 3; IBM's is the most concise and introduces more features) and videos. The Eclipse docs are good but harder to navigate.
It's all slicker in Android Studio.

how to get word-topic probability using mallet

I've made a parallel topic model using mallet.
And I want to get top-words for each document.
To do that, I'm trying to get a word-topic probability matrix.
How would I achieve this?
When you are building topics using MALLET, you have an option called --word-topic-counts-file. When you give this option and specify a file, MALLET writes ( topic, word, probability ) values per each line in the file. You can later read this file in C, Java or R (of course, any language) to create the matrix you want.
Just to make one point regarding the answer of Praveen.
Using the --word-topic-counts-file, MALLET will create a file which first few rows look something like this:
0 elizabeth 19:1
1 needham 19:2 17:1
2 died 19:2
3 mother 17:1 19:1 14:1
where first line means that the word elizabeth has been present in the topic 19 once; second line means that the word needham is associated two times with the topic 19, and with the topic 17 once; and so on...
Although, this file doesn't give you explicit probabilities, you can use it to calculate them.

Categories