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

Hitakshi Buch hbuch06 at hotmail.com
Fri Apr 21 13:45:20 PDT 2006


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.


_________________________________________________________________
Search on the go: Try Windows Live(tm) Search for Mobile beta
http://www1.imagine-msn.com/minisites/mobile/Default.aspx?locale=en-us
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060421/a6d2da1a/attachment.html


More information about the antlr-interest mailing list