Search in sources :

Example 21 with SyntaxException

use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.

the class TestSyntax 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);
        Syntax syntax = new Syntax();
        syntax.addTerminal("SYMBOL", TokenType.ID, "i");
        syntax.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
        syntax.addTerminal("MINUS", TokenType.OPERATOR, OperatorType.MINUS);
        syntax.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
        syntax.addTerminal("DIVIDE", TokenType.OPERATOR, OperatorType.DIVIDE);
        syntax.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
        syntax.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
        syntax.setEpsilonName("epsilon");
        String[] nons = new String[] { "E", "E1", "T", "T1", "F", "A", "M" };
        for (String non : nons) {
            syntax.addNonTerminal(non);
        }
        syntax.infer("E -> T E1");
        syntax.infer("E1 -> A T E1 | @epsilon");
        syntax.infer("T -> F T1");
        syntax.infer("T1 -> M F T1 | @epsilon");
        syntax.infer("F -> @LPA E @RPA | @SYMBOL");
        syntax.infer("A -> @PLUS | @MINUS");
        syntax.infer("M -> @TIMES | @DIVIDE");
        syntax.initialize("E");
        System.out.println(syntax.toString());
    // 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();
    }
}
Also used : SyntaxException(priv.bajdcc.LL1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Syntax(priv.bajdcc.LL1.syntax.Syntax)

Example 22 with SyntaxException

use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.

the class TestGrammar 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);
        // Grammar grammar = new Grammar("(i * i) * (i + i) - i");
        Grammar grammar = new Grammar("i + i * i");
        grammar.addTerminal("SYMBOL", TokenType.ID, "i");
        grammar.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
        grammar.addTerminal("MINUS", TokenType.OPERATOR, OperatorType.MINUS);
        grammar.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
        grammar.addTerminal("DIVIDE", TokenType.OPERATOR, OperatorType.DIVIDE);
        grammar.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
        grammar.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
        grammar.setEpsilonName("epsilon");
        String[] nons = new String[] { "E", "E1", "T", "T1", "F", "A", "M" };
        for (String non : nons) {
            grammar.addNonTerminal(non);
        }
        grammar.infer("E -> T E1");
        grammar.infer("E1 -> A T E1 | @epsilon");
        grammar.infer("T -> F T1");
        grammar.infer("T1 -> M F T1 | @epsilon");
        grammar.infer("F -> @LPA E @RPA | @SYMBOL");
        grammar.infer("A -> @PLUS | @MINUS");
        grammar.infer("M -> @TIMES | @DIVIDE");
        grammar.initialize("E");
        System.out.println(grammar.toString());
        System.out.println(grammar.getPredictionString());
        grammar.run();
        System.out.println(grammar.getTokenString());
    // 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();
    } catch (GrammarException e) {
        System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
        e.printStackTrace();
    }
}
Also used : SyntaxException(priv.bajdcc.LL1.syntax.handler.SyntaxException) GrammarException(priv.bajdcc.LL1.grammar.error.GrammarException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Grammar(priv.bajdcc.LL1.grammar.Grammar)

Example 23 with SyntaxException

use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.

the class TestGrammar2 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);
        Grammar syntax = new Grammar("b b b b ");
        syntax.addTerminal("a", TokenType.ID, "a");
        syntax.addTerminal("b", TokenType.ID, "b");
        syntax.setEpsilonName("epsilon");
        String[] nons = new String[] { "S", "A", "B" };
        for (String non : nons) {
            syntax.addNonTerminal(non);
        }
        syntax.infer("S -> A B A B");
        syntax.infer("A -> @a A | @epsilon");
        syntax.infer("B -> @b B | @epsilon");
        syntax.initialize("S");
        System.out.println(syntax.toString());
        System.out.println(syntax.getPredictionString());
        syntax.run();
        System.out.println(syntax.getTokenString());
    // 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();
    } catch (GrammarException e) {
        System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
        e.printStackTrace();
    }
}
Also used : SyntaxException(priv.bajdcc.LL1.syntax.handler.SyntaxException) GrammarException(priv.bajdcc.LL1.grammar.error.GrammarException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Grammar(priv.bajdcc.LL1.grammar.Grammar)

Example 24 with SyntaxException

use of priv.bajdcc.LALR1.syntax.handler.SyntaxException 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");
}
Also used : ISemanticAnalyzer(priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer)

Example 25 with SyntaxException

use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.

the class Grammar method declareErrorHandler.

/**
 * 声明错误处理器
 *
 * @throws SyntaxException 词法错误
 */
private void declareErrorHandler() throws SyntaxException {
    addErrorHandler("lost_exp", new LostHandler("表达式"));
    addErrorHandler("lost_func", new LostHandler("函数"));
    addErrorHandler("lost_token", new LostHandler("标识符"));
    addErrorHandler("lost_func_name", new LostHandler("函数名"));
    addErrorHandler("lost_func_body", new LostHandler("函数体"));
    addErrorHandler("lost_block", new LostHandler("块"));
    addErrorHandler("lost_stmt", new LostHandler("语句"));
    addErrorHandler("lost_string", new LostHandler("字符串"));
    addErrorHandler("lost_assign", new LostHandler("等号'='"));
    addErrorHandler("lost_call", new LostHandler("调用主体"));
    addErrorHandler("lost_lpa", new LostHandler("左圆括号'('"));
    addErrorHandler("lost_rpa", new LostHandler("右圆括号')'"));
    addErrorHandler("lost_lsq", new LostHandler("左方括号'['"));
    addErrorHandler("lost_rsq", new LostHandler("右方括号']'"));
    addErrorHandler("lost_lbr", new LostHandler("左花括号'{'"));
    addErrorHandler("lost_rbr", new LostHandler("右花括号'}'"));
    addErrorHandler("lost_colon", new LostHandler("冒号':'"));
    addErrorHandler("lost_semi", new LostHandler("分号';'"));
    addErrorHandler("lost_doc", new LostHandler("文档"));
    addErrorHandler("lost_var", new LostHandler("赋值"));
    addErrorHandler("lost_array", new LostHandler("数组'[]'"));
    addErrorHandler("lost_map", new LostHandler("字典'{}'"));
    addErrorHandler("lost_dot", new LostHandler("点号'.'"));
    addErrorHandler("lost_property", new LostHandler("属性连接符'::'"));
    addErrorHandler("lost_try", new LostHandler("属性连接符'::'"));
    addErrorHandler("lost_catch", new LostHandler("'catch'"));
}
Also used : LostHandler(priv.bajdcc.LALR1.grammar.error.LostHandler)

Aggregations

RegexException (priv.bajdcc.util.lexer.error.RegexException)28 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)26 Grammar (priv.bajdcc.LALR1.grammar.Grammar)18 RuntimeCodePage (priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage)15 RuntimeException (priv.bajdcc.LALR1.grammar.runtime.RuntimeException)15 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 Interpreter (priv.bajdcc.LALR1.interpret.Interpreter)13 Semantic (priv.bajdcc.LALR1.semantic.Semantic)5 ISemanticAnalyzer (priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer)3 Syntax (priv.bajdcc.LALR1.syntax.Syntax)3 RuleItem (priv.bajdcc.LALR1.syntax.rule.RuleItem)3 RuntimeMachine (priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine)2 PropertyExp (priv.bajdcc.LALR1.syntax.exp.PropertyExp)2 Grammar (priv.bajdcc.LL1.grammar.Grammar)2 GrammarException (priv.bajdcc.LL1.grammar.error.GrammarException)2 SyntaxException (priv.bajdcc.LL1.syntax.handler.SyntaxException)2 OperatorType (priv.bajdcc.util.lexer.token.OperatorType)2 Token (priv.bajdcc.util.lexer.token.Token)2 TokenType (priv.bajdcc.util.lexer.token.TokenType)2