use of priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer in project jMiniLang by bajdcc.
the class Grammar method infer.
/**
* 进行推导
*
* @throws SyntaxException 词法错误
*/
private void infer() throws SyntaxException {
/* 起始符号就是main函数 */
infer(handler.getSemanticHandler("main"), "program -> stmt_list[0]{lost_stmt}");
/* Block语句 */
infer(handler.getSemanticHandler("block"), "block -> @LBR#do_enter_scope# [stmt_list[0]] @RBR#do_leave_scope#{lost_rbr}");
infer(handler.getSemanticHandler("block_stmt"), "block_stmt -> block[0]");
/* 当前块(Block)全部由语句组成 */
infer(handler.getSemanticHandler("stmt_list"), "stmt_list -> stmt[0]{lost_stmt} [stmt_list[1]]");
/* 语句分为变量定义(赋值)、调用语句 */
infer(handler.getSemanticHandler("copy"), "stmt_exp -> var[0] | call[0] | cycle_ctrl[0] | exp[0]");
infer(handler.getSemanticHandler("copy"), "stmt -> stmt_stmt[0] | stmt_ctrl[0] | block_stmt[0]");
infer(handler.getSemanticHandler("stmt_exp"), "stmt_stmt -> [stmt_exp[0]] @SEMI{lost_semi}");
infer(handler.getSemanticHandler("copy"), "stmt_ctrl -> ret[0] | port[0] | if[0] | for[0] | foreach[0] | while[0] | try[0] | throw[0]");
/* 返回语句 */
infer(handler.getSemanticHandler("return"), "ret -> (@YIELD[1] | @RETURN) [exp[0]] @SEMI{lost_semi}");
/* 变量定义(赋值)语句(由于支持Lambda,函数定义皆为Lambda形式) */
infer(handler.getSemanticHandler("var"), "var -> (@VARIABLE[11] | @LET[12]) @ID[0]#declear_variable#{lost_token} @ASSIGN{lost_assign} (func[1]{lost_func} | exp[2]{lost_exp})");
/* 类属性赋值语句 */
infer(handler.getSemanticHandler("set"), "set -> @SET exp[3]{lost_exp} @PROPERTY{lost_property} exp[4]{lost_exp} @ASSIGN{lost_assign} exp[2]{lost_exp}");
/* 类方法调用语句 */
infer(handler.getSemanticHandler("invoke"), "invoke -> @INVOKE[0] exp[1]{lost_exp} @PROPERTY{lost_property} exp[2]{lost_exp} [@PROPERTY] @LPA{lost_lpa} [exp_list[3]] @RPA{lost_rpa}");
/* 导入与导出语句 */
infer(handler.getSemanticHandler("port"), "port -> (@IMPORT[1] | @EXPORT[2]) @LITERAL[0]{lost_string} @SEMI{lost_semi}");
/* 表达式(算符文法) */
ISemanticAnalyzer exp_handler = handler.getSemanticHandler("exp");
infer(exp_handler, "exp -> exp01[0]");
infer(exp_handler, "exp01 -> [exp01[1] (@EQ_ASSIGN[2] | @ADD_ASSIGN[2] | @SUB_ASSIGN[2] | @MUL_ASSIGN[2] | @DIV_ASSIGN[2] | @AND_ASSIGN[2] | @OR_ASSIGN[2] | @XOR_ASSIGN[2] | @MOD_ASSIGN[2])] exp0[0]");
infer(exp_handler, "exp0 -> exp1[0] [@QUERY[4] exp0[6] @COLON[5]{lost_colon} exp0[7]]");
infer(exp_handler, "exp1 -> [exp1[1] (@AND_OP[2] | @OR_OP[2])] exp2[0]");
infer(exp_handler, "exp2 -> [exp2[1] (@OR[2] | @XOR[2] | @AND[2])] exp3[0]");
infer(exp_handler, "exp3 -> [exp3[1] (@EQ_OP[2] | @NE_OP[2])] exp4[0]");
infer(exp_handler, "exp4 -> [exp4[1] (@LT[2] | @GT[2] | @LE_OP[2] | @GE_OP[2])] exp5[0]");
infer(exp_handler, "exp5 -> [exp5[1] (@LEFT_OP[2] | @RIGHT_OP[2])] exp6[0]");
infer(exp_handler, "exp6 -> [exp6[1] (@ADD[2] | @SUB[2])] exp7[0]");
infer(exp_handler, "exp7 -> [exp7[1] (@MUL[2] | @DIV[2] | @MOD[2])] exp8[0]");
infer(exp_handler, "exp8 -> (@NOT_OP[3] | @NOT[3]) exp8[1] | exp9[0]");
infer(exp_handler, "exp9 -> (@INC_OP[3] | @DEC_OP[3]) exp9[1] | exp9[1] (@INC_OP[3] | @DEC_OP[3]) | exp10[0]");
infer(exp_handler, "exp10 -> [exp10[1] @DOT[2]] type[0]");
/* 调用语句 */
infer(handler.getSemanticHandler("call_exp"), "call -> @CALL (@LPA{lost_lpa} func[0]{lost_call} @RPA{lost_rpa} | @ID[1]{lost_call}) @LPA{lost_lpa} [exp_list[2]] @RPA{lost_rpa}");
/* 函数定义 */
infer(handler.getSemanticHandler("token_list"), "var_list -> @ID[0]#declear_param# [@COMMA var_list[1]]");
infer(handler.getSemanticHandler("exp_list"), "exp_list -> exp[0] [@COMMA exp_list[1]]");
infer(handler.getSemanticHandler("token_list"), "doc_list -> @LITERAL[0] [@COMMA doc_list[1]]");
/* 函数主体 */
infer(handler.getSemanticHandler("func"), "func -> (@FUNCTION[10]#func_clearargs# | @YIELD#func_clearargs#) [@LSQ doc_list[0]{lost_doc} @RSQ] (@ID[1]#predeclear_funcname#{lost_func_name} | @NOT[1]#predeclear_funcname#{lost_func_name}) @LPA{lost_lpa} [var_list[2]] @RPA{lost_rpa} (@PTR_OP#do_enter_scope#{lost_func_body} exp[3]#do_leave_scope#{lost_exp} | block[4]{lost_func_body})");
/* 匿名主体 */
infer(handler.getSemanticHandler("lambda"), "lambda -> @LAMBDA[1]#lambda# @LPA{lost_lpa} [var_list[2]] @RPA{lost_rpa} (@PTR_OP{lost_func_body} exp[3]{lost_exp} | block[4]{lost_func_body})");
/* 基本数据类型 */
infer(handler.getSemanticHandler("type"), "type -> @ID[0] [(@LPA[3] [exp_list[4]] @RPA{lost_rpa}) | (@LSQ exp[5]{lost_exp} @RSQ{lost_rpa})] | @INTEGER[0] | @DECIMAL[0] | @LITERAL[0] | @CHARACTER[0] | @BOOLEAN[0] | @LPA exp[1]{lost_exp} @RPA{lost_rpa} | call[1] | lambda[2] | set[1] | invoke[1] | array[1] | map[1]");
/* 条件语句 */
infer(handler.getSemanticHandler("if"), "if -> @IF @LPA{lost_lpa} exp[0]{lost_exp} @RPA{lost_rpa} block[1]{lost_block} [@ELSE (block[2]{lost_block} | if[3]{lost_block})]");
/* 循环语句 */
infer(handler.getSemanticHandler("for"), "for -> @FOR#do_enter_cycle# @LPA{lost_lpa} [var[0]] @SEMI{lost_semi} [exp[1]] @SEMI{lost_semi} [exp[2] | var[2]] @RPA{lost_rpa} block[3]{lost_block}");
infer(handler.getSemanticHandler("while"), "while -> @WHILE#do_enter_cycle# @LPA{lost_lpa} exp[0] @RPA{lost_rpa} block[1]{lost_block}");
/* 循环语句 */
infer(handler.getSemanticHandler("foreach"), "foreach -> @FOREACH#do_enter_cycle# @LPA{lost_lpa} @VARIABLE{lost_var} @ID[0]#declear_variable#{lost_token} @COLON{lost_colon} exp[1]{lost_exp} @RPA{lost_rpa} block[2]{lost_block}");
/* 循环控制语句 */
infer(handler.getSemanticHandler("cycle"), "cycle_ctrl -> @BREAK[0] | @CONTINUE[0]");
/* 数组初始化 */
infer(handler.getSemanticHandler("array"), "array -> @LSQ [exp_list[0]] @RSQ{lost_rsq}");
/* 字典初始化 */
infer(handler.getSemanticHandler("map_list"), "map_list -> @LITERAL[1] @COLON[3]{lost_colon} exp[2]{lost_exp} [@COMMA map_list[0]]");
infer(handler.getSemanticHandler("map"), "map -> @LBR [map_list[0]] @RBR{lost_rbr}");
/* 异常处理 */
infer(handler.getSemanticHandler("try"), "try -> @TRY block[1]{lost_block} @CATCH{lost_catch} [@LPA{lost_lpa} @ID[0]#declear_param#{lost_token} @RPA{lost_rpa}] block[2]#clear_catch#{lost_block}");
infer(handler.getSemanticHandler("throw"), "throw -> @THROW exp[0]{lost_exp} @SEMI{lost_semi}");
initialize("program");
}
use of priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer 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.semantic.token.ISemanticAnalyzer in project jMiniLang by bajdcc.
the class TestSemantic3 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 = "a a a a";
Semantic semantic = new Semantic(expr);
semantic.addTerminal("a", TokenType.ID, "a");
semantic.addNonTerminal("START");
semantic.addNonTerminal("Z");
semantic.addErrorHandler("sample", null);
ISemanticAnalyzer handleValue = (indexed, query, recorder) -> 1;
ISemanticAnalyzer handleCopy = (indexed, query, recorder) -> indexed.get(0).object;
ISemanticAnalyzer handleRec = (indexed, query, recorder) -> {
int lop = Integer.parseInt(indexed.get(0).object.toString());
return lop + 1;
};
// 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, "START -> Z[0]");
semantic.infer(handleRec, "Z -> Z[0] @a[1]");
semantic.infer(handleValue, "Z -> @a[0]");
semantic.initialize("START");
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();
}
}
Aggregations