Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news.iecc.com!.POSTED!nerds-end From: Robert Jacobson Newsgroups: comp.compilers Subject: Grammar with low-precedence postfix operator? Date: Thu, 5 Feb 2015 13:27:31 -0500 (EST) Organization: Compilers Central Lines: 45 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <15-02-006@comp.compilers> NNTP-Posting-Host: news.iecc.com Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: miucha.iecc.com 1423160851 74912 2001:470:1f07:1126:0:676f:7373:6970 (5 Feb 2015 18:27:31 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Thu, 5 Feb 2015 18:27:31 +0000 (UTC) Keywords: parse, question Posted-Date: 05 Feb 2015 13:27:31 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:1508 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: grammar MathExpression; //Parser Rules term : INT | '(' expr ')' //parentheses | 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 ; 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