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

Mike Quilleash mike.quilleash at azuresolutions.com
Fri Apr 21 13:18:35 PDT 2006


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 Buch
Sent: 21 April 2006 21:13
To: antlr-interest at antlr.org
Subject: [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 newline
DEFAULT_QUOTE:   ( '\''! 
                   (~('\'') )+ 
                   '\''! )
    ;

protected
LINETERMINATOR
    :   '\r' '\n'   // DOS
    |   '\n'        // UNIX
    ;
    
WHITESPACE
 : LINETERMINATOR 
 |  ' '
 | '\t'
 | '\f'
 ;
}

 

Any help will be greatly appreciated.  Thank you.


________________________________

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060421/959abc41/attachment.html


More information about the antlr-interest mailing list