Postfix and right-associative operators in LR(0) parsers

Posted by Ian on Stack Overflow See other posts from Stack Overflow or by Ian
Published on 2012-08-29T09:37:47Z Indexed on 2012/08/29 9:38 UTC
Read the original article Hit count: 207

Filed under:
|
|
|
|

Is it possible to construct an LR(0) parser that could parse a language with both prefix and postfix operators? For example, if I had a grammar with the + (addition) and ! (factorial) operators with the usual precedence then 1+3! should be 1 + 3! = 1 + 6 = 7, but surely if the parser were LR(0) then when it had 1+3 on the stack it would reduce rather than shift?

Also, do right associative operators pose a problem? For example, 2^3^4 should be 2^(3^4) but again, when the parser have 2^3 on the stack how would it know to reduce or shift?

If this isn't possible is there still a way to use an LR(0) parser, possibly by converting the input into Polish or Reverse Polish notation or adding brackets in the appropriate places? Would this be done before, during or after the lexing stage?

© Stack Overflow or respective owner

Related posts about parsing

Related posts about postfix