QLEXpress语法分析和计算的入口类详细分析--Expressrunner(三)

QLEXpress语法分析和计算的入口类详细分析–Expressrunner(三)

1.执行一段文本(一)

2.执行一段文本(二)

3.执行一段文本(三)

public Object execute(String expressString, IExpressContext<String, Object> context, List<String> errorList,
        boolean isCache, boolean isTrace, Log log) throws Exception {
        InstructionSet parseResult;
        if (isCache) {
            parseResult = expressInstructionSetCache.get(expressString);
            if (parseResult == null) {
                synchronized (expressInstructionSetCache) {
                    parseResult = expressInstructionSetCache.get(expressString);
                    if (parseResult == null) {
                        parseResult = this.parseInstructionSet(expressString);
                        expressInstructionSetCache.put(expressString, parseResult);
                    }
                }
            }
        } else {
            parseResult = this.parseInstructionSet(expressString);
        }
        return executeReentrant(parseResult, context, errorList, isTrace, log);
    }

    private Object executeReentrant(InstructionSet sets, IExpressContext<String, Object> iExpressContext,
        List<String> errorList, boolean isTrace, Log log) throws Exception {
        try {
            int reentrantCount = threadReentrantCount.get() + 1;
            threadReentrantCount.set(reentrantCount);

            return reentrantCount > 1 ?
                // 线程重入
                InstructionSetRunner.execute(this, sets, this.loader, iExpressContext, errorList, isTrace, false, true,
                    log, false) :
                InstructionSetRunner.executeOuter(this, sets, this.loader, iExpressContext, errorList, isTrace, false,
                    log, false);
        } finally {
            threadReentrantCount.set(threadReentrantCount.get() - 1);
        }
    }

4.解析一段文本,生成指令集合

5.输出全局定义信息

6.优先从本地指令集缓存获取指令集,没有的话生成并且缓存在本地

 public InstructionSet getInstructionSetFromLocalCache(String expressString) throws Exception {
        InstructionSet parseResult = expressInstructionSetCache.get(expressString);
        if (parseResult == null) {
            synchronized (expressInstructionSetCache) {
                parseResult = expressInstructionSetCache.get(expressString);
                if (parseResult == null) {
                    parseResult = this.parseInstructionSet(expressString);
                    expressInstructionSetCache.put(expressString, parseResult);
                }
            }
        }
        return parseResult;
    }

    public InstructionSet createInstructionSet(ExpressNode root, String type) throws Exception {
        InstructionSet result = new InstructionSet(type);
        createInstructionSet(root, result);
        return result;
    }

    public void createInstructionSet(ExpressNode root, InstructionSet result) throws Exception {
        Stack<ForRelBreakContinue> forStack = new Stack<>();
        createInstructionSetPrivate(result, forStack, root, true);
        if (!forStack.isEmpty()) {
            throw new QLCompileException("For处理错误");
        }
    }

    public boolean createInstructionSetPrivate(InstructionSet result, Stack<ForRelBreakContinue> forStack,
        ExpressNode node, boolean isRoot) throws Exception {
        InstructionFactory factory = InstructionFactory.getInstructionFactory(node.getInstructionFactory());
        return factory.createInstruction(this, result, forStack, node, isRoot);
    }

7.获取一个表达式需要的外部变量名称列表

8.是否忽略charset类型的数据,而识别为string

9.提供简答的语法检查,保证可以在运行期本地环境编译成指令

10.提供复杂的语法检查,不保证可以在运行期本地环境编译成指令

public boolean checkSyntax(String text, boolean mockRemoteJavaClass, List<String> remoteJavaClassNames) {
        try {
            Map<String, String> selfDefineClass = new HashMap<>();
            for (ExportItem item : this.loader.getExportInfo()) {
                if (item.getType().equals(InstructionSet.TYPE_CLASS)) {
                    selfDefineClass.put(item.getName(), item.getName());
                }
            }
            Word[] words = this.parse.splitWords(text, isTrace, selfDefineClass);
            ExpressNode root = this.parse.parse(this.rootExpressPackage, words, text, isTrace, selfDefineClass,
                mockRemoteJavaClass);
            InstructionSet result = createInstructionSet(root, "main");
            if (this.isTrace && log.isDebugEnabled()) {
                log.debug(result);
            }
            if (mockRemoteJavaClass && remoteJavaClassNames != null) {
                remoteJavaClassNames.addAll(Arrays.asList(result.getVirClasses()));
            }
            return true;
        } catch (Exception e) {
            log.error("checkSyntax has Exception", e);
            return false;
        }
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. 课程设计目标 实验建立C-编译器。只含有扫描程序(scanner)和语法分析(parser)部分。 2. 分析与设计 C-编译器设计的整体框架,本实验实现扫描处理和语法分析程序(图中粗黑部分)。 2.1 、扫描程序scanner部分 2.1.1系统设计思想 设计思想:根据DFA图用switch-case结构实现状态转换。 惯用词法: ① 语言的关键字:else if int return void while ② 专用符号:+ - * / < >= == != = ; , ( ) [ ] { } /* */ ③ 其他标记是ID和NUM,通过下列正则表达式定义: ID = letter letter* NUM = digit digit* letter = a|..|z|A|..|Z digit = 0|..|9 大写和小写字母是有区别的 ④ 空格由空白、换行符和制表符组成。空格通常被忽略,除了它必须分开ID、NUM关键字。 ⑤ 注释用通常的C语言符号/ * . . . * /围起来。注释可以放在任何空白出现的位置(即注释不能放在标记内)上,且可以超过一行。注释不能嵌套 说明:当输入的字符使DFA到达接受状态的时候,则可以确定一个单词了。初始状态设置为START,当需要得到下一个token时,取得次token的第一个字符,并且按照DFA与对此字符的型分析,转换状态。重复此步骤,直到DONE为止,输出token型。当字符为“/”时,状态转换为SLAH再判断下一个字符,如果为“*”则继续转到INCOMMENT,最后以“*”时转到ENDCOMMENT状态,表明是注释,如果其他的则是字符停滞于当前字符,并且输出“/”。 2.1.2程序流程图

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsgodlike

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值