Is SableCC appropriate for parsing open streams?

49 views
Skip to first unread message

Kevin Krumwiede

unread,
Jan 27, 2013, 2:40:09 AM1/27/13
to sab...@googlegroups.com
Is it possible to write a SableCC grammar in such a way that working classes receive ASTs while the input stream is still open?  For example, could you use it for something like a terminal emulator?

Gonzalo Ortiz Jaureguizar

unread,
Jan 27, 2013, 4:46:42 PM1/27/13
to sab...@googlegroups.com
It depends of what you want to do. I am almost sure that SableCC is not going to generate the AST until EOL is found (ie, when you call Parser#parse, the call is not going to return until the stream that the lexer is reading returns the EOL character). So if you want to parse infinitely, you can not use SableCC (maybe you can use Ratz! Parser but its documentation is poor).

If you want to parse something from the terminal but you want to stop in some specific character, maybe you can do it playing with the reader interface that SableCC uses. But I am talking on air and it can be complicated. In this case, you can also use another technique, you can use a trivial parser that simulates a StringTokenizer but with streams and parse each "line" (the string between two special characters) with SableCC


2013/1/27 Kevin Krumwiede <kjk...@gmail.com>
Is it possible to write a SableCC grammar in such a way that working classes receive ASTs while the input stream is still open?  For example, could you use it for something like a terminal emulator?

--
-- You received this message because you are subscribed to the SableCC group. To post to this group, send email to sab...@googlegroups.com. To unsubscribe from this group, send email to sablecc+u...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/sablecc?hl=en
 
 

Kevin Krumwiede

unread,
May 15, 2017, 1:16:54 AM5/15/17
to SableCC
Four years later...

I'm thinking of using SableCC to parse text that contains a mixture of garbage and patterns I'm interested in.  Lexer#getToken() contains this logic:

    if(accept_state != -1) {
        // return the token
    }
    else {
        if(this.text.length() > 0) {
            // throw a LexerException
        }   
        // return EOF
    }

So when it hits garbage, it throws a LexerException. I've overcome this by overriding getToken():

    @Override
    protected Token getToken() throws IOException, LexerException {
        try {
            return super.getToken();
        }
        catch(final LexerException e) {
            return new EOF(e.getToken().getLine(), e.getToken().getPos());
        }
    }

But this makes exception handling part of the normal program flow, so I'm looking for a better way.  Is it possible to override the lexer template (src/org/sablecc/sablecc/lexer.txt) without building my own version of SableCC?

Etienne Gagnon

unread,
May 15, 2017, 1:25:41 AM5/15/17
to sab...@googlegroups.com
Hi Kevin,

On 2017-05-15 at 00:01, Kevin Krumwiede wrote:
> Is it possible to override the lexer template
> (src/org/sablecc/sablecc/lexer.txt) without building my own version of
> SableCC?
>

Yes, it's possible. Just modify the template and replace the lexer.txt
within the jar file.

Cheers,

Etienne

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


Kevin Krumwiede

unread,
May 15, 2017, 2:40:56 AM5/15/17
to SableCC
In my case, the jar is downloaded and used by Maven.  But it turned out to be easier than I expected to create a customized version based on forks of sablecc-maven-plugin and sablecc-maven-port from https://github.com/johnny-bui.

Kevin Krumwiede

unread,
May 18, 2017, 4:10:52 PM5/18/17
to SableCC
Reply all
Reply to author
Forward
0 new messages