PDA

توجه ! این یک نسخه آرشیو شده می باشد و در این حالت شما عکسی را مشاهده نمی کنید برای مشاهده کامل متن و عکسها بر روی لینک مقابل کلیک کنید : برنامه های نوشته شده به زبان جاوا



sunyboy
10-25-2008, 07:25 PM
پروژه ی کامپایلر




تعریف متغیر و انتساب و عملگرهای محاسباتی ... فقط فاز اسکنر و پارسر هستش




public class compiler
{
public String[] t = {"keyword", "id" , "delimeter" , "operator" , "number"};
public int tc = 0;
public int [] token = new int [100];
////////////////////////////////////////////////////
public void scanner () throws Exception
{
char [] s = new char [100];
char ch = ' ';
int i= 0, l, j;
System.out.println("please enter your statments:");
while ((ch = (char)System.in.read()) != '#')
{
s[i] = ch;
i ++;
}
l = i - 1;
i = 0;
while (i <= l)
{
i = space (s, i);
if (i > l)
break;
if (s[i] == 13)
i+= 2;
if (letter (s, i))
i = id (s, i);
else if (digit (s, i))
i = number (s, i);
else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || s[i] == '%' || s[i] == '=')
i = cul (s, i);
else if (s[i] == ';'|| s[i] == '[' || s[i] == ']' || s[i] == '{' || s[i] == '}' || s[i] == 39 || s[i] == '(' || s[i] == ')' || s[i] == '"' ||s[i] == ',')
i = del (s, i);
else if (s[i] == '#')
System.exit(0);
}
System.out.println ("end of scan!");
token [tc] = 28;
pars(s);
}
///////////////////////////////////////////////////////////////////////
public int cul (char []s, int i)
{
int r=i;
switch (s[i])
{
case '=':
System.out.println ((tc +1) + ". =\t" + t[3]);
r = i + 1;
token[tc] = 5;
break;
case '+':
if (s[i + 1] == '=')
{
System.out.println ((tc +1) + ". +=\t" + t[3]);
r = i + 2;
token[tc] = 0;
}
else if (s[i + 1] == '+')
{
System.out.println ((tc + 1) + ". ++\t" + t [3]);
r = i + 2;
token[tc] = 15;
}
else
{
System.out.println ((tc +1) + ". +\t" + t[3]);
r = i + 1;
token[tc] = 13;
}
break;
case '-' :
if (s[i + 1] == '=')
{
System.out.println ((tc +1) + ". -=\t" + t[3]);
r = i + 2;
token[tc] = 1;
}
else if (s[i + 1] == '-')
{
System.out.println ((tc + 1) + ". --\t" + t [3]);
r = i + 2;
token[tc] = 16;
}
else
{
System.out.println ((tc +1) + ". -\t" + t[3]);
r = i + 1;
token[tc] = 14;
}
break;
case '*' :
if (s[i + 1] == '=')
{
System.out.println ((tc +1) + ". *=\t" + t[3]);
r = i + 2;
token[tc] = 2;
}
else
{
System.out.println ((tc +1) + ". *\t" + t[3]);
r = i + 1;
token[tc] = 6;
}
break;
case '/' :
if (s[i + 1] == '=')
{
System.out.println ((tc +1) + ". /=\t" + t[3]);
r = i + 2;
token[tc] = 3;
}
else
{
System.out.println ((tc +1) + ". /\t" + t[3]);
r = i + 1;
token[tc] = 7;
}
break;
case '%' :
if (s[i + 1] == '=')
{
System.out.println ((tc +1) + ". %=\t" + t[3]);
r = i + 2;
token[tc] = 4;
}
else
{
System.out.println ((tc +1) + ". %\t" + t[3]);
r = i + 1;
token[tc] = 8;
}
break;
}
tc ++;
return r;
}
///////////////////////////////////////////////////////////////////////
public int id (char []s, int i)
{
boolean flag = true;
int j = 1;
System.out.print ((tc + 1) + ". " + s[i]);
while (flag)
{
i ++;
if (letter (s, i) || digit (s, i))
{
System.out.print ( s[i]);
j++;
}
else
break;
}
token[tc] = 11;
switch (j)
{
case 3:
if (s[i - 3] == 'i' && s[i - 2] == 'n' && s[i -1] == 't')
{
token [tc] = 26;
System.out.println ("\t" + t[0]);
}
else
j = 0;
break;
case 4:
if (s [i - 4] == 'c' && s [i - 3] == 'h' && s [i - 2] == 'a' && s [i - 1] == 'r')
{
System.out.println ("\t" + t[0]);
token [tc] = 23;
}
else
j = 0;
break;
case 5:
if (s [i - 5] == 'f' && s [i - 4] == 'l' && s [i - 3] == 'o' && s [i - 2] == 'a' && s [i - 1] == 't')
{
System.out.println ("\t" + t[0]);
token [tc] = 24;
}
else
j = 0;
break;
case 6:
if (s [i - 6] == 's' && s [i - 5] == 't' && s [i - 4] == 'r' && s [i - 3] == 'i' && s [i - 2] == 'n' && s [i - 1] == 'g')
{
token[tc] = 20;
System.out.println ("\t" + t[0]);
}
else if (s[i - 6] == 'd' && s[i - 5] == 'o' && s[i - 4] == 'u' && s[i - 3] == 'b' && s[i - 2] == 'l' && s[i - 1] == 'e')
{
System.out.println ("\t" + t[0]);
token[tc] = 25;
}
else
j = 0;
break;
default:
System.out.println ("\t" + t[1]);
}
if (j == 0)
System.out.println ("\t" + t[1]);
tc ++;
return i;
}
///////////////////////////////////////////////////////////////////////////
public int number (char []s, int i)
{
boolean flag = true;
System.out.print ((tc + 1) + ". ");
while (flag)
{
if (digit (s, i))
System.out.print (s[i]);
else
break;
i++;
token[tc] = 9;
}
if (s[i] == '.')
{
System.out.print (s[i]);
while (flag)
{
i ++;
if (digit (s, i))
System.out.print (s [i]);
else
break;
}
token[tc] = 10;
}
System.out.println ("\t" + t[4]);
tc ++;
return i;
}
//////////////////////////////////////////////////////////////////////////
public int del (char []s, int i)
{
if (s[i] == '(' )
{
System.out.println((tc + 1) +". (\t" + t [2]);
i++;
token[tc] = 12;
}
if (s[i] == '"' )
{
System.out.println((tc + 1) +". "+ s[i]+"\t" + t [2]);
i++;
token[tc] = 17;
}
if (s[i] == ',' )
{
System.out.println((tc + 1) +". ,\t" + t [2]);
i++;
token[tc] = 18;
}
if (s[i] == '{' )
{
System.out.println((tc + 1) +". {\t" + t [2]);
i++;
token[tc] = 19;
}
if (s[i] == 39 )
{
System.out.println((tc + 1) +". '\t" + t [2]);
i++;
token[tc] = 21;
}
if (s[i] == '[' )
{
System.out.println((tc + 1) +". [\t" + t [2]);
i++;
token[tc] = 22;
}
if (s[i] == ';' )
{
System.out.println((tc + 1) +". ;\t" + t [2]);
i++;
token[tc] = 27;
}
if (s[i] == '}' )
{
System.out.println((tc + 1) +". }\t" + t [2]);
i++;
token[tc] = 29;
}
if (s[i] == ')' )
{
System.out.println((tc + 1) +". )\t" + t [2]);
i++;
token[tc] = 30;
}
if (s[i] == ']' )
{
System.out.println((tc + 1) +". ]\t" + t [2]);
i++;
token[tc] = 31;
}
tc++;
return i;
}
//////////////////////////////////////////////////////////////////////////
public boolean letter (char []s, int i)
{
if (('a' <= s[i] && s[i] <= 'z') || ('A' <= s[i] && s[i] <= 'Z'))
return true;
else
return false;
}
/////////////////////////////////////////////////////////////////////////
public boolean digit (char []s, int i)
{
if ('0' <= s[i] && s[i] <= '9')
return true;
else
return false;
}
////////////////////////////////////////////////////////////////////////
public int space (char []s, int i)
{
boolean flag = true;
while (flag)
{
if (s[i] == ' ' || s[i] == '\t' )
i ++;

else
break;
}
return i;
}
///////////////////////////////////////////////////////////////////////
public void pars (char []s)
{
int [][] table = new int [74][32];
int l=10, k, x, t;
stack stk = new stack();
for (l = 32; l< 74; l++)
for (t = 0; t <32; t++)
table [l][t] = 0;
table[32][11]=2;
table[32][15]=2;
table[32][16]=2;
table[32][20]=1;
table[32][23]=1;
table[32][24]=1;
table[32][25]=1;
table[32][26]=1;
table[32][28]=3;
table[33][20]=4;
table[33][23]=4;
table[33][24]=4;
table[33][26]=4;
table[34][20]=8;
table[34][23]=7;
table[34][24]=6;
table[34][26]=5;
table[35][26]=9;
table[36][5]=66;
table[36][22]=65;
table[36][27]=67;
table[37][22]=10;
table[38][22]=12;
table[38][27]=11;
table[39][9]=13;
table[39][12]=13;
table[40][18]=14;
table[40][29]=15;
table[41][9]=17;
table[41][11]=16;
table[42][24]=18;
table[42][25]=18;
table[43][24]=19;
table[43][25]=20;
table[44][22]=70;
table[45][9]=21;
table[45][10]=21;
table[45][11]=21;
table[46][18]=22;
table[46][29]=23;
table[47][9]=24;
table[47][10]=24;
table[47][11]=24;
table[48][23]=25;
table[49][5]=76;
table[49][21]=75;
table[49][27]=77;
table[50][20]=26;
table[51][20]=27;
table[52][18]=29;
table[52][29]=30;
table[53][20]=31;
table[54][20]=32;
table[55][5]=81;
table[55][22]=80;
table[55][27]=82;
table[56][17]=33;
table[57][17]=34;
table[58][18]=35;
table[58][29]=36;
table[59][9]=37;
table[59][10]=38;
table[59][11]=39;
table[60][17]=40;
table[61][11]=41;
table[61][15]=41;
table[61][16]=41;
table[62][11]=42;
table[62][15]=43;
table[62][16]=44;
table[63][0]=47;
table[63][1]=47;
table[63][2]=47;
table[63][3]=47;
table[63][4]=47;
table[63][5]=47;
table[63][15]=45;
table[63][16]=46;
table[64][0]=48;
table[64][1]=49;
table[64][2]=50;
table[64][3]=51;
table[64][4]=52;
table[64][5]=53;
table[65][9]=54;
table[65][10]=54;
table[65][11]=54;
table[65][12]=54;
table[66][13]=55;
table[66][14]=56;
table[66][28]=57;
table[66][30]=57;
table[67][9]=58;
table[67][10]=58;
table[67][11]=58;
table[67][12]=58;
table[68][6]=59;
table[68][7]=60;
table[68][9]=61;
table[68][28]=62;
table[68][30]=62;
table[69][9]=63;
table[69][10]=63;
table[69][11]=63;
table[69][12]=64;
table[70][5]=68;
table[70][27]=69;
table[71][5]=73;
table[71][27]=74;
table[72][5]=78;
table[72][27]=79;
table[73][5]=83;
table[73][27]=84;
stk.push(28);
stk.push(32);
tc = 0;
k = token[tc];
while(k > -1)
{
l = stk.pop();
if (l == k)
{
if (l == 28)
{
System.out.println ("accept");
System.exit(0);
}
else
{
// stk.pop();
tc++;
}
}
else if (k != l)
if ((l > -1 && l < 32) && (k > -1 && k < 32))
{
System.out.println ("ERROR in token " +(tc +1));
System.exit (0);
}
else if (l > 31)
{
stk.push(l);
x = table [l][k];
switch (x)
{
case 0:
System.out.println ("error in token " + (tc+1));
System.exit(0);
break;
case 1:
stk.pop();
stk.push(32);
stk.push(33);
break;
case 2:
stk.pop();
stk.push(32);
stk.push(61);
break;
case 3:
stk.pop();
break;
case 4:
stk.pop();
stk.push(27);
stk.push(34);
break;
case 5:
stk.pop();
stk.push(35);
break;
case 6:
stk.pop();
stk.push(42);
break;
case 7:
stk.pop();
stk.push(48);
break;
case 8:
stk.pop();
stk.push(54);
break;
case 9:
stk.pop();
stk.push(36);
stk.push(11);
stk.push(26);
break;
case 10:
stk.pop();
stk.push(38);
stk.push(31);
stk.push(9);
stk.push(22);
break;
case 11:
stk.pop();
break;
case 12:
stk.pop();
stk.push(31);
stk.push(9);
stk.push(22);
break;
case 13:
stk.pop();
stk.push(40);
stk.push(41);
break;
case 14:
stk.pop();
stk.push(40);
stk.push(41);
stk.push(18);
break;
case 15:
stk.pop();
break;
case 16:
stk.pop();
stk.push(11);
break;
case 17:
stk.pop();
stk.push(9);
break;
case 18:
stk.pop();
stk.push(44);
stk.push(11);
stk.push(43);
break;
case 19:
stk.pop();
stk.push(24);
break;
case 20:
stk.pop();
stk.push(25);
break;
case 21:
stk.pop();
stk.push(46);
stk.push(47);
break;
case 22:
stk.pop();
stk.push(45);
stk.push(18);
break;
case 23:
stk.pop();
break;
case 24:
stk.pop();
stk.push(59);
break;
case 25:
stk.pop();
stk.push(49);
stk.push(11);
stk.push(23);
break;
case 26:
stk.pop();
stk.push(21);
stk.push(59);
stk.push(21);
break;
case 27:
stk.pop();
stk.push(52);
stk.push(53);
break;
case 29:
stk.pop();
stk.push(53);
stk.push(18);
break;
case 30:
stk.pop();
break;
case 31:
stk.pop();
stk.push(50);
break;
case 32:
stk.pop();
stk.push(55);
stk.push(11);
stk.push(20);
break;
case 33:
stk.pop();
stk.push(17);
stk.push(59);
stk.push(17);
break;
case 34:
stk.pop();
stk.push(58);
stk.push(60);
break;
case 35:
stk.pop();
stk.push(60);
stk.push(18);
break;
case 36:
stk.pop();
break;
case 37:
stk.pop();
stk.push(9);
break;
case 38:
stk.pop();
stk.push(10);
break;
case 39:
stk.pop();
stk.push(11);
break;
case 40:
stk.pop();
stk.push(56);
break;
case 41:
stk.pop();
stk.push(27);
stk.push(62);
break;
case 42:
stk.pop();
stk.push(63);
stk.push(11);
break;
case 43:
stk.pop();
stk.push(11);
stk.push(15);
break;
case 44:
stk.pop();
stk.push(11);
stk.push(16);
break;
case 45:
stk.pop();
stk.push(15);
break;
case 46:
stk.pop();
stk.push(16);
break;
case 47:
stk.pop();
stk.push(65);
stk.push(64);
break;
case 48:
stk.pop();
stk.push(0);
break;
case 49:
stk.pop();
stk.push(1);
break;
case 50:
stk.pop();
stk.push(2);
break;
case 51:
stk.pop();
stk.push(3);
break;
case 52:
stk.pop();
stk.push(4);
break;
case 53:
stk.pop();
stk.push(5);
break;
case 54:
stk.pop();
stk.push(66);
stk.push(67);
break;
case 55:
stk.pop();
stk.push(66);
stk.push(67);
stk.push(13);
break;
case 56:
stk.pop();
stk.push(66);
stk.push(67);
stk.push(14);
break;
case 57:
stk.pop();
break;
case 58:
stk.pop();
stk.push(68);
stk.push(69);
break;
case 59:
stk.pop();
stk.push(68);
stk.push(69);
stk.push(6);
break;
case 60:
stk.pop();
stk.push(68);
stk.push(69);
stk.push(7);
break;
case 61:
stk.pop();
stk.push(68);
stk.push(69);
stk.push(8);
break;
case 62:
stk.pop();
break;
case 63:
stk.pop();
stk.push(59);
break;
case 64:
stk.pop();
stk.push(30);
stk.push(65);
stk.push(12);
break;
case 65:
stk.pop();
stk.push(70);
stk.push(37);
break;
case 66:
stk.pop();
stk.push(9);
stk.push(5);
break;
case 67:
stk.pop();
break;
case 68:
stk.pop();
stk.push(29);
stk.push(39);
stk.push(19);
stk.push(5);
break;
case 69:
stk.pop();
break;
case 70:
stk.pop();
stk.push(71);
stk.push(37);
break;
case 71:
stk.pop();
stk.push(59);
stk.push(5);
break;
case 72:
stk.pop();
break;
case 73:
stk.pop();
stk.push(29);
stk.push(45);
stk.push(19);
stk.push(5);
break;
case 74:
stk.pop();
break;
case 75:
stk.pop();
stk.push(72);
stk.push(37);
break;
case 76:
stk.pop();
stk.push(50);
stk.push(5);
break;
case 77:
stk.pop();
break;
case 78:
stk.pop();
stk.push(29);
stk.push(51);
stk.push(19);
stk.push(5);
break;
case 79:
stk.pop();
break;
case 80:
stk.pop();
stk.push(73);
stk.push(37);
break;
case 81:
stk.pop();
stk.push(56);
stk.push(5);
break;
case 82:
stk.pop();
break;
case 83:
stk.pop();
stk.push(29);
stk.push(57);
stk.push(19);
stk.push(5);
break;
case 84:
stk.pop();
break;
}//switch
}// else if
k = token[tc];
}//while
}
}

public class stack
{
private int top= 0;
private int size=100;
private int [] elem = new int [100];
///////////////////////////////////////////////
private boolean isfull ()
{
if (top >= size)
return true;
else
return false;
}
///////////////////////////////////////////////
private boolean isempty ()
{
if ( top < 0)
return true;
else
return false;
}
////////////////////////////////////////////////
public void push (int z)
{
if (isfull())
{
System.out.print ("stack is full!!");
System.exit (1);
}
elem [top] = z;
top++;
}
///////////////////////////////////////////////
public int pop ()
{
top--;
if (isempty())
{
System.out.println ("stack is empty!!!");
System.exit (2);
}
return elem[top];
}
}





public class project
{
public static void main(String []arg) throws Exception
{
compiler p = new compiler();
p.scanner();
}
}

sunyboy
11-02-2008, 02:03 AM
آموزش ساخت یک IDE



یک پوشه به اسم jde مخفف (java development environment) بسازید و وارد اون بشید با دستور copy con jde.java و یا via jde.java توی لینوکس یک فایل بسازید و مستقیم دستورات زیر رو توش وارد کنید



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

public class jde extends JFrame {
private JTextArea _editArea;
private JFileChooser _fileChooser = new JFileChooser();
private Action _openAction = new OpenAction();
private Action _saveAction = new SaveAction();
private Action _exitAction = new ExitAction();

public static void main(String[] args) {
new jde();
}
public jde() {
_editArea = new JTextArea(15, 80);
_editArea.setBorder(BorderFactory.createEmptyBorde r(2,2,2,2));
_editArea.setFont(new Font("monospaced", Font.PLAIN, 14));
JScrollPane scrollingText = new JScrollPane(_editArea);
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollingText, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = menuBar.add(new JMenu("File"));
fileMenu.setMnemonic('F');
fileMenu.add(_openAction);
fileMenu.add(_saveAction);
fileMenu.addSeparator();
fileMenu.add(_exitAction);
setContentPane(content);
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("jde");
pack();
setLocationRelativeTo(null);
setVisible(true);
}
class OpenAction extends AbstractAction {
public OpenAction() {
super("Open...");
putValue(MNEMONIC_KEY, new Integer('O'));
}
public void actionPerformed(ActionEvent e) {
int retval = _fileChooser.showOpenDialog(jde.this);
if (retval == JFileChooser.APPROVE_OPTION) {
File f = _fileChooser.getSelectedFile();
try {
FileReader reader = new FileReader(f);
_editArea.read(reader, "");
} catch (IOException ioex) {
System.out.println(e);
System.exit(1);
}
}
}
}
class SaveAction extends AbstractAction {
SaveAction() {
super("Save...");
putValue(MNEMONIC_KEY, new Integer('S'));
}
public void actionPerformed(ActionEvent e) {
int retval = _fileChooser.showSaveDialog(jde.this);
if (retval == JFileChooser.APPROVE_OPTION) {
File f = _fileChooser.getSelectedFile();
try {
FileWriter writer = new FileWriter(f);
_editArea.write(writer);
} catch (IOException ioex) {
JOptionPane.showMessageDialog(jde.this, ioex);
System.exit(1);
}
}
}
}
class ExitAction extends AbstractAction {
public ExitAction() {
super("Exit");
putValue(MNEMONIC_KEY, new Integer('X'));
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
}




و با دستور javac jde.java کمپایلش کنید و با java jde اجراش کنید.
تبریک میگم اولین قدم برای ساخت ide رو برداشتید

خروجی برنام

http://www.pnu-club.com/imported/2008/11/8.jpg

موفق باشید