5

Is it possible to parse an expression (without ambiguity) that can contains binary prefix, binary infix and binary postfix operators (let's assume that all the symbols are different) with precedence between them? For example:

a = 2 3 post+
b = pre+ 2 3*4

Then a would equal to 5 because = has lower precedence than the postfix post+ operator and b would be 14. I know that you can parse infix notated expressions with operator precedence parse or shunting yard but this problem seems far more complex for me.

Edit:

Parenthesis are allowed and pre/post variations of an operator have the same precedence as the infix one.

I would like to roll a hand-written algorithm.

Edit2:

By precedence I mean how much to consume. For example this:

a = 2 3 post+

Could result in these AST-s:

'=' has higher precedence than 'post+':
    post+
    /  \
   =    3
  / \
 a  2

'post+' has higher precedence than '=':
      =
     / \
   a   post+
       /  \
      2    3

(The second one is what I need in this situation). I can't really use existing parser generators or fixed grammar for operands because the operators are loaded dynamically.

Peter Lenkefi
  • 1,306
  • 11
  • 29
  • How would you handle `2+3+4` using only `pre+` and `post+`? I am seeking a red flag and looking at the patterns that only use `pre+` and `post+` with them next to each other. – Guy Coder Jan 10 '17 at 17:01
  • @GuyCoder Parenthesis are allowed. 2+3+4 could be pre+ 2 3 4 post+ – Peter Lenkefi Jan 10 '17 at 17:07
  • @GuyCoder Maybe there should be a rule that if pre+, post+ and infix + have the same precedence, then the ordrer is: pre+ first, infix next and post+ at last – Peter Lenkefi Jan 10 '17 at 17:09
  • One of the easiest ways to check this is to use a tool that check for ambiguity on a grammars such as [ANTLR](http://www.antlr.org/)or use [Prolog DCG](http://www.amzi.com/manuals/amzi/pro/ref_dcg.htm) and look for multiple answers. – Guy Coder Jan 10 '17 at 17:13
  • Since you allow parenthesis `()` I don't see a problem because`(2 + 3)` is the same as `(pre+ 2 3)` which is the same as `(2 3 post+)`. If it is ambiguous without the parenthesis, just add parenthesis. – Guy Coder Jan 10 '17 at 17:16
  • Does your grammar/expressions use `*`, `/`, and `-`? You should really post a full grammar instead of an example with these type of questions. – Guy Coder Jan 10 '17 at 17:20
  • @GuyCoder Yes it would contain several operators with different precedences – Peter Lenkefi Jan 10 '17 at 17:20
  • Do the operators `pre+`, `post+` and `+` have the same or different precedence? – Guy Coder Jan 10 '17 at 17:22
  • @GuyCoder They do, but a rule could be applied (pre first, then in, and post lastly) – Peter Lenkefi Jan 10 '17 at 17:23
  • `They do` means what? `same` or `different` precedence. Again, please post a full grammar. When you have posted a full grammar I will take a look again, but I really don't foresee any problems because parenthesis are allowed. – Guy Coder Jan 10 '17 at 17:25
  • @GuyCoder Excuse me, I've misread. They have the same precedence. – Peter Lenkefi Jan 10 '17 at 17:26
  • 2
    @PeterLenkefi: Now I don't understand. If the names of `pre+`, `post+` and `infix+` are really different, why does it matter what their respective precedences are? But having said that, it is *impossible* for a prefix operator and a postfix operator to have the "same" precedence. For simplicity, consider only unary operators, and look at the expression `prefix operand postfix`. Either `prefix` or `postfix` must bind more tightly. (You can handwave about associativity, but in this context that's not really useful. In the end, one of the operators binds more tightly.) – rici Jan 11 '17 at 16:14
  • @rici Then what about if prefix is more tight than postfix? – Peter Lenkefi Jan 11 '17 at 16:29
  • @PeterLenkefi: From a parsing perspective, different symbols are different symbols. If you really mean that the names of the three variants of the `+` operator are different, then there is no reason for there to be *any* relationship between precedences. However, it is not meaningful to say that two operators with different syntaxes have the same precedence. – rici Jan 11 '17 at 16:43
  • You still have not posted a full grammar. – Guy Coder Jan 14 '17 at 14:40
  • @GuyCoder There is no grammar for it as the operators are just defined runtime and we can assume that an atomic expression is just a number or anything in parenthesis. I've found this: http://stackoverflow.com/questions/427040/any-reason-i-couldnt-create-a-language-supporting-infix-postfix-and-prefix-fu However the example code is in ML... – Peter Lenkefi Jan 14 '17 at 16:53

0 Answers0