Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #1508

Grammar with low-precedence postfix operator?

From Robert Jacobson <rljacobson@gmail.com>
Newsgroups comp.compilers
Subject Grammar with low-precedence postfix operator?
Date 2015-02-05 13:27 -0500
Organization Compilers Central
Message-ID <15-02-006@comp.compilers> (permalink)

Show all headers | View raw


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

Back to comp.compilers | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Grammar with low-precedence postfix operator? Robert Jacobson <rljacobson@gmail.com> - 2015-02-05 13:27 -0500
  Re: Grammar with low-precedence postfix operator? Kaz Kylheku <kaz@kylheku.com> - 2015-02-05 21:10 +0000
    Re: Grammar with low-precedence postfix operator? Robert Jacobson <rljacobson@gmail.com> - 2015-02-07 10:30 -0800
  Re: Grammar with low-precedence postfix operator? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-02-07 11:27 +0000
    Re: Grammar with low-precedence postfix operator? jgk@panix.com (Joe keane) - 2015-02-10 21:03 +0000
      Re: Grammar with low-precedence postfix operator? Kaz Kylheku <kaz@kylheku.com> - 2015-02-11 02:19 +0000
  Re: Grammar with low-precedence postfix operator? Stefan Monnier <monnier@iro.umontreal.ca> - 2015-02-08 12:34 -0500
    Re: Grammar with low-precedence postfix operator? Kaz Kylheku <kaz@kylheku.com> - 2015-02-09 01:24 +0000
      Re: Grammar with low-precedence postfix operator? Hans-Peter Diettrich <DrDiettrich1@netscape.net> - 2015-02-09 08:05 +0100
  Re: Grammar with low-precedence postfix operator? Robert Jacobson <rljacobson@gmail.com> - 2015-02-21 12:55 -0800

csiph-web