use of priv.bajdcc.LALR1.syntax.token.OperatorType in project jMiniLang by bajdcc.
the class TestOperator method main.
public static void main(String[] args) {
// System.out.println("Z -> `a`<,> | B | [`a` `b` Z B]");
try {
// Scanner scanner = new Scanner(System.in);
// String expr = "( i )";
// String expr = "((3))+4*5+66/(3-8)";
// String expr = "(4 * 7 - 13) * (3 + 18 / 3 - 3)";
String expr = "(5 + 5)";
Semantic semantic = new Semantic(expr);
semantic.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
semantic.addTerminal("MINUS", TokenType.OPERATOR, OperatorType.MINUS);
semantic.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
semantic.addTerminal("DIVIDE", TokenType.OPERATOR, OperatorType.DIVIDE);
semantic.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
semantic.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
semantic.addTerminal("INTEGER", TokenType.INTEGER, null);
semantic.addNonTerminal("Z");
semantic.addNonTerminal("E");
semantic.addNonTerminal("T");
semantic.addNonTerminal("F");
semantic.addActionHandler("enter_paran", (indexed, manage, access, recorder) -> System.out.println("enter"));
semantic.addActionHandler("leave_paran", (indexed, manage, access, recorder) -> System.out.println("leave"));
semantic.addErrorHandler("lost_exp", (iterator, bag) -> {
bag.bRead = false;
bag.bPass = true;
return "表达式不完整";
});
semantic.addErrorHandler("lost_exp_right", (iterator, bag) -> {
bag.bRead = false;
bag.bPass = true;
return "缺少右括号";
});
ISemanticAnalyzer handleCopy = (indexed, query, recorder) -> indexed.get(0).object;
ISemanticAnalyzer handleBinop = (indexed, query, recorder) -> {
int lop = Integer.parseInt(indexed.get(0).object.toString());
int rop = Integer.parseInt(indexed.get(2).object.toString());
Token op = indexed.get(1).token;
if (op.kToken == TokenType.OPERATOR) {
OperatorType kop = (OperatorType) op.object;
switch(kop) {
case PLUS:
return lop + rop;
case MINUS:
return lop - rop;
case TIMES:
return lop * rop;
case DIVIDE:
if (rop == 0) {
return lop;
} else {
return lop / rop;
}
default:
return 0;
}
} else {
return 0;
}
};
ISemanticAnalyzer handleValue = (indexed, query, recorder) -> indexed.get(0).token.object;
// syntax.infer("E -> T `PLUS`<+> E | T `MINUS`<-> E | T");
// syntax.infer("T -> F `TIMES`<*> T | F `DIVIDE`</> T | F");
// syntax.infer("F -> `LPA`<(> E `RPA`<)> | `SYMBOL`<i>");
semantic.infer(handleCopy, "Z -> E[0]");
semantic.infer(handleCopy, "E -> T[0]");
semantic.infer(handleBinop, "E -> E[0] ( @PLUS[1]<+> | @MINUS[1]<-> ) T[2]{lost_exp}");
semantic.infer(handleCopy, "T -> F[0]");
semantic.infer(handleBinop, "T -> T[0] ( @TIMES[1]<*> | @DIVIDE[1]</> ) F[2]{lost_exp}");
semantic.infer(handleValue, "F -> @INTEGER[0]<integer>");
semantic.infer(handleCopy, "F -> @LPA#enter_paran#<(> E[0]{lost_exp} @RPA#leave_paran#{lost_exp_right}<)>");
semantic.initialize("Z");
// System.out.println(semantic.toString());
// System.out.println(semantic.getNGAString());
// System.out.println(semantic.getNPAString());
// System.out.println(semantic.getInst());
System.out.println(semantic.getTrackerError());
System.out.println(semantic.getTokenList());
System.out.println(semantic.getObject());
// scanner.close();
} catch (RegexException e) {
System.err.println(e.getPosition() + "," + e.getMessage());
e.printStackTrace();
} catch (SyntaxException e) {
System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
e.printStackTrace();
}
}
use of priv.bajdcc.LALR1.syntax.token.OperatorType in project jMiniLang by bajdcc.
the class Syntax method doAnalysis.
/**
* 分析表达式
*
* @param type
* 结束类型
* @param obj
* 结束数据
* @return 表达式树根结点
* @throws SyntaxException 词法错误
*/
private ISyntaxComponent doAnalysis(TokenType type, Object obj) throws SyntaxException {
/* 新建序列用于存放结果 */
SequenceExp sequence = new SequenceExp();
/* 可能会使用的分支 */
BranchExp branch = null;
/* 添加子结点接口 */
IExpCollction collection = sequence;
/* 表达式通用接口 */
ISyntaxComponent result = sequence;
for (; ; ) {
if ((token.kToken == type && (token.object == null || token.object.equals(obj)))) {
// 结束字符
if (syntaxLexer.index() == 0) {
// 表达式为空
err(SyntaxError.NULL);
} else if (collection.isEmpty()) {
// 部件为空
err(SyntaxError.INCOMPLETE);
} else {
next();
// 正常终止
break;
}
} else if (token.kToken == TokenType.EOF) {
err(SyntaxError.INCOMPLETE);
}
// 当前待赋值的表达式
ISyntaxComponent exp = null;
switch(token.kToken) {
case OPERATOR:
OperatorType op = (OperatorType) token.object;
next();
switch(op) {
case ALTERNATIVE:
if (// 在此之前没有存储表达式 (|...)
collection.isEmpty()) {
err(SyntaxError.INCOMPLETE);
} else {
if (branch == null) {
// 分支为空,则建立分支
branch = new BranchExp();
// 用新建的分支包含并替代当前序列
branch.add(sequence);
result = branch;
}
// 新建一个序列
sequence = new SequenceExp();
branch.add(sequence);
continue;
}
break;
case // '('
LPARAN:
// 递归分析
exp = doAnalysis(TokenType.OPERATOR, OperatorType.RPARAN);
break;
case // '['
LSQUARE:
// 递归分析
exp = doAnalysis(TokenType.OPERATOR, OperatorType.RSQUARE);
// 包装
OptionExp option = new OptionExp();
option.expression = exp;
exp = option;
break;
default:
err(SyntaxError.SYNTAX);
break;
}
break;
case EOF:
return result;
case TERMINAL:
exp = matchTerminal();
next();
PropertyExp property1 = matchStorage(exp, token.object);
exp = property1;
if (token.kToken == TokenType.ACTION) {
property1.actionHandler = matchAction();
next();
}
if (token.kToken == TokenType.HANDLER) {
property1.errorHandler = matchHandler();
next();
}
break;
case NONTERMINAL:
exp = matchNonTerminal();
next();
PropertyExp property2 = matchStorage(exp, token.object);
exp = property2;
if (token.kToken == TokenType.ACTION) {
property2.actionHandler = matchAction();
next();
}
if (token.kToken == TokenType.HANDLER) {
property2.errorHandler = matchHandler();
next();
}
break;
default:
err(SyntaxError.SYNTAX);
break;
}
if (exp != null) {
sequence.add(exp);
}
}
return result;
}
Aggregations