佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1190|回复: 3

詢問(緊急)! 如何轉換C語言去JAVA語言?

[复制链接]
发表于 16-8-2008 08:32 PM | 显示全部楼层 |阅读模式
詢問(緊急)! 如何轉換C語言去JAVA語言?                                                                                                                                                                                                                                                                                                                        大家好,我是多媒體大學的學生。
最近有個課業要求我們轉換C語言去JAVA語言。
請問有誰可以教教我?

講師給了我一個vsm.c的file要求我們translate去.java file
我嘗試用了在internet找到的c2j translator可是轉換后的.java file不能compile和execute.
有150個error.

懇求各位專業人士能給予指導.
謝謝!
回复

使用道具 举报


ADVERTISEMENT

发表于 16-8-2008 11:13 PM | 显示全部楼层
可以看看vsm.c的code嗎?
回复

使用道具 举报

 楼主| 发表于 17-8-2008 12:35 AM | 显示全部楼层

回复 3# SotongJiang 的帖子

// VSM interpreter 1.2
// (c)2007 Ian Chai
// This is a simple stack machine assembly language interpreter.

// #define DUMP                // Print a memory dump at the end of execution
// #define TRACE        // Print a memory dump at each step

#include <stdio.h>
#ifdef TRACE
#include <curses.h>
#define OUTPUTLINES 2        // How many lines to reserve for output window.
int line, col;                // Remember where in the output you were.
#endif

#define VALSIZE 80
#define MEMSIZE 1000
#define SYMSIZE 100a

typedef struct
{ int opcode;
  char label[VALSIZE];
  int number;
} TokType;

int *mem;
size_t memsize;
TokType *symtbl;
size_t symsize;
int symcount;
int pc, fp, sp, linenum;

enum opcodes { NOP, ADD, BNZ, BRA, BZE, CALL, DB, DEREF, DIV,
        HALT, MOD, MUL, POP, POPSTO, PUSH, PUSHFP, PUSHPC, READ, RET,
        RETVAL, SUB, BLZ, BGZ, LT, GT, EQ, NOT, TRAP, COLON, NUMBER,
        LABEL };

#define NUMOP (TRAP+1)

char *op[NUMOP] = { "NOP", "ADD", "BNZ", "BRA", "BZE", "CALL", "DB",
        "DEREF", "DIV", "HALT", "MOD", "MUL", "POP", "POPSTO", "PUSH",
        "PUSHFP", "PUSHPC", "READ", "RET", "RETVAL", "SUB", "BLZ",
        "BGZ", "LT", "GT", "EQ", "NOT", "TRAP" };

TokType lex(FILE *in);
int getWord(FILE *in, TokType *tok);
void initialize(void);
void load(FILE *in);
void execute(void);
void dump(void);
void dumpPageAndPause(int place);
void push(int value);
int pop(void);
void labelUse(TokType tok);
void labelDefine(TokType tok);
int labelFind(char label[]);
int insertLabel(TokType tok);
void backpatch(int addr, int place);
void printLabelErrorAndExit(char label[]);
void printLexicalErrorAndExit(char got, char expected);
void printRuntimeErrorAndExit(char err[]);
void printSyntaxErrorAndExit(char err[], TokType tok);
void printMemoryErrorAndExit(char varName[], int location);
void printOpcodeErrorAndExit(int code);
void printMem(int loc);

int main(int argc, char *argv[])
{ FILE *in;
  TokType tok;

  if (argc==2)
  { in = fopen(argv[1],"r");
    if (in==NULL)
    { perror("opening input file");
      exit(1);
    }
  }
  else
    in = stdin;

#ifdef TRACE
  initscr();
  line = LINES - OUTPUTLINES;
  col = 0;
#endif

  initialize();
  load(in);
  execute();

#ifdef TRACE
  endwin();
#endif

#ifdef DUMP
  dump();
#endif
}

void initialize(void)
{ mem = (int *)malloc(sizeof(int)*MEMSIZE);
  memsize = MEMSIZE;
  symtbl = (TokType *)malloc(sizeof(TokType)*SYMSIZE);
  symsize = SYMSIZE;
  symcount = 0;
  pc = 0;
  fp = 0;
  sp = 0;
  linenum = 1;
}

void printLabelErrorAndExit(char label[])
{ fprintf(stderr,"Line %d: Label Error: %s redefined\n", linenum,label);
  exit(1);
}

void printLexicalErrorAndExit(char got, char expected)
{
  fprintf(stderr,"Line %d: Lexical Error: '%c' expected; ",linenum,expected);
  fprintf(stderr,"'%c' found instead.\n", got);
  exit (1);
}

void printSyntaxErrorAndExit(char err[], TokType tok)
{
  fprintf(stderr,"Line %d: Syntax Error: %s expected; ",linenum,err);
  switch (tok.opcode)
  { case NUMBER:
      fprintf(stderr,"NUMBER %d", tok.number);
      break;
    case LABEL:
      fprintf(stderr,"LABEL %s", tok.label);
      break;
    case COLON:
      fprintf(stderr,"COLON");
      break;
    default:
      fprintf(stderr,"%s", op[tok.opcode]);
  }
  fprintf(stderr," found instead.\n");
  exit (1);
}

void printRuntimeErrorAndExit(char err[])
{
  fprintf(stderr,"Runtime error: %s\n", err);
  dump();
  exit(1);
}

void printMemoryErrorAndExit(char varName[], int location)
{
  fprintf(stderr,"Memory access out of bounds error: %s, %d\n",
      varName, location);
  dump();
  exit(1);
}

void printOpcodeErrorAndExit(int code)
{
  fprintf(stderr,"Illegal opcode error: %d\n", code);
  dump();
  exit(1);
}

void push(int value)
// Enter an entry into the memory and increment sp
// and make more memory if not enough.
{ if (sp >= memsize)
  { memsize += MEMSIZE;
    mem = (int *)realloc(mem, memsize * sizeof(int));
  }

  mem[sp++] = value;
}

int pop(void)
{ if (sp<=0)
    printRuntimeErrorAndExit("Stack underrun");
  sp--;
  return (mem[sp]);
}

void load(FILE *in)
{ TokType tok;
  while (tok = lex(in), tok.opcode!=EOF)
  { switch (tok.opcode)
    { case PUSH:        // PUSH takes a number or label so fall through
        push(tok.opcode);        // to DB case to do the number or label.
      case DB:                // Don't enter DB into memory
        tok = lex(in);
        switch (tok.opcode)
        { case NUMBER:
            push(tok.number);
            break;
          case LABEL:
            labelUse(tok);
            break;
          default:
            printSyntaxErrorAndExit("NUMBER or LABEL",tok);
        }
        break;
      case LABEL:
        labelDefine(tok);
        tok = lex(in);
        if (tok.opcode != COLON)
          printSyntaxErrorAndExit("COLON",tok);
        break;
      default:
        if (tok.opcode < NUMOP)
          push(tok.opcode);
        else
          printSyntaxErrorAndExit("VSM code",tok);
    }
  }
}

void printMem(int loc)
{ printf("\t%8d\t%4d",loc,mem[loc]);
  if ((mem[loc]<NUMOP)&&(mem[loc]>=0))
    printf("\t%s",op[mem[loc]]);
  if ((mem[loc]>=' ') && (mem[loc]<='~'))
    printf("\t'%c'",mem[loc]);
  putchar('\n');
}

#ifdef TRACE
void printMemCurses(int loc)
{ printw("\t%8d\t%4d",loc,mem[loc]);
  if ((mem[loc]<NUMOP)&&(mem[loc]>=0))
    printw("\t%s",op[mem[loc]]);
  if ((mem[loc]>=' ') && (mem[loc]<='~'))
    printw("\t'%c'",mem[loc]);
  addch('\n');
}
#endif

void dump(void)
{ int i;

  printf("\nMemory currently contains:\n");
  printf("PC=%d\t FP=%d\n",pc,fp);
  for (i=0; i<sp; i++)
  { if (i==pc) printf("PC ");
    if (i==fp) printf("FP ");
    printMem(i);
  }

  printf("Symbol table:\n");
  for (i=0; i<symcount; i++)
  { printf("%s\t%d\t",symtbl.label,symtbl.number);
    if (symtbl.opcode)
      printf("Defined\n");
    else
      printf("Not yet defined\n");
  }
}

#ifdef TRACE
void dumpPageAndPause(int place)
{ int top, m1, m2, bottom, i;
#define WINLINES (LINES-OUTPUTLINES)

  if (sp-place < WINLINES)
  { top = sp - WINLINES;
    if (top<0) top=0;
    m1 = m2 = bottom = sp;
  }
  else
  { top = place - WINLINES/4;
    if (top < 0) top = 0;
    m1 = top + WINLINES/2.0;
    bottom = sp;
    m2 = bottom - WINLINES/2.0;
  }

  move(0,0);

  for (i=top; i<m1; i++)
  { if (i==pc) printw("PC ");
    if (i==fp) printw("FP ");
    printMemCurses(i);
  }
  standout();
  for (i=m2; i<bottom; i++)
  { if (i==pc) printw("PC ");
    if (i==fp) printw("FP ");
    printMemCurses(i);
  }
  standend();

  while (getch()!='\n');
}
#endif

TokType lex(FILE *in)
// Get the next token from the input file
{ TokType tok;
  int i;

  i = getWord(in,&tok);        // Read the next word into tok
  if (i==EOF)
  { tok.opcode=EOF;
    return tok;
  }
  if (i==NUMBER)
    tok.opcode = NUMBER;
  else if (i==COLON)
    tok.opcode = COLON;
  else
  { for (i=0; i<NUMOP; i++)
      if (strcasecmp(tok.label,op))
        continue;
      else
      { tok.opcode = i;
        break;
      }
    if (i>=NUMOP)
      tok.opcode=LABEL;
  }
  return tok;
}

int getc_incr_linenum(FILE *in)
{ int c = fgetc(in);
  if (c=='\n') linenum++;
}

int ungetc_decr_linenum(int c, FILE *in)
{ if (c=='\n') linenum--;
  ungetc(c,in);
}

int getWord(FILE *in, TokType *tok)
// Get the next word from the input file, ignoring blanks.
// Return EOF if there are no more words.
// Return 1 if the word is a number.
{ int c,i,sign=1;

  i = 0;
  
restart:
  while (isspace(c=getc_incr_linenum(in)));

  if (c=='\\')        // comment
  { while(getc_incr_linenum(in)!='\n');        // Get rid of rest of line
    goto restart;
  }

  if (c==':')
  { tok->label[0]=c;
    tok->label[1]='\0';
    return COLON;
  }

  if (c=='\'')        // Character constant
  { tok->label[0] = '\'';
    c = tok->label[1] = tok->number = getc_incr_linenum(in);
    c = tok->label[2] = getc_incr_linenum(in);
    if (c!='\'') printLexicalErrorAndExit(c,'\'');
    return NUMBER;
  }

  if (c=='-')
  { sign=-1;
    tok->label[i++]=c;
    c = getc_incr_linenum(in);
    if (!isdigit(c))        // Treat as a label
    { tok->label='\0';
      ungetc_decr_linenum(c,in);
      return LABEL;
    }
  }

  if (c==EOF) return EOF;

  if (isdigit(c))        // Numeric mode.
  { tok->number = 0;
    do
    {
      tok->number = ((tok->number)*10) + (c-'0');
      tok->label = c;
      if (i<VALSIZE) i++;
      c = getc_incr_linenum(in);
    } while (isdigit(c));
    tok->label='\0';
    ungetc_decr_linenum(c,in);        // Put back extra character.
    tok->number *= sign;
    return NUMBER;
  }
  else                        // Identifier mode
  { do
    { tok->label = c;
      if (i<VALSIZE) i++;
      c = getc_incr_linenum(in);
    } while (isalnum(c));
    if (c!=EOF) ungetc_decr_linenum(c,in);
    tok->label = '\0';
    return LABEL;
  }
}

int labelFind(char label[])
{ int i;

  for (i=0; i<symcount; i++)
  { if (strcasecmp(label,symtbl.label)==0)
      return i;
  }
  return -1;
}
回复

使用道具 举报

 楼主| 发表于 17-8-2008 12:35 AM | 显示全部楼层
void labelUse(TokType tok)
{ int i, currsp = sp;

  i = labelFind(tok.label);
  if (i<0)        // i.e. not found
  { tok.number = 0;                // An undefined lable,
    tok.opcode = 0;                // so must indicate will backpatch.
    i = insertLabel(tok);
  }
  push(symtbl.number);        // Insert the label's location
  if (symtbl.opcode==0)        // It's an undefined label, so
    symtbl.number = currsp;        // update the backpatch chain.
}

void labelDefine(TokType tok)
{ int i, currsp = sp;

  i = labelFind(tok.label);
  if (i<0)        // i.e. not found, so first use is define
  { tok.number = currsp;
    tok.opcode = 1;        // Don't need to backpatch
    i = insertLabel(tok);
  }
  else        // Must backpatch
  { if (symtbl.opcode == 1)        // Double definition of label!
      printLabelErrorAndExit(symtbl.label);
    backpatch(symtbl.number,currsp);
    symtbl.opcode = 1;
    symtbl.number = currsp;
  }
}

int insertLabel(TokType tok)
{ symtbl[symcount++] = tok;
  if (symcount > symsize)        // Need to grow the symbol table
  { symsize += SYMSIZE;
    symtbl = (TokType *) realloc (symtbl, sizeof(TokType));
  }
  return symcount-1;
}

void backpatch(int addr, int place)
{ if (mem[addr])
    backpatch(mem[addr], place);
  mem[addr] = place;
}

#define memcheck(loc,varName)                        \
{ if ((loc)>=sp)                                        \
    printMemoryErrorAndExit(varName,pc);        \
}

void execute(void)
{ int t1, t2;        // temporary variables for arithmetic, etc.
  while (mem[pc]!=HALT)
  {
#ifdef TRACE
    dumpPageAndPause(pc);
#endif
    switch(mem[pc])
    { case NOP:
        break;
      case TRAP:
#ifdef TRACE
        move(line,col);
        if (mem[sp-1]=='\r')
        { line++;
          col = 0;
        }
        else
        { if (!iscntrl(mem[sp-1]))
          { addch(mem[sp-1]);
            col++;
            if (col >= COLS)
            { col = 0;
              line ++;
            }
          }
        }
        if (line > LINES)
        { scroll(stdscr);
          line --;
        }
#else
        putchar(mem[sp-1]);
#endif
        sp--;
        break;
      case READ:
        push(getchar());
        break;
      case ADD:
        t2 = pop();
        t1 = pop();
        push(t1+t2);
        break;
      case SUB:
        t2 = pop();
        t1 = pop();
        push(t1-t2);
        break;
      case MOD:
        t2 = pop();
        t1 = pop();
        push(t1%t2);
        break;
      case MUL:
        t2 = pop();
        t1 = pop();
        push(t1*t2);
        break;
      case DIV:
        t2 = pop();
        t1 = pop();
        push(t1/t2);
        break;
      case LT:
        t2 = pop();
        t1 = pop();
        push (t1<t2);
        break;
      case GT:
        t2 = pop();
        t1 = pop();
        push (t1>t2);
        break;
      case EQ:
        t2 = pop();
        t1 = pop();
        push (t1==t2);
        break;
      case NOT:
        t1 = pop();
        push (!t1);
        break;
      case PUSH:
        pc++;
        push(mem[pc]);
        break;
      case PUSHFP:
        push(fp);
        break;
      case PUSHPC:
        push(pc);
        break;
      case POPSTO:
        t2 = pop();
        t1 = pop();
        memcheck(t1, "POPSTO");
        mem[t1] = t2;
        break;
      case POP:
        t1 = pop();
        break;
      case BRA:
        t1 = pop();
        pc = t1-1;
        break;
      case BZE:
        t2 = pop();
        t1 = pop();
        if (t2==0) pc = t1-1;
        break;
      case BNZ:
        t2 = pop();
        t1 = pop();
        if (t2!=0) pc = t1-1;
        break;
      case BLZ:
        t2 = pop();
        t1 = pop();
        if (t2<0) pc = t1-1;
        break;
      case BGZ:
        t2 = pop();
        t1 = pop();
        if (t2>0) pc = t1-1;
        break;
      case DEREF:
        t1 = pop();
        memcheck(t1, "DEREF");
        push(mem[t1]);
        break;
      case CALL:
        t1 = pop();
        push(fp);
        push(pc+1);
        fp = sp-1;
        pc = t1-1;
        break;
      case RET:
        sp = fp-1;
        pc = mem[fp]-1;
        fp = mem[fp-1];
        break;
      case RETVAL:
        t1 = pop();
        sp = fp-1;
        pc = mem[fp]-1;
        fp = mem[fp-1];
        push(t1);
        break;
      default:
        printOpcodeErrorAndExit(mem[pc]);
    }
    pc ++;
    if (pc >= sp)
      printMemoryErrorAndExit("PC",pc);
  }
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 24-12-2025 06:41 AM , Processed in 0.096306 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表