Is there some standardized or widely accepted algorithm for picking up operators in shift/reduce conflicts in LALR parser? The question is naive, my problem is not with implementing my solution, but implementing the solution is already widely used.For shift the operator is the next input token, for reduce, it depends -- I consider all already read symbols (for given production) declared as operators:if there is one -- it is the operatorif there are more than one -- I report grammar errorif there is none I use the input token as operatorSo for example:E ::= E + EE ::= E * EE ::= numin case ofE + E | * numConsidering first production I read +, and since it is the only one operator read I pick this one for reduce operator. * is the first token in input so it servers as shift operator. But is my algorithm correct (i.e. the same as normally used for LALR parsers)? I am especially worried for the point when I have more than 1 operator in the read tokens.UpdateWhat I NOT am asking hereI don't ask how to resolve shift/reduce conflict once you have the operators. I don't ask how to set the precedence for operators.What I am asking hereI ask how to extract operators from the stream of tokens. Let's say user set some precedence rules for +, - and *. The stack is: - E + E and input is E. Or the stack is E and input is *.What is the operator for reduce in first case? And in the second? Why?The stack is really entire right hand side of production.
How to extract operators from the grammar productions for conflict resolution in LALR parser?
parsing;operators
Converted from a comment, first, a rephrasing of your question:Suppose we have the following ambiguous grammar:E ::= E + EE ::= E * EE ::= numIn the LR automaton, we will have the following shift-reduce conflict (among others):E ::= E * E , +E ::= E  + EAs is common knowledge, reducing here will make '*' bind more tightly as '+' (as normal), while shifting will make '+' bind more tightly.Parser generators such as Yacc and Bison allow the user to mark tokens as binding more tightly than other tokens. As normal, we say that '* > +'.However, there seems to be a mismatch somewhere: the '>' we use above is an operator between two tokens, but the shift-reduce conflict is a conflict between a token and a production.In Yacc and Bison, not just tokens but productions also have a priority. The user can mark tokens with priorities (as is normally done), and productions (that have not been given an explicit priority) have their priority inferred. The priority of a production is equal to the priority of the rightmost token in the production.In E ::= E * E, the rightmost token is '*', so in the shift reduce conflict above the priority of the reduction is '*', and the priority of the shift is '+', and as the priority of '*' is higher than '+', we decide to reduce rather than shift. As another example, the rightmost token of A ::= a b B c d C D E is 'd' (with only capitalized letters being nonterminals).Adding A ::= a b B c d C D E %prec a to a production in Yacc and Bison explicitly gives that production the priority of 'a', but this is not normally done. Note that some productions may not have a rightmost token, or the priority between tokens is not defined, in which cases Yacc and Bison fall back to always shifting to resolve conflicts (Yacc and Bison always create a fully deterministic table, unless in GLR mode).For completeness: the action with higher priority wins, so if the production has a higher priority than the token, we reduce instead of shifting. If priorities are equal, we look at associativity: left associativity means reducing, right associativity means shifting. 'nonassoc' is the third type of 'associativity', and it means an error: the shift/reduce conflict is resolved by removing both entries in the table, so the parser rejects inputs entering that state with that lookahead.