首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >java递归下降分析_JAVA中的递归下降

java递归下降分析_JAVA中的递归下降

作者头像
全栈程序员站长
发布2022-07-04 13:26:37
发布2022-07-04 13:26:37
1.6K0
举报

大家好,又见面了,我是你们的朋友全栈君。

public class Parser {

// These are the token type

final int NONE = 0;

final int DELIMITER = 1;

final int VARIABLE = 2;

final int NUMBER = 3;

// These are the type of syntax errors

final int SYNTAX = 0;

final int UNBALPARENS = 1;

final int NOEXP = 2;

final int DIVBYZERO = 3;

// These token indicates end-of-expression

final String EOF = “\0”;

private String exp;

private int expIdx;

private String token;

private int tokType;

public double evaluate(String expstr) throws ParserException {

double result;

exp = expstr;

expIdx = 0;

getToken();

// no expression present

if (token.equals(EOF))

handleErr(NOEXP);

result = evalExp2();

if (!token.equals(EOF)) {

handleErr(SYNTAX);

}

return result;

}

private double evalExp2() throws ParserException {

char op;

double result;

double partialResult;

result = evalExp3();

while ((op = token.charAt(0)) == ‘+’ || op == ‘-‘) {

getToken();

partialResult = evalExp3();

switch (op) {

case ‘-‘:

result = result – partialResult;

break;

case ‘+’:

result = result + partialResult;

break;

}

}

return result;

}

private double evalExp3() throws ParserException {

char op;

double result;

double partialResult;

result = evalExp4();

while ((op = token.charAt(0)) == ‘/’ || op == ‘*’ || op == ‘%’) {

getToken();

partialResult = evalExp4();

switch (op) {

case ‘*’:

result = result * partialResult;

break;

case ‘/’:

if (partialResult == 0.0)

handleErr(DIVBYZERO);

result = result / partialResult;

break;

case ‘%’:

if (partialResult == 0.0)

handleErr(DIVBYZERO);

result = result / partialResult;

break;

}

}

return result;

}

private double evalExp4() throws ParserException {

double result;

double partialResult;

double ex;

int t;

result = evalExp5();

if (token.equals(“^”)) {

getToken();

partialResult = evalExp4();

ex = result;

if (partialResult == 0.0) {

result = 1.0;

} else

for (t = (int) partialResult – 1; t > 0; t–)

result = result * ex;

}

return result;

}

private double evalExp5() throws ParserException {

double result;

String op;

op = “”;

if ((tokType == DELIMITER) && token.equals(“+”) || token.equals(“-“)) {

op = token;

getToken();

}

result = evalExp6();

if (op.equals(“-“))

result = -result;

return result;

}

private double evalExp6()throws ParserException{

double result;

if(token.equals(“(“)){

getToken();

result = evalExp2();

if(!token.equals(“)”))

handleErr(UNBALPARENS);

getToken();

}

else result = atom();

return result;

}

private double atom() throws ParserException {

double result = 0.0;

switch (tokType) {

case NUMBER:

try {

result = Double.parseDouble(token);

} catch (NumberFormatException exc) {

handleErr(SYNTAX);

}

getToken();

break;

default:

handleErr(SYNTAX);

break;

}

return result;

}

private void handleErr(int error) throws ParserException {

String[] err = { “Syntax error”, “Unbalanced parentheses”,

“No expression Present”, “Division by zero” };

throw new ParserException(err[error]);

}

//下面的函数就是获得独立元素的值,并且用tokType来保存类型

private void getToken() {

// obtain the next token

tokType = NONE;

token = “”;

if (expIdx == exp.length()) {

token = EOF;

return;

}

while (expIdx < exp.length()

&& Character.isWhitespace(exp.charAt(expIdx)))

++expIdx;

if (expIdx == exp.length()) {

token = EOF;

return;

}

if (isDelim(exp.charAt(expIdx))) {// is operator

token += exp.charAt(expIdx);

expIdx++;

tokType = DELIMITER;

} else if (Character.isLetter(exp.charAt(expIdx))) {

while (!isDelim(exp.charAt(expIdx))) {

token += exp.charAt(expIdx);

expIdx++;

if (expIdx >= exp.length())

break;

}

tokType = VARIABLE;

} else if (Character.isDigit(exp.charAt(expIdx))) {

while (!isDelim(exp.charAt(expIdx))) {

token += exp.charAt(expIdx);

expIdx++;

if (expIdx >= exp.length())

break;

}

tokType = NUMBER;

} else {

token = EOF;

return;

}

}

private boolean isDelim(char c) {

if ((“+-*/%^=()”.indexOf(c)) != -1)

return true;

return false;

}

}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/149323.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档