Search in sources :

Example 16 with SyntaxException

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

the class TestSyntax2 method main.

public static void main(String[] args) {
    try {
        // Scanner scanner = new Scanner(System.in);
        Syntax syntax = new Syntax();
        syntax.addTerminal("a", TokenType.ID, "a");
        syntax.addTerminal("b", TokenType.ID, "b");
        syntax.addNonTerminal("Z");
        syntax.addNonTerminal("S");
        syntax.addNonTerminal("B");
        syntax.addErrorHandler("sample", null);
        syntax.infer("Z -> S");
        syntax.infer("S -> B B");
        syntax.infer("B -> @a B");
        syntax.infer("B -> @b");
        syntax.initialize("Z");
        System.out.println(syntax.toString());
        System.out.println(syntax.getNGAString());
        System.out.println(syntax.getNPAString());
    // 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.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Syntax(priv.bajdcc.LALR1.syntax.Syntax)

Example 17 with SyntaxException

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

the class TestSyntax3 method main.

public static void main(String[] args) {
    try {
        // Scanner scanner = new Scanner(System.in);
        Syntax syntax = new Syntax();
        syntax.addTerminal("a", TokenType.ID, "a");
        syntax.addTerminal("c", TokenType.ID, "c");
        syntax.addTerminal("d", TokenType.ID, "d");
        syntax.addNonTerminal("S");
        syntax.addNonTerminal("A");
        syntax.addErrorHandler("sample", null);
        syntax.infer("S -> @c A @d");
        syntax.infer("A -> @a");
        syntax.infer("A -> A @a");
        syntax.initialize("S");
        System.out.println(syntax.toString());
        System.out.println(syntax.getNGAString());
        System.out.println(syntax.getNPAString());
    // 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.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Syntax(priv.bajdcc.LALR1.syntax.Syntax)

Example 18 with SyntaxException

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

the class UIMainFrame method startOS.

private void startOS(UIGraphics g) {
    IOSCodePage[] pages = new IOSCodePage[] { // OS
    new OSEntry(), new OSIrq(), new OSTask(), // IRQ
    new IRPrint(), new IRRemote(), new IRTask(), new IRSignal(), // TASK
    new TKSystem(), new TKUtil(), new TKUI(), new TKNet(), // UI
    new UIMain(), new UIClock(), new UIHitokoto(), new UIMonitor(), // USER
    new UserMain(), // USER ROUTINE
    new URShell(), new UREcho(), new URPipe(), new URDup(), new URGrep(), new URRange(), new URProc(), new URTask(), new URSleep(), new URTime(), new URCount(), new URTest(), new URMsg(), new URNews(), new URBash(), new URReplace(), new URUtil(), new URAI(), // USER FILE
    new URFileLoad(), new URFileSave(), new URFileAppend() };
    try {
        String code = "import \"sys.base\";\n" + "import \"sys.proc\";\n" + "call g_load_sync_x(\"/kern/entry\");\n";
        interpreter = new Interpreter();
        for (IOSCodePage page : pages) {
            interpreter.load(page);
        }
        Grammar grammar = new Grammar(code);
        // System.out.println(grammar.toString());
        RuntimeCodePage page = grammar.getCodePage();
        // System.out.println(page.toString());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        RuntimeCodePage.exportFromStream(page, baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        interpreter.run("@main", bais);
    } catch (RegexException e) {
        System.err.println();
        System.err.println(e.getPosition() + "," + e.getMessage());
        e.printStackTrace();
    } catch (SyntaxException e) {
        System.err.println();
        System.err.println(String.format("模块名:%s. 位置:%s. 错误:%s-%s(%s:%d)", e.getPageName(), e.getPosition(), e.getMessage(), e.getInfo(), e.getFileName(), e.getPosition().iLine + 1));
        e.printStackTrace();
    } catch (RuntimeException e) {
        System.err.println();
        System.err.println(e.getkError().getMessage() + " " + e.getPosition() + ": " + e.getInfo());
        e.printStackTrace();
    } catch (Exception e) {
        System.err.println();
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
}
Also used : OSTask(priv.bajdcc.LALR1.interpret.os.kern.OSTask) IOSCodePage(priv.bajdcc.LALR1.interpret.os.IOSCodePage) URFileSave(priv.bajdcc.LALR1.interpret.os.user.routine.file.URFileSave) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) UIHitokoto(priv.bajdcc.LALR1.interpret.os.ui.UIHitokoto) UserMain(priv.bajdcc.LALR1.interpret.os.user.UserMain) URFileAppend(priv.bajdcc.LALR1.interpret.os.user.routine.file.URFileAppend) IRPrint(priv.bajdcc.LALR1.interpret.os.irq.IRPrint) ByteArrayInputStream(java.io.ByteArrayInputStream) TKUtil(priv.bajdcc.LALR1.interpret.os.task.TKUtil) UIClock(priv.bajdcc.LALR1.interpret.os.ui.UIClock) IRSignal(priv.bajdcc.LALR1.interpret.os.irq.IRSignal) OSEntry(priv.bajdcc.LALR1.interpret.os.kern.OSEntry) TKSystem(priv.bajdcc.LALR1.interpret.os.task.TKSystem) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) UIMain(priv.bajdcc.LALR1.interpret.os.ui.UIMain) Interpreter(priv.bajdcc.LALR1.interpret.Interpreter) OSIrq(priv.bajdcc.LALR1.interpret.os.kern.OSIrq) TKNet(priv.bajdcc.LALR1.interpret.os.task.TKNet) IRRemote(priv.bajdcc.LALR1.interpret.os.irq.IRRemote) Grammar(priv.bajdcc.LALR1.grammar.Grammar) RuntimeCodePage(priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TKUI(priv.bajdcc.LALR1.interpret.os.task.TKUI) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RegexException(priv.bajdcc.util.lexer.error.RegexException) UIMonitor(priv.bajdcc.LALR1.interpret.os.ui.UIMonitor) IRTask(priv.bajdcc.LALR1.interpret.os.irq.IRTask) URFileLoad(priv.bajdcc.LALR1.interpret.os.user.routine.file.URFileLoad) RegexException(priv.bajdcc.util.lexer.error.RegexException)

Example 19 with SyntaxException

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

the class Syntax method addNonTerminal.

/**
 * 添加非终结符
 *
 * @param name
 *            非终结符名称
 * @throws SyntaxException 词法错误
 */
public void addNonTerminal(String name) throws SyntaxException {
    RuleExp exp = new RuleExp(arrNonTerminals.size(), name);
    if (!mapNonTerminals.containsKey(name)) {
        mapNonTerminals.put(name, exp);
        arrNonTerminals.add(exp);
    } else {
        err(SyntaxError.REDECLARATION);
    }
}
Also used : RuleExp(priv.bajdcc.LL1.syntax.exp.RuleExp)

Example 20 with SyntaxException

use of priv.bajdcc.LL1.syntax.handler.SyntaxException 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;
                    default:
                        err(SyntaxError.SYNTAX);
                        break;
                }
                break;
            case EOF:
                return result;
            case TERMINAL:
                exp = matchTerminal();
                next();
                break;
            case NONTERMINAL:
                exp = matchNonTerminal();
                next();
                break;
            default:
                err(SyntaxError.SYNTAX);
                break;
        }
        if (exp != null) {
            sequence.add(exp);
        }
    }
    return result;
}
Also used : IExpCollction(priv.bajdcc.LL1.syntax.exp.IExpCollction) BranchExp(priv.bajdcc.LL1.syntax.exp.BranchExp) SequenceExp(priv.bajdcc.LL1.syntax.exp.SequenceExp) OperatorType(priv.bajdcc.LL1.syntax.token.OperatorType)

Aggregations

RegexException (priv.bajdcc.util.lexer.error.RegexException)27 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)26 Grammar (priv.bajdcc.LALR1.grammar.Grammar)17 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 Syntax (priv.bajdcc.LALR1.syntax.Syntax)3 RuntimeMachine (priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine)2 ISemanticAnalyzer (priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer)2 Grammar (priv.bajdcc.LL1.grammar.Grammar)2 GrammarException (priv.bajdcc.LL1.grammar.error.GrammarException)2 TokenExp (priv.bajdcc.LL1.syntax.exp.TokenExp)2 SyntaxException (priv.bajdcc.LL1.syntax.handler.SyntaxException)2 RuleItem (priv.bajdcc.LL1.syntax.rule.RuleItem)2 TokenType (priv.bajdcc.util.lexer.token.TokenType)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1