I have a flex-bison project in which I need to support a few string operators, and operator '^' means reverse a string and operator [i] means return index i in the string. correct input and output for example :
input : ^"abc"[0] ---> correct output: "c", my output: "a"
that's because first I want to reverse it("cba") and then take the 0 index ("cba"[0] is c). Now, I don't know how to do that precedence, so my code outputs "a" since it first takes "abc"[0]--> "a" and then reverses it-->"a". as of now I have in my bison file:
%left STR MINI
%left '^'
substring:
STR MINI { //THIS IS DONE FIRST, SUBSTRING
$$ = substringFind($1,$2,$2,temp);
}
| '^' substring { //BUT I WANT THIS (REVERSING) TO BE FIRST
$$ = reverseStrings($2,temp);
}
;
how do I change that precedence? I don't really understand the precedence rules, it was very easy with plus (+) before multiple (*) but with those operators I don't really know how to work with it. ANY HELP...?
You need separate productions, not alternates within the same production, something like:
string
: substring
;
substring
: reverse MINI { ... }
| reverse
;
reverse
: "^" reverse { ... }
| STR
;