Search in sources :

Example 6 with Token

use of priv.bajdcc.util.lexer.token.Token in project jMiniLang by bajdcc.

the class Function method genCode.

@Override
public void genCode(ICodegen codegen) {
    codegen.genFuncEntry(getRefName());
    codegen.genCode(RuntimeInst.inop);
    if (name.toRealString().equals(realName)) {
        codegen.genCode(RuntimeInst.ipush, codegen.genDataRef(realName));
        codegen.genCode(RuntimeInst.irefun);
    }
    int start = codegen.getCodeIndex();
    int i = 0;
    for (Token token : params) {
        codegen.genCode(RuntimeInst.iloada, i);
        codegen.genCode(RuntimeInst.ipush, codegen.genDataRef(token.object));
        codegen.genCode(RuntimeInst.ialloc);
        codegen.genCode(RuntimeInst.ipop);
        i++;
    }
    block.genCode(codegen);
    int end = codegen.getCodeIndex() - 1;
    if (start <= end) {
        codegen.genDebugInfo(start, end, this.toString());
    }
    codegen.genCode(RuntimeInst.inop);
}
Also used : Token(priv.bajdcc.util.lexer.token.Token)

Example 7 with Token

use of priv.bajdcc.util.lexer.token.Token in project jMiniLang by bajdcc.

the class Function method getDoc.

public String getDoc() {
    if (doc == null || doc.isEmpty()) {
        return "过程无文档";
    }
    StringBuilder sb = new StringBuilder();
    for (Token token : doc) {
        sb.append(token.object.toString());
        sb.append(System.lineSeparator());
    }
    return sb.toString();
}
Also used : Token(priv.bajdcc.util.lexer.token.Token)

Example 8 with Token

use of priv.bajdcc.util.lexer.token.Token in project jMiniLang by bajdcc.

the class Semantic method run.

/**
 * 进行语义处理
 */
private void run() throws SyntaxException {
    if (!arrErrors.isEmpty()) {
        System.err.println(getTrackerError());
        throw new SyntaxException(SyntaxError.COMPILE_ERROR, arrErrors.get(0).position, "出现语法错误");
    }
    /* 规则集合 */
    ArrayList<RuleItem> items = npa.getRuleItems();
    /* 符号表查询接口 */
    IQuerySymbol query = getQuerySymbolService();
    /* 符号表管理接口 */
    IManageSymbol manage = getManageSymbolService();
    /* 语义错误处理接口 */
    ISemanticRecorder recorder = getSemanticRecorderService();
    /* 复制单词流 */
    ArrayList<Token> tokens = arrTokens.stream().map(Token::copy).collect(Collectors.toCollection(ArrayList::new));
    /* 运行时自动机 */
    SemanticMachine machine = new SemanticMachine(items, arrActions, tokens, query, manage, recorder, debug);
    /* 遍历所有指令 */
    arrInsts.forEach(machine::run);
    object = machine.getObject();
    if (object != null) {
        Function entry = (Function) object;
        manage.getManageScopeService().registerFunc(entry);
    }
}
Also used : Function(priv.bajdcc.LALR1.grammar.tree.Function) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RuleItem(priv.bajdcc.LALR1.syntax.rule.RuleItem) IManageSymbol(priv.bajdcc.LALR1.grammar.symbol.IManageSymbol) Token(priv.bajdcc.util.lexer.token.Token) IQuerySymbol(priv.bajdcc.LALR1.grammar.symbol.IQuerySymbol) ISemanticRecorder(priv.bajdcc.LALR1.grammar.semantic.ISemanticRecorder)

Example 9 with Token

use of priv.bajdcc.util.lexer.token.Token in project jMiniLang by bajdcc.

the class Semantic method getTokenList.

/**
 * 获取单词流描述
 * @return 单词流描述
 */
public String getTokenList() {
    StringBuilder sb = new StringBuilder();
    sb.append("#### 单词流 ####");
    sb.append(System.lineSeparator());
    for (Token token : arrTokens) {
        sb.append(token.toString());
        sb.append(System.lineSeparator());
    }
    return sb.toString();
}
Also used : Token(priv.bajdcc.util.lexer.token.Token)

Example 10 with Token

use of priv.bajdcc.util.lexer.token.Token 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("3 - (28 / (4 * 7)) * (2 + 4) + 5");
        grammar.addTerminal("i", TokenType.INTEGER, null);
        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);
        String[] nons = new String[] { "E", "T", "F" };
        for (String non : nons) {
            grammar.addNonTerminal(non);
        }
        grammar.addPatternHandler("1", new IPatternHandler() {

            @Override
            public Object handle(List<Token> tokens, List<Object> symbols) {
                return Integer.parseInt(tokens.get(0).object.toString());
            }

            @Override
            public String getPatternName() {
                return "操作数转换";
            }
        });
        grammar.addPatternHandler("010", new IPatternHandler() {

            @Override
            public Object handle(List<Token> tokens, List<Object> symbols) {
                int lop = (int) symbols.get(0);
                int rop = (int) symbols.get(1);
                Token op = tokens.get(0);
                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;
                }
            }

            @Override
            public String getPatternName() {
                return "二元运算";
            }
        });
        grammar.addPatternHandler("101", new IPatternHandler() {

            @Override
            public Object handle(List<Token> tokens, List<Object> symbols) {
                Token ltok = tokens.get(0);
                Token rtok = tokens.get(1);
                Object exp = symbols.get(0);
                if (ltok.object == OperatorType.LPARAN && rtok.object == OperatorType.RPARAN) {
                    // 判断括号
                    return exp;
                }
                return null;
            }

            @Override
            public String getPatternName() {
                return "括号运算";
            }
        });
        grammar.infer("E -> E @PLUS T | E @MINUS T | T");
        grammar.infer("T -> T @TIMES F | T @DIVIDE F | F");
        grammar.infer("F -> @LPA E @RPA | @i");
        grammar.initialize("E");
        System.out.println(grammar.getPrecedenceString());
        System.out.println(grammar.toString());
        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 : Token(priv.bajdcc.util.lexer.token.Token) Grammar(priv.bajdcc.OP.grammar.Grammar) OperatorType(priv.bajdcc.util.lexer.token.OperatorType) SyntaxException(priv.bajdcc.OP.syntax.handler.SyntaxException) GrammarException(priv.bajdcc.OP.grammar.error.GrammarException) RegexException(priv.bajdcc.util.lexer.error.RegexException) IPatternHandler(priv.bajdcc.OP.grammar.handler.IPatternHandler)

Aggregations

Token (priv.bajdcc.util.lexer.token.Token)22 OperatorType (priv.bajdcc.util.lexer.token.OperatorType)6 RegexException (priv.bajdcc.util.lexer.error.RegexException)5 IPatternHandler (priv.bajdcc.OP.grammar.handler.IPatternHandler)4 ExpValue (priv.bajdcc.LALR1.grammar.tree.ExpValue)3 GrammarException (priv.bajdcc.OP.grammar.error.GrammarException)3 SyntaxException (priv.bajdcc.OP.syntax.handler.SyntaxException)3 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)2 Grammar (priv.bajdcc.OP.grammar.Grammar)2 Lexer (priv.bajdcc.util.lexer.Lexer)2 TokenType (priv.bajdcc.util.lexer.token.TokenType)2 BufferedReader (java.io.BufferedReader)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 PrintStream (java.io.PrintStream)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Scanner (java.util.Scanner)1 Grammar (priv.bajdcc.LALR1.grammar.Grammar)1 RuntimeFuncObject (priv.bajdcc.LALR1.grammar.runtime.data.RuntimeFuncObject)1