Search in sources :

Example 26 with SyntaxException

use of priv.bajdcc.LL1.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 27 with SyntaxException

use of priv.bajdcc.LL1.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 28 with SyntaxException

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

the class TestGrammar method main.

public static void main(String[] args) {
    try {
        // String expr = "void main(int argc, char** argv) {return 0;}";
        // BufferedReader br = new BufferedReader(new FileReader("E:/http.c"));
        BufferedReader br = new BufferedReader(new FileReader("E:/a.c"));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line).append(System.lineSeparator());
        }
        br.close();
        Grammar grammar = new Grammar(sb.toString());
        // System.out.println(grammar.getNGAString());
        System.out.println(grammar.getNPAString());
        System.out.println(grammar.getInst());
        System.out.println(grammar.getTrackerError());
        System.out.println(grammar.getTokenList());
        System.out.println(grammar.getObject());
    // FileWriter fw = new FileWriter("E:/testgrammar.txt");
    // fw.append(grammar.toString());
    // fw.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 (IOException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
}
Also used : SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Grammar(priv.bajdcc.LALR1.grammar.Grammar) IOException(java.io.IOException)

Example 29 with SyntaxException

use of priv.bajdcc.LL1.syntax.handler.SyntaxException 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();
    }
}
Also used : ISemanticAnalyzer(priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer) OperatorType(priv.bajdcc.util.lexer.token.OperatorType) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) Token(priv.bajdcc.util.lexer.token.Token) RegexException(priv.bajdcc.util.lexer.error.RegexException) Semantic(priv.bajdcc.LALR1.semantic.Semantic) TokenType(priv.bajdcc.util.lexer.token.TokenType) ISemanticAnalyzer(priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) Semantic(priv.bajdcc.LALR1.semantic.Semantic) RegexException(priv.bajdcc.util.lexer.error.RegexException) Token(priv.bajdcc.util.lexer.token.Token) OperatorType(priv.bajdcc.util.lexer.token.OperatorType)

Example 30 with SyntaxException

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

the class TestSemantic 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 = "((i))+i*i+i/(i-i)";
        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("SYMBOL", TokenType.ID, "i");
        semantic.addNonTerminal("Z");
        semantic.addNonTerminal("E");
        semantic.addNonTerminal("T");
        semantic.addNonTerminal("F");
        semantic.addErrorHandler("sample", null);
        // 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("Z -> E");
        semantic.infer("E -> T");
        semantic.infer("E -> E @PLUS<+> T");
        semantic.infer("E -> E @MINUS<-> T");
        semantic.infer("T -> F");
        semantic.infer("T -> T @TIMES<*> F");
        semantic.infer("T -> T @DIVIDE</> F");
        semantic.infer("F -> @SYMBOL<i>");
        semantic.infer("F -> @LPA<(> E @RPA<)>");
        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());
    // 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) Semantic(priv.bajdcc.LALR1.semantic.Semantic) RegexException(priv.bajdcc.util.lexer.error.RegexException)

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