Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,538 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,784 people online right now. Registration is fast and FREE... Join Now!




compiler as final year project.

2 Pages V  1 2 >  
Reply to this topicStart new topic

compiler as final year project., related to the topic i posted in Java forum.

mdew_47
15 Aug, 2008 - 06:42 AM
Post #1

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

my C side code for the JNI interface is:


CODE

# include <C:\j2sdk1.4.2_03\include\jni.h>
# include <C:\Program Files\InstallShield\InstallShield for Microsoft Visual C++ 6\Include\project.h>
# include <stdio.h>
# include <string.h>

JNIEXPORT jstring JNICALL Java_project_test
(JNIEnv *env , jobject obj, jstring s)
{
char (*str)[40] =(*env)->GetStringUTFChars(env,s,0);
int i;
for(i=0;(*str)[0]=='^'&&(*str)[1]=='c';i++)
do{
strupr(str);
}while((*str)[i]!='c'&&(*str)[i+1]!='^');
return((*env)->NewStringUTF(env,str));
(*env)->ReleaseStringUTFChars(env,s,str);
}


it is not working as i thouht might be i got a wrong logic:

i thing off converting to capital all the characters that are between

^c<char array>c^

if anyone can help thanks in advance.
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project.
15 Aug, 2008 - 06:57 AM
Post #2

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

pls it is urgent, if anyone can please help! rolleyes.gif

This post has been edited by mdew_47: 15 Aug, 2008 - 09:19 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Compiler As Final Year Project.
15 Aug, 2008 - 10:16 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 50 times
Dream Kudos: 550
My Contributions
well (*env)->ReleaseStringUTFChars(env,s,str);

is after the return statement and so it will never execute...

not really familiar with JNI. but I would think it would be something like this: (note code not compiled)

CODE
JNIEXPORT jstring JNICALL Java_project_test
(JNIEnv *env , jobject obj, jstring s)
{
    char str[256] =(*env)->GetStringUTFChars(env,s,0);
    int i = 2;
    if(str[0]=='^'&&str[1]=='c') {
        do{
            toupper(str[i++]);
        } while(i < 255 && (str[i]!='c' && str[i+1]!='^'));
    }
    (*env)->ReleaseStringUTFChars(env,s,str);
    return((*env)->NewStringUTF(env,str));
}


this is based upon code from here.

Note: Why are you doing this in JNI? -- this far easier to do in java proper!

This post has been edited by NickDMax: 16 Aug, 2008 - 06:49 AM
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Compiler As Final Year Project.
15 Aug, 2008 - 01:48 PM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
Is JNI necessary for this? I looked at your requirements in the Java section, but I don't see why you couldn't still implement it like a traditional compiler. By this I mean that the compiler would be a standalone executable, and the Java would simply be a front end to it.

Here are some examples (it covers utilizing the executing process's standard input and output streams also):
http://www.rgagnon.com/javadetails/java-0014.html

Also, with respect to the code, you cannot initialize C/C++ arrays at runtime with array initializes. What this means is that you can't do:

char somestr[100] = some_non_const_expr();

------

You can do:
char somestr[100] = "xxxx";

-- or --
char somestr[100] = { 'a', 'b', 'c', ... };


But for what you're doing, you need to do:

const char* str = (*env)->GetStringUTFChars(env,s,0);
int len = strlen(str);
/* Process the string, from 0 .. len - 1 */

This post has been edited by perfectly.insane: 15 Aug, 2008 - 01:49 PM
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project.
16 Aug, 2008 - 01:39 AM
Post #5

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

my guide asked me to get a Java interface for the compiler, to make it user friendly. He say there are things in the C actual compiler part which cannot be implemented from Java proper. Hence the result is the combination of both the language one for this and the other for that.
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project.
16 Aug, 2008 - 02:08 AM
Post #6

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

QUOTE(NickDMax @ 15 Aug, 2008 - 11:16 AM) *

well (*env)->ReleaseStringUTFChars(env,s,str);

is after the return statement and so it will never execute...

not really familiar with JNI. but I would think it would be something like this: (note code not compiled)

CODE
JNIEXPORT jstring JNICALL Java_project_test
(JNIEnv *env , jobject obj, jstring s)
{
    char str[256] =(*env)->GetStringUTFChars(env,s,0);
    int i = 2;
    if(str[0]=='^'&&(*str)[1]=='c') {
        do{
            toupper(str[i++]);
        } while(i < 255 && (str[i]!='c' && str[i+1]!='^'));
    }
    (*env)->ReleaseStringUTFChars(env,s,str);
    return((*env)->NewStringUTF(env,str));
}


this is based upon code from here.

Note: Why are you doing this in JNI? -- this far easier to do in java proper!


sorry, nick your code got the same problem as of me. Instead of returning capital it is returning garbage value.

i am using the string between ^c<char array>c^ because ^c and c^ is my command which is to be passed to make the required string capital.

this project is similar to the Latex software if anyone can help in that case.

QUOTE(perfectly.insane @ 15 Aug, 2008 - 02:48 PM) *

Is JNI necessary for this? I looked at your requirements in the Java section, but I don't see why you couldn't still implement it like a traditional compiler. By this I mean that the compiler would be a standalone executable, and the Java would simply be a front end to it.

Here are some examples (it covers utilizing the executing process's standard input and output streams also):
http://www.rgagnon.com/javadetails/java-0014.html

Also, with respect to the code, you cannot initialize C/C++ arrays at runtime with array initializes. What this means is that you can't do:

char somestr[100] = some_non_const_expr();

------

You can do:
char somestr[100] = "xxxx";

-- or --
char somestr[100] = { 'a', 'b', 'c', ... };


But for what you're doing, you need to do:

const char* str = (*env)->GetStringUTFChars(env,s,0);
int len = strlen(str);
/* Process the string, from 0 .. len - 1 */



Thanks, i just forgot that, i will try and let you know.
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project.
16 Aug, 2008 - 08:02 AM
Post #7

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

after using strlen the strupr is giving error.
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Compiler As Final Year Project.
16 Aug, 2008 - 08:09 AM
Post #8

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
A compile error, a runtime error, or it simply doesn't function as needed?
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project.
16 Aug, 2008 - 08:10 AM
Post #9

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

i am thinking of storing the pointer object to character array "*str" in another pointer and then applying strlen on str to get the length such as

char *t;
*t=*str;
int len =strlen(str)
.....
....
strupr(t);
....
...
will it work ?



This post has been edited by mdew_47: 16 Aug, 2008 - 08:11 AM
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Compiler As Final Year Project.
16 Aug, 2008 - 08:13 AM
Post #10

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
The variable t hasn't been initialized, so you can't do that. It would have to be:

char* t;
t = str;

Not:
*t = *str, which assigns the first character of str to the first character of t, and since t isn't initialized, it won't work (it will likely cause a crash).

This post has been edited by perfectly.insane: 16 Aug, 2008 - 08:14 AM
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project.
16 Aug, 2008 - 08:18 AM
Post #11

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

QUOTE(perfectly.insane @ 16 Aug, 2008 - 09:09 AM) *

A compile error, a runtime error, or it simply doesn't function as needed?





during compile time only warnings are given:


and the when i click the compile button garbage values are printed for infinite times.

QUOTE(perfectly.insane @ 16 Aug, 2008 - 09:13 AM) *

The variable t hasn't been initialized, so you can't do that. It would have to be:

char* t;
t = str;

Not:
*t = *str, which assigns the first character of str to the first character of t, and since t isn't initialized, it won't work (it will likely cause a crash).



so now i know why the JVM crashes.
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project.
16 Aug, 2008 - 08:24 AM
Post #12

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

QUOTE(perfectly.insane @ 16 Aug, 2008 - 09:13 AM) *

The variable t hasn't been initialized, so you can't do that. It would have to be:

char* t;
t = str;




it is now giving warning that: warning c4090: '=' : different 'const' qualifiers.

so i must the const out of: const char* str = (*env)->.......

isn't it?
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/2/08 10:25PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month