2

I am facing a problem that you guys can probably help me solve.

I am trying to parse a grammar that contains this :

Expr:  BinOp | Name | Constant ;
Name: id=ID ;
Constant: value=INT ;
BinOp: left=expr op=operator right=expr ;
Operator: Add | Sub ;
Add: 'plus' ;
Sub: 'minus' ;

But this is causing a stack overflow because I guess the BinOp evaluates itself as the left expr and it goes like this forever. Any idea on how to solve the problem ? If possible without the need to add delimiters such as parenthesis.

MrFlow
  • 53
  • 5

1 Answers1

2

textX uses recursive descent parsing with backtracking (implemented by Arpeggio), i.e. it is a top-down approach which by default doesn't support left recursive rules, because to match some A rule it first has to match A leading to an infinite recursion.

In order to implement expression-like languages you need to remove all directly or indirectly left recursive rules. There is a mechanical approach to do that. This description of left recursion removal works for Arpeggio and thus textX.

You can also see calc example in Arpeggio. Although the syntax in Arpeggio is different the Arpeggio is the underlying engine of textX so all that is written there works for textX.

Also note that, in the transformed grammar, operator priority is encoded in the grammar rules by having lower priority operators match sooner and higher priority operators match later.

Igor Dejanović
  • 881
  • 5
  • 5