Search in sources :

Example 26 with RegexException

use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.

the class TestRegex method main.

public static void main(String[] args) {
    try {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        Regex ra = new Regex(str, true);
        String context = scanner.nextLine();
        String match = ra.match(context);
        if (match == null) {
            System.err.println("failed");
        } else {
            System.out.println(match);
        }
        scanner.close();
    } catch (RegexException e) {
        System.err.println(e.getPosition() + "," + e.getMessage());
        e.printStackTrace();
    }
}
Also used : Scanner(java.util.Scanner) Regex(priv.bajdcc.util.lexer.regex.Regex) RegexException(priv.bajdcc.util.lexer.error.RegexException)

Example 27 with RegexException

use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.

the class CharacterFilter method filter.

@Override
public RegexStringIteratorData filter(IRegexStringIterator iterator) {
    // 获取解析组件
    RegexStringUtility utility = iterator.utility();
    RegexStringIteratorData data = new RegexStringIteratorData();
    try {
        if (!iterator.available()) {
            data.kMeta = MetaType.END;
            data.chCurrent = MetaType.END.getChar();
        } else {
            data.kMeta = iterator.meta();
            data.chCurrent = iterator.current();
            iterator.next();
            if (data.kMeta == MetaType.SINGLE_QUOTE) {
                // 过滤单引号
                data.kMeta = MetaType.NULL;
            } else if (data.kMeta == MetaType.ESCAPE) {
                // 处理转义
                data.chCurrent = iterator.current();
                iterator.next();
                data.kMeta = MetaType.MUST_SAVE;
                data.chCurrent = utility.fromEscape(data.chCurrent, RegexError.ESCAPE);
            }
        }
    } catch (RegexException e) {
        System.err.println(e.getPosition() + " : " + e.getMessage());
        data.kMeta = MetaType.ERROR;
        data.chCurrent = MetaType.ERROR.getChar();
    }
    return data;
}
Also used : RegexStringUtility(priv.bajdcc.util.lexer.regex.RegexStringUtility) RegexStringIteratorData(priv.bajdcc.util.lexer.regex.RegexStringIteratorData) RegexException(priv.bajdcc.util.lexer.error.RegexException)

Example 28 with RegexException

use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.

the class TestSyntaxLexer method main.

public static void main(String[] args) {
    try {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        SyntaxLexer lexer = new SyntaxLexer();
        lexer.setContext(str);
        Token token;
        for (; ; ) {
            token = lexer.nextToken();
            if (token.kToken == TokenType.EOF) {
                break;
            }
            System.out.println(token.toString());
        }
        scanner.close();
    } catch (RegexException e) {
        System.err.println(e.getPosition() + "," + e.getMessage());
        e.printStackTrace();
    }
}
Also used : Scanner(java.util.Scanner) SyntaxLexer(priv.bajdcc.LALR1.syntax.lexer.SyntaxLexer) RegexException(priv.bajdcc.util.lexer.error.RegexException) Token(priv.bajdcc.LALR1.syntax.token.Token)

Example 29 with RegexException

use of priv.bajdcc.util.lexer.error.RegexException 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 30 with RegexException

use of priv.bajdcc.util.lexer.error.RegexException 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)

Aggregations

RegexException (priv.bajdcc.util.lexer.error.RegexException)40 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)24 Grammar (priv.bajdcc.LALR1.grammar.Grammar)18 RuntimeCodePage (priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage)16 RuntimeException (priv.bajdcc.LALR1.grammar.runtime.RuntimeException)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 Interpreter (priv.bajdcc.LALR1.interpret.Interpreter)14 Scanner (java.util.Scanner)6 Token (priv.bajdcc.util.lexer.token.Token)5 Semantic (priv.bajdcc.LALR1.semantic.Semantic)4 SyntaxException (priv.bajdcc.OP.syntax.handler.SyntaxException)4 OperatorType (priv.bajdcc.util.lexer.token.OperatorType)4 Syntax (priv.bajdcc.LALR1.syntax.Syntax)3 GrammarException (priv.bajdcc.OP.grammar.error.GrammarException)3 IPatternHandler (priv.bajdcc.OP.grammar.handler.IPatternHandler)3 RegexStringIteratorData (priv.bajdcc.util.lexer.regex.RegexStringIteratorData)3 RegexStringUtility (priv.bajdcc.util.lexer.regex.RegexStringUtility)3 RuntimeMachine (priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine)2 ISemanticAnalyzer (priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer)2