简体   繁体   中英

Same operator as prefix and postfix Precedence issue (Grammar, Java Cup)

I'm using Java, JFlex which passes data to Java Cup.

How can I define precedence of an operator, which can be both postfix and prefix but with different precedence.

What I mean:

terminal END;
terminal OP1, OP2, OP3;

precedence OP3;
precedence OP2;
precedence OP1_POSTFIX;  //OP1 in postfix position
precedence OP1_PREFIX;   //OP1 in prefix position

EXPR ::= END |OP1_PREFIX EXPR | EXPR OP1_POSTFIX

I don't know how to make something like this work. Whatever I make up I end up with shift / reduce conflicts.

Thank you for your time and help.

UPDATE:

This is for a school project and I got the list of operator precedence, thus I have to stick with it.

This is a part of the given list of operator priorities from min to max priority:

binary +, -    (left precedence)
not
unary postfix ^
unary prefix +, -, ^

I don't know how to ensure this kind of precedence.

I don't know why, but it turns out this is the way to go.

terminal END, EXPR, EXPR1, EXPR2;
terminal OP1, OP2, OP3;

precedence OP3; 
precedence OP2;
precedence OP1_POSTFIX;  //OP1 in postfix position
precedence OP1_PREFIX;   //OP1 in prefix position

EXPR  ::= END | EXPR1 | EXPR2
EXPR1 ::= OP1_PREFIX EXPR
EXPR2 ::= EXPR OP1_POSTFIX

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM