1

I am trying to parse CSP(Communicating Sequential Processes) CSP Reference Manual. I have defined following grammar rules.

assignment
    : IDENT '=' processExpression
    ;
processExpression
    :   ( STOP
        | SKIP
        | chaos
        | prefix
        | prefixWithValue
        | seqComposition
        | interleaving
        | externalChoice

        ....

seqComposition
    :   processExpression ';' processExpression
    ;
interleaving
    :   processExpression '|||' processExpression
    ;
externalChoice
    :   processExpression '[]' processExpression
        ;

Now ANTLR reports that

seqComposition 
interleaving
externalChoice

are left recursive . Is there any way to remove this or I should better used Bison Flex for this type of grammar. (There are many such rules)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tasawer Khan
  • 5,994
  • 7
  • 46
  • 69

2 Answers2

3

Define a processTerm. Then write rules looking like

assignment
    : IDENT '=' processExpression
    ;
processTerm
    :   ( STOP
        | SKIP
        | chaos
        | prefix
        ...
processExpression
    :   ( processTerm
        | processTerm ';' processExpression
        | processTerm '|||' processExpression
        | processTerm '[]' processExpression

        ....

If you want to have things like seqComposition still defined, I think that would be OK as well. But you need to make sure that the parsing of processExpansion is going to always consume more text as you proceed through your rules.

btilly
  • 43,296
  • 3
  • 59
  • 88
  • 1
    Deleted my answer. Separating terms from operators is definitely the way to go! It would probably be best to factor out processTerm to avoid backtracking, though, unless ANTLR does that automatically? – ikegami Mar 24 '11 at 22:09
  • 1
    `processExpression : processTerm ( ';' processExpression | '|||' processExpression | '[]' processExpression | ) ;` – ikegami Mar 24 '11 at 22:10
  • @ikegami: ANTLR is supposed to compile a DFA to do lookahead, which should factor that out automatically. Though it can't hurt to do that for it. Unless, of course, you forget to make a `processTerm` a `processExpansion`. :-P – btilly Mar 24 '11 at 22:37
  • @btilly, I didn't forget, if you're implying that I did. Note the empty alternation. – ikegami Mar 25 '11 at 05:08
  • @ikegami: Clearly I hadn't noted it before. In part, possibly because that possibility was first on my list and last on yours. – btilly Mar 25 '11 at 06:12
  • @btilly, That was intentional. If productions are attempted in order of appearance, placing the empty production last can reduce backtracking. – ikegami Mar 25 '11 at 08:12
  • Thanks for answer but problem is that Term is actually expression in this case. a seqComposition can be seqComposition ';' seqComposition. How should I model that? – Tasawer Khan Mar 25 '11 at 14:03
  • @tasawer-khan: Can a `seqComposition` be empty? If not then if you look far enough to the left, a `seqComposition` is always something simpler followed by `;` followed by something more complex. Give that something simpler a name. If it can be empty, break out that as a special case, and now a `seqComposition` has to either start with a `;` or some kind of simpler term and you no longer have recursion. – btilly Mar 25 '11 at 15:33
  • @ikegami: Ah. ANTLR compiles a DFA to match forward whatever it can without backtracking, so normally you should do very little backtracking. – btilly Mar 25 '11 at 15:34
  • @btilly thank you for your answer. I am learning techniques like you told. But after this answer I am sure this can be done so it is worth spending time. It is my first parser so will take some time to everything right. Thank you for providing very useful info and motivation. – Tasawer Khan Mar 25 '11 at 19:19
  • Currently ANTLR does not perform automatic left-factoring. Depending on the maximum number of terminals matched by the `processTerm` rule, left factoring the `processTerm` in `processExpression` could lead to substantially reduced memory overhead and overall parser performance. – Sam Harwell Apr 27 '13 at 02:23
1

Read the guide to removing left recursion in on the ANTLR wiki. It helped me a lot.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Matthew Sowders
  • 1,640
  • 1
  • 19
  • 32