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

Mike Quilleash mike.quilleash at azuresolutions.com
Fri Apr 21 14:35:05 PDT 2006


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:45
To: Mike Quilleash; antlr-interest at antlr.org
Subject: 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 +0100
	From: mike.quilleash at azuresolutions.com
	To: 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 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.


________________________________

	<http:///> 


________________________________

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


More information about the antlr-interest mailing list