Zinxira
Zinxira

Reputation: 11

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. 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". 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.

Upvotes: 0

Views: 305

Answers (1)

Zinxira
Zinxira

Reputation: 11

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')}
    ;
...

Upvotes: 1

Related Questions