简体   繁体   English

带有Python3的ANTLR4:“ IndentationError:意外缩进”

[英]ANTLR4 with Python3 : “IndentationError : unexpected indent”

I am learning ANTLR4 with Python 3.4.2 and my goal here is just to write multiple lines of Python code inside the {} of a rule. 我正在学习ANTLR4和Python 3.4.2,我的目标是在规则的{}中编写多行Python代码。 When I define the rules of my parser, I have the following block of code : 当我定义解析器的规则时,我具有以下代码块:

...
term
    : term '*' fact
    | term '/' fact 
    {
print('a')
    }
    | fact
      {
print('b')
      }
    ;
...

Which doesn't compile and raises " print('a') IndentationError : unexpected indent ". 不会编译并引发“ print('a')IndentationError:意外缩进 ”。 I tried to understand and I found that the following block of code doesn't throw any error : 我试图理解,发现以下代码块不会引发任何错误:

...
term
    : term '*' fact
    | term '/' fact 
    {print('a')}
    | fact
      {
print('b')
      }
    ;
...

It acts as if it was ok when I used one operand but not with 2 operands. 当我使用一个操作数而不是两个操作数时,它的行为就好像可以。

Why ? 为什么呢

I did my own searches on internet but I didn't find any similar cases. 我在互联网上进行了自己的搜索,但没有发现任何类似的案例。

Ok, I have found something that seems to work : 好的,我发现了一些似乎可行的方法:

...
term
    : term '*' fact
    | term '/' fact 
      {print('a1')}
      {print('a2')}
    | fact
      {print('b1')}
      {print('b2')}
    ;
...

and it's also ok with indentation : 缩进也可以:

...
term
    : term '*' fact
    | term '/' fact 
      {if True:}
      {    print('a1')}
      {    print('a2')}
    | fact
      {print('b1')}
      {print('b2')}
    ;
...

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

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