Precedence directives

48 views
Skip to first unread message

Steve Stewart

unread,
Jul 16, 2012, 6:17:13 PM7/16/12
to sab...@googlegroups.com
Hi there,

I downloaded the latest stable release of SableCC. I'm new to SableCC, and I'm wondering if there is support and documentation for the use of precedence directives in a grammar.

Thanks,
Steven Stewart

Etienne Gagnon

unread,
Jul 16, 2012, 7:17:43 PM7/16/12
to sab...@googlegroups.com
Hi Steve,

SableCC 3.x (stable) does not have precedence directives. The reason is that the Bison/Yacc-like directives can easily modify the parsed language without issuing warnings about it. The generated parser could parse a language that is not described by the (directive-less) grammar. This is scary; you end up not knowing exactly what the parsed language is. By design, SableCC does not allow for such unsafe directives.

But, this is not a big limitation. Rewriting a grammar to implement precedence levels is pretty straight forward. You will find a detailed explanation of how to do it in the old mailing-list archive: http://lists.sablecc.org/pipermail/sablecc-discussion/msg00433.html

You can see a real life example in the Java grammar that has many precedence levels (was it 17 levels?): http://sablecc3.sablecc.org/wiki/Java-1.5

SableCC 4 will support precedence directives in a restricted but safe manner. Precedence directives, in SableCC 4, do not modify the language described by the grammar; they can only serve as a disambiguation mechanism.

Definitions:
  • Language: The (possibly infinite) set of accepted strings (i.e. the set of syntactically accepted programs).
  • Grammar: Finite description of a language (using productions, etc.).
  • Ambiguity: A grammar is ambiguous if it allows for two (or more) distinct syntax trees for the same string (i.e. program).
  • Disambiguation: Selecting one specific tree among the many syntax trees of a string, given an ambiguous grammar.

Have fun!

Etienne
Etienne Gagnon, Ph.D.
http://sablecc.org
On 2012-07-16 18:17, Steve Stewart wrote:

Steve Stewart

unread,
Jul 16, 2012, 9:53:34 PM7/16/12
to sab...@googlegroups.com
Thanks for the prompt reply. I was just sitting here reading your Master's thesis, in fact. I'll take a look at that link. Yes, using precedence directives as a disambiguation mechanism would be a desirable feature.

I may be posting again. I'm a PhD student in the formal methods lab at the University of Waterloo. Regardless of operator precedences, I'm trying to work out how to cleanly resolve a couple of parsing ambiguities in a grammar for Alloy 4 (a lightweight modelling language that's been around for about a decade). I'm thinking of adopting SableCC for the front-end, replacing a current JavaCUP/JLex implementation.

Regards,
Steven Stewart
Reply all
Reply to author
Forward
0 new messages