Antlr4:不匹配输入 [英] Antlr4: Mismatch input

查看:29
本文介绍了Antlr4:不匹配输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的语法:

qwe.g4

grammar qwe;

query
    : COLUMN OPERATOR value EOF
    ;

COLUMN
    : [a-z_]+
    ;

OPERATOR
    : ('='|'>'|'<')
    ;

SCALAR
    : [a-z_]+
    ;

value
    : SCALAR
    ;

WS : [ \t\r\n]+ -> skip ;

在 Python 代码中处理:

Handling in Python code:

qwe.py

from antlr4 import InputStream, CommonTokenStream

from qweLexer import qweLexer
from qweParser import qweParser

conditions = 'total_sales>qwe'

lexer = qweLexer(InputStream(conditions))
stream = CommonTokenStream(lexer)
parser = qweParser(stream)
tree = parser.query()


for child in tree.getChildren():
    print(child, '==>', type(child))

运行 qwe.py 在解析(词法分析?)value 时输出错误:

Running qwe.py outputs error when parsing (lexing?) value:

如何解决这个问题?

我读了一些并假设与 COLUMN 规则也匹配 value...

I read some and suppose that there is something to do with COLUMN rule that also matches value...

推荐答案

你的 COLUMNSCALAR 词法分析器规则是相同的.当 LExer 匹配两个规则时,识别最长令牌的规则将获胜.如果令牌长度相同(如此处所示),则第一个规则获胜.

Your COLUMN and SCALAR lexer rules are identical. When the LExer matches two rules, then the rule that recognizes the longest token will win. If the token lengths are the same (as the are here), the the first rule wins.

您的令牌流将是 COLUMN OPERATOR COLUMN

那是你的 query 规则不匹配.

That's thy the query rule won't match.

作为一般做法,最好使用 grun 别名(安装教程将告诉您如何设置)来转储令牌流.

As a general practice, it's good to use the grun alias (that the setup tutorial will tell you how to set up) to dump the token stream.

grun qwe tokens -tokens <样本输入文件

一旦得到预期的输出,您可能需要使用 grun 工具来显示输入的解析树以验证它是否正确.所有这些都无需将生成的代码连接到目标语言中即可完成,并有助于确保您的语法在连接之前基本正确.

Once that gives you the expected output, you'll probably want to use the grun tool to display parse trees of your input to verify that is correct. All of this can be done without hooking up the generated code into your target language, and helps ensure that your grammar is basically correct before you wire things up.

这篇关于Antlr4:不匹配输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆