Is it possible to specify multiple entry points in a sablecc grammar ?

19 views
Skip to first unread message

fabien...@imt-atlantique.fr

unread,
Mar 8, 2019, 9:50:43 AM3/8/19
to SableCC
Hi all,
Is it possible to specify multiple entry points in a sablecc grammar ? If this is not possible is there any known hack to parse only parts of a valid program ?

Fabien

Etienne Gagnon

unread,
Mar 8, 2019, 11:09:24 AM3/8/19
to sab...@googlegroups.com
Hi Fabien,

It's not possible to do this directly, but it's very easy to do with the
following trick:

1) define dummy start tokens, one for each entry point, e.g.

...
Tokens,

  start_xxx = ;
  start_yyy = ;

  id = letter (letter | digit)*;
...

2) Use the dummy tokens to start the alternatives of an "entry_point"
main production, e.g.

  entry_point =
    {xxx} start_xxx xxx |
    {yyy} start_yyy yyy;

  xxx = ...
...

3) Define a custom lexer that injects in initial start token in the
token stream to select the appropriate entry point, e.g.

class CustomLexer extends Lexer {... new TStartXxx() ... new TStartYyy()
...}


Have fun!

Etienne

Etienne Gagnon, Ph.D.
http://sablecc.org

fabien...@imt-atlantique.fr

unread,
Mar 8, 2019, 11:19:57 AM3/8/19
to SableCC
Thanks a lot.
Reply all
Reply to author
Forward
0 new messages