Re: Grammar with low-precedence postfix operator?

Kaz Kylheku <kaz@kylheku.com>
Thu, 5 Feb 2015 21:10:47 +0000 (UTC)

          From comp.compilers

Related articles
Grammar with low-precedence postfix operator? rljacobson@gmail.com (Robert Jacobson) (2015-02-05)
Re: Grammar with low-precedence postfix operator? kaz@kylheku.com (Kaz Kylheku) (2015-02-05)
Re: Grammar with low-precedence postfix operator? anton@mips.complang.tuwien.ac.at (2015-02-07)
Re: Grammar with low-precedence postfix operator? rljacobson@gmail.com (Robert Jacobson) (2015-02-07)
Re: Grammar with low-precedence postfix operator? monnier@iro.umontreal.ca (Stefan Monnier) (2015-02-08)
Re: Grammar with low-precedence postfix operator? kaz@kylheku.com (Kaz Kylheku) (2015-02-09)
Re: Grammar with low-precedence postfix operator? DrDiettrich1@netscape.net (Hans-Peter Diettrich) (2015-02-09)
Re: Grammar with low-precedence postfix operator? jgk@panix.com (2015-02-10)
[2 later articles]
| List of all articles for this month |

From: Kaz Kylheku <kaz@kylheku.com>
Newsgroups: comp.compilers
Date: Thu, 5 Feb 2015 21:10:47 +0000 (UTC)
Organization: Aioe.org NNTP Server
References: 15-02-006
Keywords: parse
Posted-Date: 06 Feb 2015 22:12:08 EST

On 2015-02-05, Robert Jacobson <rljacobson@gmail.com> wrote:
> I'm trying to write an ANTLR4 grammar for a language with a low precedence
> postfix operator (Wolfram Language with '&', but I use a simple toy grammar
> below). I'm struggling with finding the right pattern to express this
> language.


I suspect this is not nicely solvable by LALR type tools. Only with hacks.


The problem is that the low precedence postfix operator has no starting
marker to look back to.


When this operator occurs in an expression, it means "everything which comes
before me in the expression is my argument".


The problem with this is that it leaves no room for the onstruct to be
*embedded* in another one. Since the operator says "everything is mine!" it
means, by the same token (pun intended) that "I am not a subexpression; I am
the top-level constituent!". And since that is the case, because
it is a postfix operator, it means that the expression ends right there.


If you want 2+2++^3 to mean (2+2)++^3, that means that you need
a special low-precedence versions of ^ (and other operators) for that
case, so the ++ expression can be a constituent of something.


Try something along these lines. This Yacc grammar only gives me one
shift-reduce conflicts, on account of the empty production rule for
pp_factor_continue. I didn't test it; I only skimmed through the state
transitions in y.output:


%token INT PLUSPLUS


%%


expr : mul_expr
          | expr '+' mul_expr
          | expr PLUSPLUS pp_add_continue
          ;


mul_expr : factor
                  | primary '*' factor
                  ;


factor : primary
              | factor '^' primary
              ;


primary : '(' expr ')'
                | INT
                ;


pp_add_continue : '+' mul_expr
                                | pp_mul_continue
                                ;


pp_mul_continue : '*' factor
                                | pp_factor_continue
                                ;


pp_factor_continue : '^' primary
                                      | /* NOTHING */
                                      ;


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.