Looking for suggestions on Lexer grammar
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sam_Walker
Posted On:   Tuesday, October 8, 2002 10:21 AM

I'm trying write a lexer for a language that uses * in the first column to indicate the entire line is a comment. It also uses '*' for multiplication, and "**" for exponentation. I've had some trouble with the grammar, and finally came up with this... protected MULT : '*' ; protected EXP : "**" ; STAR_COMMENT : ('*' { if (getColumn() == 2) { BitSet temp_bitset = new BitSet(); temp_bitset.add(' '); temp_bitset.add(' '); consumeUntil(temp_bitset); $setType(Token.SKIP); } else if (LA(1) == '*') { consume(); $setType(EXP); } else { $setType(MULT);    More>>

I'm trying write a lexer for a language that uses * in the first column to indicate the entire line is a comment. It also uses '*' for multiplication, and "**" for exponentation.


I've had some trouble with the grammar, and finally came up with this...

			
protected
MULT : '*' ;

protected
EXP : "**" ;

STAR_COMMENT : ('*' { if (getColumn() == 2)
{
BitSet temp_bitset = new BitSet();
temp_bitset.add('
');
temp_bitset.add('
');
consumeUntil(temp_bitset);
$setType(Token.SKIP);
}
else if (LA(1) == '*') {
consume();
$setType(EXP);
}
else {
$setType(MULT);
}
} )
;

It works, but I keep thinking there's gotta be a better way.

So I thought someone might have a suggestion or two.

BTW, I have experience with C/C++ and VB, but Java and ANTLR are new to me. Actually, ANTLR is the reason I've started learning Java.

Thanks.

   <<Less
About | Sitemap | Contact