Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,450 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,107 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, i have been given a 2-phase compiler containig the lexical phase and t

Rating  5
mdew_47
2 Aug, 2008 - 11:03 PM
Post #1

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

i have been given a 2-phase compiler containig the lexical phase and the syntax phase as my final year project. for that i need to know how to call a C program from a Java applet using a button called "compile". A little help will make me thankful.

1. Write a C program for the compiler.
2. Build an interface with JAVA.
3. Give some button, give a text area.
4. Write any thing in the text area.
5.when you click the "compile" button the C program must be called and it should compile the text in the text area.

My guide haven't told me about security, so it must be stand alone, just a 2-phase compiler running on one machine.


i have gone through "The Complete reference to Java- by Herbert Schildt"

1. There it say i must declare the method i want to call from the C program as:

public native int method();

we should not define the method here.

2. Produce a header to use in the C program by:

javah -jni <Java Program name>

3. Write the C program using:
a. # include <jni.h>
b.# include "programname.h" /* the header file we created"
c. # include <stdio.h>

4. As i am doing in Windows environment so:

to compile the C program i should use

Cl/LD <C programname.c>

BUT after compiling the error in am getting is:

fatal error C1083: Cannot open include file:'jni.h': No such files or directory

Can you help to solve this?


My Java code is:

java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class compiler extends Frame implements ActionListener{
TextArea A = new TextArea(32,140);
Button S = new Button("SAVE");
Button C = new Button("COMPILE");
Button Cl = new Button("CLEAR");
Button E = new Button("EXIT");
public static void main(String args[])
{
compiler f = new compiler();
f.setSize(900,900);
f.setVisible(true);
}
public compiler(){
setTitle("COMPILER");
Panel p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(A);
Panel p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.add(S);
p2.addİ;
p2.add(Cl);
p2.add(E);
setLayout(new FlowLayout(FlowLayout.LEFT));
add(p1);
add(p2);
S.addActionListener(this);
C.addActionListener(this);
Cl.addActionListener(this);
E.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
compiler f = new compiler();
String args = e.getActionCommand();
if(e.getSource() instanceof Button)
{
if(args.equals("SAVE"))
{
}
if(args.equals("COMPILE"))
{
f.test();
}
if(args.equals("CLEAR"))
{
A.setText("");
A.requestFocus();
}
if(args.equals("EXIT"))
{
System.exit(0);
}
}
}
public native void test();
}


and C program code is:

# include <jni.h>
# include "compiler.h"
# include <stdio.h>
JNIEXPORT void JNICALL Java_Compiler_test(JNIEnv *env, jobject obj)
{
printf("Testing!");
}

*edit: Please use code tags in the future, thanks! code.gif

This post has been edited by Martyr2: 3 Aug, 2008 - 10:50 AM
User is offlineProfile CardPM
+Quote Post

lordms12
RE: Compiler As Final Year Project
3 Aug, 2008 - 12:39 AM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 314



Thanked: 15 times
Dream Kudos: 225
My Contributions
Please post your code like IPB Image

About the compiler you can use tools like Lex/Flex - and - Yacc/Bison to in your rules and out your compiler
Simple Flex input:
lex
%{
#include "y.tab.h"
#include <stdlib.h>
void yyerror(char *);
%}

%%

[a-z] {
yylval = *yytext - 'a';
return VARIABLE;
}

[0-9]+ {
yylval = atoi(yytext);
return INTEGER;
}

[-+()=/*\n] { return *yytext; }

[ \t] ; /* skip whitespace */

. yyerror("Unknown character");

%%

int yywrap(void) {
return 1;
}

Simple Bison input
yacc
%{
#include <stdio.h>
int yylex(void);
void yyerror(char *);
%}

%token INTEGER

%%

program:
program expr '\n' { printf("%d\n", $2); }
|
;

expr:
INTEGER
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
;

%%

void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
}

int main(void) {
yyparse();
return 0;
}


If you found this way is hard you can use some programs like UltraGram (Check its examples)

I do not think you will face problems with remaining parts except with JNI

User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project
3 Aug, 2008 - 02:51 AM
Post #3

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

I don't think your suggestion will help me.

I know all about LEX and YACC but i am not asked to use it.

My compiler will be program in C.

Now i need to call that C program from a JAVA GUI, and that only is creating problem, for i am getting the error mentioned above.

Thanks anyway, but it doesn't solve my problem wink2.gif





User is offlineProfile CardPM
+Quote Post

lordms12
RE: Compiler As Final Year Project
3 Aug, 2008 - 03:51 AM
Post #4

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 314



Thanked: 15 times
Dream Kudos: 225
My Contributions
I did not see the error between all of this, why was not your title meaningful!!!

# include <"C:\Program Files\Java\jdk1.6.turn.gif1\include\jni.h">

Please, edit your first post to make it readable.
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project
3 Aug, 2008 - 04:49 AM
Post #5

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

yes the error was:

fatal error C1083: Cannot open include file:'jni.h': No such files or directory

this is because i used # include<jni.h>

i will try what you thought and if successful i will let you know.
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project
3 Aug, 2008 - 05:02 AM
Post #6

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

i tried and it is now giving the error:

C:\j2sdk1.4.2_03\include\jni.h(27) : fatal error C1083: Cannot open include file : 'jni_md.h' : No such file or directory


jni_md.h is in win32 folder which is include folder. So how can i solve this now. Thank

the book i gone through also said "You might need to specify the path to jni.h and its subordinate file jni_md.h

User is offlineProfile CardPM
+Quote Post

lordms12
RE: Compiler As Final Year Project
3 Aug, 2008 - 10:41 AM
Post #7

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 314



Thanked: 15 times
Dream Kudos: 225
My Contributions
Copy C:\j2sdk1.4.2_03\include\ content into your compiler folder
I am using CodeBlocks with MinGW compiler so I would copy into this folder C:\Program Files\CodeBlocks\MinGW\include

This post has been edited by lordms12: 3 Aug, 2008 - 10:42 AM
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project
5 Aug, 2008 - 06:48 AM
Post #8

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

i
Copied C:\j2sdk1.4.2_03\include\ s all contents into my turbo c++ compiler's "include" folder. But i am still getting the same error.

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

mdew_47
RE: Compiler As Final Year Project
5 Aug, 2008 - 07:50 AM
Post #9

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

as you can see i forgot to use:

CODE
static{
System.loadLibrary("compiler");
}


Now when i used it:

1.compiling with Javac gives no error.
2.But after i use Java compiler it gives:

Exception in thread "main" java.lang.unsatisfiedLinkError: no compiler in java.library.path
at java.lang.ClassLoader.loadlibrary(ClassLoader.java:1491)
java.lang.Runtime.loadLibrary0(Runtime.java:788)
java.lang.System.loadlibrary(System.java:834)
compiler.<clinit>(compiler.java:63)
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project
7 Aug, 2008 - 08:13 AM
Post #10

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

i am able to call the C program, but as i am using a printf statement only i am getting the result in the command prompt. but i want to bring it to the text area which i kept for that

How can i do that, can anybody help.
User is offlineProfile CardPM
+Quote Post

prajayshetty
RE: Compiler As Final Year Project
7 Aug, 2008 - 02:09 PM
Post #11

D.I.C Head
Group Icon

Joined: 27 Apr, 2007
Posts: 230


Dream Kudos: 25
My Contributions
i am not sure weather this info will help u see if u are creating a gui in java or in c in c just send that info to ur java program that statement to be printed to your java program
i mean either by files or someother that u will have to figure out i didnt did any project such using 2 languange never used jni or jna so dont havemuch info anyway i hope this info might help you

This post has been edited by prajayshetty: 7 Aug, 2008 - 02:10 PM
User is offlineProfile CardPM
+Quote Post

mdew_47
RE: Compiler As Final Year Project
9 Aug, 2008 - 07:03 AM
Post #12

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 32

what i write in the Text area i can show that in the command prompt screen now, using JNI.

But now i want that thing to be returned to that text area, how can i do that can anybody help?
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 02:55PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month