从C到Java的转换

从C到Java的转换,java,c,Java,C,我正在使用下面显示的代码,并试图将其从C代码转换为java代码。这个程序应该是一个词法分析器。我发现大多数C代码与java代码非常相似,但我发现某些部分与之不同。在所讨论的部分中,我将其放入了一个转换器中,发现它没有以与java相同的方式“使用” /* front.c - a lexical analyzer system for simple arithmetic expressions */ #include <stdio.h> #include <ctype.h>

我正在使用下面显示的代码,并试图将其从C代码转换为java代码。这个程序应该是一个词法分析器。我发现大多数C代码与java代码非常相似,但我发现某些部分与之不同。在所讨论的部分中,我将其放入了一个转换器中,发现它没有以与java相同的方式“使用”

/* front.c - a lexical analyzer system for simple arithmetic expressions */
#include <stdio.h>
#include <ctype.h>

/* Global declarations */
/* Variables */
int charClass;
char lexeme [100];
char nextChar;
int lexLen;
int token;
int nextToken;
FILE *in_fp, *fopen();

/* Function declarations */
void addChar();
void getChar();
void getNonBlank();
int lex();

/* Character classes *
#define LETTER 0
#define DIGIT 1
#define UNKNOWN 99

/* Token codes */
#define INT_LIT 10
#define IDENT 11
#define ASSIGN_OP 20
#define ADD_OP 21
#define SUB_OP 22
#define MULT_OP 23
#define DIV_OP 24
#define LEFT_PAREN 25
#define RIGHT_PAREN 26




/******************************************************/
/* main driver */
main() 
{
/* Open the input data file and process its contents */
    if ((in_fp = fopen("front.in", "r")) == NULL)
        printf("ERROR - cannot open front.in \n");
    else {
        getChar();
    do {
        lex();
    } while (nextToken != EOF);
    }
}
/*****************************************************/
/* lookup - a function to lookup operators and parentheses and return the token */
int lookup(char ch) {
    switch (ch) {
        case '(':
        addChar();
        nextToken = LEFT_PAREN;
        break;

        case ')':
        addChar();
        nextToken = RIGHT_PAREN;
        break;

        case '+':
        addChar();
        nextToken = ADD_OP;
        break;

        case '-':
        addChar();
        nextToken = SUB_OP;
        break;

        case '*':
        addChar();
        nextToken = MULT_OP;
        break;

        case '/':
        addChar();
        nextToken = DIV_OP;
        break;

        default:
        addChar();
        nextToken = EOF;
        break;
    }
    return nextToken;
}




/*****************************************************/
/* addChar - a function to add nextChar to lexeme */
void addChar() {
    if (lexLen <= 98) {
        lexeme[lexLen++] = nextChar;
        lexeme[lexLen] = 0;
    }
    else
        printf("Error - lexeme is too long \n");
}
/*****************************************************/
/* getChar - a function to get the next character of input and determine its character class */
void getChar() 
{
    if ((nextChar = getc(in_fp)) != EOF) 
    {
        if (isalpha(nextChar))
        charClass = LETTER;
        else if (isdigit(nextChar))
        charClass = DIGIT;
        else charClass = UNKNOWN;
    }
    else
    charClass = EOF;
}
/*****************************************************/
/* getNonBlank - a function to call getChar until it returns a non-whitespace character */
void getNonBlank() 
{
    while (isspace(nextChar))
    getChar();
}




*****************************************************/
/* lex - a simple lexical analyzer for arithmetic expressions */
int lex() 
{
    lexLen = 0;
    getNonBlank();
    switch (charClass) 
    {

        /* Parse identifiers */
        case LETTER:
        addChar();
        getChar();
        while (charClass == LETTER || charClass == DIGIT) 
        {
            addChar();
            getChar();
        }
    nextToken = IDENT;
    break;
    /* Parse integer literals */
    case DIGIT:
    addChar();
    getChar();
    while (charClass == DIGIT) 
    {
        addChar();
        getChar();
    }
    nextToken = INT_LIT;
    break;
    /* Parentheses and operators */
    case UNKNOWN:
    lookup(nextChar);
    getChar();
    break;
    /* EOF */
    case EOF:
    nextToken = EOF;
    lexeme[0] = 'E';
    lexeme[1] = 'O';
    lexeme[2] = 'F';
    lexeme[3] = 0;
    break;
    } /* End of switch */
    printf("Next token is: %d, Next lexeme is %s\n",
    nextToken, lexeme);
    return nextToken;
} /* End of function lex */
/*front.c-用于简单算术表达式的词法分析器系统*/
#包括
#包括
/*全球宣言*/
/*变数*/
int字符类;
字符词素[100];
char nextChar;
int lexLen;
int标记;
int nextToken;
文件*in_fp,*fopen();
/*函数声明*/
void addChar();
void getChar();
void getNonBlank();
int-lex();
/*字符类*
#定义字母0
#定义数字1
#定义未知99
/*令牌码*/
#定义INT_LIT 10
#定义标识符11
#定义赋值运算20
#定义ADD_OP 21
#定义子单元OP 22
#定义MULT_OP 23
#定义分区OP 24
#定义左对齐25
#定义右对齐项26
/******************************************************/
/*主要驱动力*/
main()
{
/*打开输入数据文件并处理其内容*/
如果((in_fp=fopen(“front.in”,“r”))==NULL)
printf(“错误-无法打开front.in\n”);
否则{
getChar();
做{
lex();
}while(nextToken!=EOF);
}
}
/*****************************************************/
/*查找-查找运算符和括号并返回标记的函数*/
int查找(char-ch){
开关(ch){
格“(”:
addChar();
nextToken=左对齐;
打破
案例“)”:
addChar();
nextToken=右对齐;
打破
格“+”:
addChar();
nextToken=添加操作;
打破
案例'-':
addChar();
nextToken=子操作;
打破
案例“*”:
addChar();
nextToken=多个操作;
打破
案例“/”:
addChar();
nextToken=分区操作;
打破
违约:
addChar();
nextToken=EOF;
打破
}
下一步返回;
}
/*****************************************************/
/*addChar-将nextChar添加到lexeme的函数*/
void addChar(){

如果(lexLen您可以用Java整数常量替换那些
#define

// before:
#define LETTER 0
// after:
public static final int LETTER = 0;

将它们转换为初始化变量?
enum
?我不熟悉Java,但在D中就是这样做的。