Thu, 5 Feb 2015 13:27:31 -0500 (EST)

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) |

[3 later articles] |

From: | Robert Jacobson <rljacobson@gmail.com> |

Newsgroups: | comp.compilers |

Date: | Thu, 5 Feb 2015 13:27:31 -0500 (EST) |

Organization: | Compilers Central |

Keywords: | parse, question |

Posted-Date: | 05 Feb 2015 13:27:31 EST |

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.

Consider my (incorrect) grammar below that parses math expressions:

<snip>

grammar MathExpression;

//Parser Rules

term

: INT

| '(' expr ')' //parentheses

| <assoc=right> term '^' term //exponentiation

| term term //implicit multiplication

;

expr

: term

| ('+' | '-') expr //unary plus/minus

| expr ('/' | '*') expr //division and explicit multiplication

| expr ('+' | '-') expr //addition/subtraction

| expr '++' //increment

;

//Lexer Rules

INT : [0-9]+ ;

WS : [ \t\r\n]+ -> skip ;

<snip>

The problem with this grammar is that the generated parser chokes on, for

example, the input '2++^3', as '^' cannot follow '++' in this grammar. If I

get rid of implicit multiplication I can collapse the grammar into a single

rule that parses the language I am wanting, but I don't want to give up

implicit multiplication.

What's the pattern for this situation?

Incidentally, this is my first post to this group, and I am charmed to see

several authors of my textbooks among the members here.

Best,

Robert

Post a followup to this message

Return to the
comp.compilers page.

Search the
comp.compilers archives again.