[antlr-interest] Island grammar for reading shell commands

Jim Idle jimi at temporal-wave.com
Wed Dec 1 10:48:38 PST 2010


Well, if it is lexer triggered then it is easy, just embed an action to do
it, All you need is 

int c = input.LA(1) 
and 
input.consume()

When the lexer token that triggers consume to end of line is seen, just eat
the rest yourself. There are probably lots of ways to do this. The text will
then be included in whatever token triggers the consume and you can deal
with it from the parser.

Jim

> -----Original Message-----
> From: Bill Lear [mailto:rael at zopyra.com]
> Sent: Tuesday, November 30, 2010 6:05 PM
> To: Jim Idle
> Cc: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Island grammar for reading shell commands
> 
> On Tuesday, November 30, 2010 at 15:49:39 (-0800) Jim Idle writes:
> >REST_OF_LINE allows an empty token which will immediately match
> nothing
> >and continue to do so forever. You want +  not *. I think you might be
> >doing this wrong to be honest. I would probably not use ANTLR for
> this.
> 
> Maybe, but Antlr is so cool ...
> 
> I did try replacing * with + and I got the same error.  Blech.  I
> really didn't want to write a parser by hand for all of this.  I've got
> significantly more to do than what I've got here, the rest of which
> should be easy to handle for antlr.
> 
> So, there is really no viable way to have Antlr read the rest of the
> line of input?  I would be happy to just write the Java code for that
> one line:
> 
> shell -timeout 30 find /var/log -name ....
> 
> If I could just get hold of the input stream, read to end of line, I
> could hand-parse the '-timeout N' part, etc., and then let the lexer
> continue reading on the next line.  Is there no way to "cut out"
> a part of the input like this to process separately?
> 
> Actually, I think I have a sick idea: Since this is line-based stuff,
> and small files, I can read this all in to memory.  I can hand-parse
> the shell command lines, and replace them with an empty line (to
> preserve line numbers in case of error) in the input, noting which
> lines I modified.  Then, I can give the massaged input to Antlr.  The
> parser is going to produce one instance of a Command class for each
> line, and return a list of them, in order.  I can just put the shell
> Command instances back in the list where they belong and be on my merry
> way..
> 
> As I said though, it would be really cool if I could just do this all
> in Antlr.
> 
> 
> Bill



More information about the antlr-interest mailing list