Search in sources :

Example 21 with RegexException

use of priv.bajdcc.util.lexer.error.RegexException 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);
        String[] nons = new String[] { "E", "T", "F" };
        for (String non : nons) {
            syntax.addNonTerminal(non);
        }
        syntax.infer("E -> E @PLUS T | E @MINUS T | T");
        syntax.infer("T -> T @MINUS F | T @DIVIDE F | F");
        syntax.infer("F -> @LPA E @RPA | @SYMBOL");
        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.OP.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Syntax(priv.bajdcc.OP.syntax.Syntax)

Example 22 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.OP.syntax.lexer.SyntaxLexer) RegexException(priv.bajdcc.util.lexer.error.RegexException) Token(priv.bajdcc.OP.syntax.token.Token)

Example 23 with RegexException

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

the class StringFilter 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 == kMeta) {
                // 过滤终结符
                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 24 with RegexException

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

the class StringPairFilter 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 == kMetaBegin || data.kMeta == kMetaEnd) {
                // 过滤终结符
                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 25 with RegexException

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

the class TestLexer method main.

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

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