[antlr-interest] A newbie question and is this mailing list a black hole for me?

Foolish Ewe foolishewe at hotmail.com
Mon Oct 23 08:46:19 PDT 2006


Hello All:

I'm still waiting to see any of my earlier posts acknowledged by this 
mailing list, however,
they aren't showing up, so I am not sure what is happening (it's been 
several business days).
If anyone gets this, please reply directly to me (or cc me and send to the 
mailing list if it
is of general interest). I have started development using ANTLR and 
advocated it to my
team as (1) generating java source, (2) being hardened by widespread use and
(3) actively supported.  Please back me up on the actively supported part, I 
don't want
to look foolish  :-).

For my job, I am writing a tool to parse a language, that for historical 
reasons has
what I'll call "undelimited strings", which are positional string parameters 
with white space delimiiters.
The problem becomes that if the undelimited string has a prefix that matches 
a keyword,
then the scanner will call it a keyword and not a string (which is 
understandable but not the
behavior I want).  I would like to control when keywords are recognized by 
the lexer, and
I would like to activate/deactivate this control in parser actions.  In the 
lexer, I added a public
boolean flag "recognizeKeyWords" which appears to work in a toy example, 
however, I don't know
how to access the extended lexer class resulting from my ANTLR file to 
control this flag.
Is there a way to do this, if so, what is the java/ANTLR syntax?

As an example, please see a small ANTLR file and Java example are appended 
to the end of this
message.

Thanks:

Bill M.

******************BEGIN ANTLR**************************************
//My play area for diagnosing strange ANTLR symptoms
//Version History: 1.0 WAM created


// WAM - Need to add some boilerplate to let Antlr generated files know that 
they are part of the ZTestParser package
header{
	package testing;
}

class P extends Parser;

// Parser options
options{
	k = 2; // Token stream lookahead, remember ANTLR uses LL(k) parsing
}

// Antlr requires Terminals have names beginning with uppercase letters, 
Nonterminals should use lowercase I guess
startRule
	:
		// the actual prefix tokens are different in practice
		getstring:GETSTRING
		// I would like to do something like the following actions where lexer is 
a type L object used in lexing
		// This is not the right syntax for this, but it shows the general idea
		// {this.lexer.recognizeKeyWord = false;}
		strval:NONEMPTYSTRING
		// {this.lexer.recognizeKeyWord = true;}
		nl1:NEWLINE sr1:startRule// breaks if the user types in "dun\n" where \n 
is newline
	|
		month:MONTH nl2:NEWLINE sr2:startRule
	|
		// added for testing, but won't work for my requirements.
		toggle:TOGGLE nl3:NEWLINE sr3:startRule
	|
		end:END nl4:NEWLINE
	;

class L extends Lexer;

// Lexer options
options{
	k=3; // lookahead (need 2 for new line, 3 should be enough for months)
	charVocabulary='\u0000'..'\u007F'; // Only support printable ASCII 
characters, don't try fancy unicode stuff
	// case sensitivitity turned off
	caseSensitiveLiterals=false;
	caseSensitive=false;
}

*****************END ANTLR BEGIN 
JAVA*****************************************

package testing;
import java.io.*;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			System.out.println("Enter a string for the test parser (note this is for 
simple ANTLR test cases)");

			L lexer = new L(new DataInputStream(System.in));


			System.out.println("After lexer instantiated before Parser 
instantiation");
			P parser = new P(lexer);
			System.out.println("After Parser instantiation before StartRule");
			parser.startRule();
			System.out.println("After startRule: Done?");
		} catch(Exception e) {
			System.err.println("exception: "+e);
		}
	}

}

_________________________________________________________________
Try the next generation of search with Windows Live Search today!  
http://imagine-windowslive.com/minisites/searchlaunch/?locale=en-us&source=hmtagline



More information about the antlr-interest mailing list