[antlr-interest] How to read input from a file?

Hitakshi Buch hbuch06 at hotmail.com
Tue Apr 25 19:37:19 PDT 2006


Ignoring whitespace was it.  
 
Mike, Thanks so much for your help and prompt responses!


Subject: RE: [antlr-interest] How to read input from a file?Date: Fri, 21 Apr 2006 22:35:05 +0100From: mike.quilleash at azuresolutions.comTo: hbuch06 at hotmail.com; antlr-interest at antlr.org



I think the lexer you have configured is considering whitespace to be a "real" token to be parsed.  I'm guessing you just want to skip these, but at the moment because they are not included in the parser it reads one DEFAULT_QUOTE rule and then finds a whitespace token and gives up.
 
First thing is put EOF token at the end of your start rule.  This will assert that there is nothing between the end of file and what was correctly parsed.  By default ANTLR will complete parsing and then just discard anything up to the end of file so you have to explicitly define the EOF token at the end of your start rule to make sure nothing has been missed.
 
Here's a typical whitespace lexer rule..
 
WS: ( ' '    | '\t'    | '\r' ( '\n' )? { newline(); }    | '\n' { newline(); }    ) { $setType( Token.SKIP ); }  ;
The most important type is 
 
$setType( Token.SKIP );
 
which says, "When this rule matches I want the token to be discarded".  As in it won't get passed to the parser at all, just thrown away.  If you want the LINETERMINATOR token to be parsed correctly rather than just thrown away then include it in your parser rules after the DEFAULT_QUOTE but inside the (...)+


From: Hitakshi Buch [mailto:hbuch06 at hotmail.com] Sent: 21 April 2006 21:45To: Mike Quilleash; antlr-interest at antlr.orgSubject: RE: [antlr-interest] How to read input from a file?

I tried the following start rule, but it still does not work:
 
startRule
 : (n:DEFAULT_QUOTE        {System.out.println("Matched default quote with " + n.getText());})+    ;I tried both + and *.  Am I doing it in the wrong place?
 
Thanks again for your help.


Subject: RE: [antlr-interest] How to read input from a file?Date: Fri, 21 Apr 2006 21:18:35 +0100From: mike.quilleash at azuresolutions.comTo: hbuch06 at hotmail.com; antlr-interest at antlr.org


Your startRule doesn't define any sort of one-or-more or zero-or-more construct so it will only parse the first line in the file then stop.  You need a (...)* or (...)+ wrapper in your startRule.


From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Hitakshi BuchSent: 21 April 2006 21:13To: antlr-interest at antlr.orgSubject: [antlr-interest] How to read input from a file?

I want to read a series for strings from a file and parse them.  My program only parses the first line in the input file though.  How can I have it parse the whole file?
 
My input file contents:
 
'Hello''ORACLE.dbs''Jackie''s raincoat''09-MAR-98'
 
I'm trying to read all strings that begin and end with a quote (').  So, my output should consist of the following:
Hello
ORACLE.dbs
Jackie's raincoat
09-MAR-98
 
INSTEAD, I only get the following:
Hello
 
My Main file looks like the following:
 
import java.io.*;
class Main {    public static void main(String[] args) {        try {            L lexer = new L(new FileInputStream("input2.inp"));            P parser = new P(lexer);            parser.setFilename("input2.inp");            parser.startRule();        } catch(Exception e) {            System.err.println("exception: "+e);        }    }
 
My .g file looks like:
 
class P extends Parser;
startRule
 : n:DEFAULT_QUOTE        {System.out.println("Matched default quote with: "+n.getText());}    ;
class L extends Lexer;
options { k=2;}
// one-or-more letters followed by a newlineDEFAULT_QUOTE:   ( '\''!                    (~('\'') )+                    '\''! )    ;
protectedLINETERMINATOR    :   '\r' '\n'   // DOS    |   '\n'        // UNIX    ;    WHITESPACE : LINETERMINATOR  |  ' ' | '\t' | '\f' ;}
 
Any help will be greatly appreciated.  Thank you.




_________________________________________________________________
Join the next generation of Hotmail and you could win the adventure of a lifetime
http://www.imagine-msn.com/minisites/sweepstakes/mail/register.aspx
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060425/8bbaa1a4/attachment.html


More information about the antlr-interest mailing list