[antlr-interest] Re: CLI with ANTLR - best practice

thierryj8 thierryj8 at yahoo.com
Fri Apr 9 14:23:45 PDT 2004


I tried the (command NEWLINE)+ and it worked as long as there was no
error.

Also, since the RETURN does not mean the end of file, I naturally had
commands that could spawn in several lines:

>list *
Processing list

Same as
>list
>*
Processing list

After thinking further, it seemed that each line should be it's own
file (thinking of a java paser), and the / to continue a line should
be handled as a special case.

I would like feedback on this format (probably not best practice, but
it seems to work so far).

The java main looks like:

  public static void main(String[] args)
  {
    try
    {
      ConsoleLexer consoleLexer = new ConsoleLexer(new
DataInputStream(System.in));
      ConsoleParser consoleParser = new ConsoleParser(consoleLexer);
      
      boolean parserException = false;
      while (parserException == false) // Each line is considered a
full file
      {
        try
        {
          System.out.print(consoleLexer.getMode() + ">");
  			  consoleParser.parse_prompt_command();
        }
        catch (Exception e)
        {
          parserException = true; // Exception caught in .g
        }
      }
    }
    catch (Exception e)
    {
      System.err.println("main exception: " + e); // really bad exception
    }
  }

And the paser command looks like:
command
  : (cmd1 | ... | cmd)
    exception
    catch [RecognitionException ex]
    {
      System.out.println("command exception " + ex.getMessage());
    }
  ;

Then new line is made an EOF type:

NEWLINE
  :  '\r' '\n'   // DOS
  |  '\r'        // Macintosh
  |  '\n'       // UNIX
  {
    $setType(Token.EOF_TYPE);
  }
 



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list