0
votes

If I defined tokens like normal object access :

[$_a-zA-Z]+[.] { return ACCESS; }
[$_a-zA-Z]+    { return ID; }
[+]            { return PLUIS;  }

And the Bison grammar rules:

Accesses
    : Accesses ACCESS    { /*do something...*/ }
    | ACCESS             { /*do something...*/ }

Expression    
    : Accesses ID PLUS Accesses ID    { /*do something...*/ }

I want to allow such contents in source codes:

moduleA.valueB.valueC + valueD

In the example, if I don't put empty rule in Accesses, the single ID variable like valueD is illegal. But if I put the empty rule in, Accesses will cause seriously S/R conflicts, and texts it matched will become strange.

And, I don't think duplicate the rules in Expression is a good idea, ex:

Expression    
    : Accesses ID PLUS Accesses ID   { /*do something...*/ }
    | ID PLUS Accesses ID            { /*do something...*/ }
    | Accesses ID PLUS ID            { /*do something...*/ }
    | ID PLUS ID                     { /*do something...*/ }

Can I find other ways to solve this problem ?

EDIT: Ok thanks to your answer noticed me this simple grammar got no conflicts. At least you let me know that the real problem may hide in somewhere else ( what a mess for a compiler newbie ! ).

2
There are no shift reduce conflicts from the Expression rule you have if you change the Accesses rule to allow epsilon, so the problem you have must be elsewhere...Chris Dodd

2 Answers

1
votes

You can do it like this: lex:

[$_a-zA-Z]+ {return WORD;}
"."         {return DOT;}
"+"         {return PLUS;}

bison:

Expression   : Value PLUS Value;
Value        : WORD|WORD AccessList;
AccessElement: DOT WORD;
AccessList   : AccessElement|AccessList AccessElement;
1
votes

There's nothing wrong with just using an epsilon production in your example:

Expression    
    : Accesses ID PLUS Accesses ID    { /*do something...*/ }
;

Accesses
    : Accesses ACCESS    { /*do something...*/ }
    |                    { /*do something...*/ }
;

gives no conflicts....