简体   繁体   English

Python ANTLR4 示例 - 解析器似乎无法正确解析

[英]Python ANTLR4 example - Parser doesn't seem to parse correctly

To demonstrate the problem, I'm going to create a simple grammar to merely detect Python-like variables.为了演示这个问题,我将创建一个简单的语法来仅检测类似 Python 的变量。

I create a virtual environment and install antlr4-python3-runtime in it, as mentioned in " Where can I get the runtime? ":我创建了一个虚拟环境并在其中安装了antlr4-python3-runtime ,如“ 哪里可以获得运行时? ”中所述:

安装 antlr4-python3-runtime

Then, I create a PyVar.g4 file with the following content:然后,我创建了一个PyVar.g4以下内容的PyVar.g4文件:

grammar         PyVar;

program:        IDENTIFIER+;

IDENTIFIER:     [a-zA-Z_][a-zA-Z0-9_]*;

NEWLINE:        '\n' | '\r\n';

WHITESPACE:     [ ]+ -> skip;

Now if I test the grammar with grun , I can see that the grammar detects the variables just fine:现在,如果我用grun测试语法,我可以看到语法检测到变量就好了:

用 grun 测试语法

Now I'm trying to write a parser in Python to do just that.现在我正在尝试用 Python 编写一个解析器来做到这一点。 I generate the Lexer and Parser, using this command:我使用以下命令生成词法分析器和解析器:

antlr4 -Dlanguage=Python3 PyVar.g4

And they're generated with no errors:它们的生成没有错误:

antlr4 -Dlanguage=Python3 PyVar.g4

But when I use the example provided in " How do I run the generated lexer and/or parser? ", I get no output:但是当我使用“ 如何运行生成的词法分析器和/或解析器? ”中提供的示例时,我没有得到任何输出:

Python 解析器不产生任何结果

What am I not doing right?我做错了什么?

There are two problems here.这里有两个问题。

1. The grammar : 1. 语法

In the line where I had,在我所在的那条线上,

program:        IDENTIFIER+;

the parser will only detect one or more variables, and it will not detect any newline.解析器只会检测一个或多个变量,而不会检测任何换行符。 The output you see when running grun is the output created by the lexer, that's why newlines are present in the tokens.您在运行grun时看到的输出是词法分析器创建的输出,这就是标记中出现换行符的原因。 So I had to replace it with something like this, for the parser to detect newlines.所以我不得不用这样的东西替换它,以便解析器检测换行符。

program:        (IDENTIFIER | NEWLINE)+;

2. Printing the output of parser 2. 打印解析器的输出

In PyVar.py file, I created a tree with this line:PyVar.py文件中,我用这一行创建了一个树:

tree = parser.program()

But it didn't print its output, nor did I know how to, but the OP's comment on this accepted answer suggests using tree.toStringTree() .但它没有打印其输出,我也不知道如何打印,但是OP 对这个已接受答案的评论建议使用tree.toStringTree()

Now if we fix those, we can see that it works:现在,如果我们修复这些,我们可以看到它有效:

作品!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM