Search in sources :

Example 31 with SyntaxException

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

the class TestSemantic4 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 = "a b c a";
        Semantic semantic = new Semantic(expr);
        semantic.addTerminal("a", TokenType.ID, "a");
        semantic.addTerminal("b", TokenType.ID, "b");
        semantic.addTerminal("c", TokenType.ID, "c");
        semantic.addNonTerminal("START");
        // 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("START -> @a [@b] [@c] @a");
        semantic.initialize("START");
        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 : SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) Semantic(priv.bajdcc.LALR1.semantic.Semantic) RegexException(priv.bajdcc.util.lexer.error.RegexException)

Example 32 with SyntaxException

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

the class Syntax method matchStorage.

/**
 * 匹配存储序号
 *
 * @param exp
 *            表达式
 * @param storage
 *            存储序号
 * @return 属性对象
 * @throws SyntaxException 词法错误
 */
private PropertyExp matchStorage(ISyntaxComponent exp, Object storage) throws SyntaxException {
    PropertyExp property;
    if (token.kToken == TokenType.STORAGE) {
        property = new PropertyExp((int) storage, null);
        next();
    } else {
        property = new PropertyExp(-1, null);
    }
    property.expression = exp;
    return property;
}
Also used : PropertyExp(priv.bajdcc.LALR1.syntax.exp.PropertyExp)

Example 33 with SyntaxException

use of priv.bajdcc.LALR1.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.LALR1.syntax.exp.RuleExp)

Example 34 with SyntaxException

use of priv.bajdcc.LALR1.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;
                    case // '('
                    LPARAN:
                        // 递归分析
                        exp = doAnalysis(TokenType.OPERATOR, OperatorType.RPARAN);
                        break;
                    case // '['
                    LSQUARE:
                        // 递归分析
                        exp = doAnalysis(TokenType.OPERATOR, OperatorType.RSQUARE);
                        // 包装
                        OptionExp option = new OptionExp();
                        option.expression = exp;
                        exp = option;
                        break;
                    default:
                        err(SyntaxError.SYNTAX);
                        break;
                }
                break;
            case EOF:
                return result;
            case TERMINAL:
                exp = matchTerminal();
                next();
                PropertyExp property1 = matchStorage(exp, token.object);
                exp = property1;
                if (token.kToken == TokenType.ACTION) {
                    property1.actionHandler = matchAction();
                    next();
                }
                if (token.kToken == TokenType.HANDLER) {
                    property1.errorHandler = matchHandler();
                    next();
                }
                break;
            case NONTERMINAL:
                exp = matchNonTerminal();
                next();
                PropertyExp property2 = matchStorage(exp, token.object);
                exp = property2;
                if (token.kToken == TokenType.ACTION) {
                    property2.actionHandler = matchAction();
                    next();
                }
                if (token.kToken == TokenType.HANDLER) {
                    property2.errorHandler = matchHandler();
                    next();
                }
                break;
            default:
                err(SyntaxError.SYNTAX);
                break;
        }
        if (exp != null) {
            sequence.add(exp);
        }
    }
    return result;
}
Also used : IExpCollction(priv.bajdcc.LALR1.syntax.exp.IExpCollction) BranchExp(priv.bajdcc.LALR1.syntax.exp.BranchExp) SequenceExp(priv.bajdcc.LALR1.syntax.exp.SequenceExp) OptionExp(priv.bajdcc.LALR1.syntax.exp.OptionExp) PropertyExp(priv.bajdcc.LALR1.syntax.exp.PropertyExp) OperatorType(priv.bajdcc.LALR1.syntax.token.OperatorType)

Example 35 with SyntaxException

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

the class Syntax method addTerminal.

/**
 * 添加终结符
 *
 * @param name
 *            终结符名称
 * @param type
 *            单词类型
 * @param obj
 *            单词信息
 * @throws SyntaxException 词法错误
 */
public void addTerminal(String name, priv.bajdcc.util.lexer.token.TokenType type, Object obj) throws SyntaxException {
    TokenExp exp = new TokenExp(arrTerminals.size(), name, type, obj);
    if (!mapTerminals.containsKey(name)) {
        mapTerminals.put(name, exp);
        arrTerminals.add(exp);
    } else {
        err(SyntaxError.REDECLARATION, name);
    }
}
Also used : TokenExp(priv.bajdcc.LALR1.syntax.exp.TokenExp)

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