[antlr-interest] Re: Antlr grammar to parse Java classfile?

Sinan sinan.karasu at boeing.com
Thu Dec 6 10:27:15 PST 2001


Ric Klaren wrote:
> 
[...]
> 
> Not 100% sure so of the top of my head... but if the rule is invoked
> somewhere in the path of a syntactic predicate will it work as well? E.g.
> if guessing > 0 ?
> 
> I have some vague recollection of these things biting in some cases.
> 
> Ric

 
I use the following for parsing hollerith fields in Fortran.

(Quote: In Mathematics , you know you have arrived when you name is not
 capitalized....)

One caveat in the following when a "character" is returned from the
lexer it also
returns all the following white space.

In other words

1 2 3    f or ma t ( 1 2 hH ello there

will return:

"1 "
"2 "
"3    "
"f "
"o"
"r "

etc....

which allows me to write rules like

format_KW: F O R M A T ;

I use extensive lookahead, since in Fortran you don't know that you got
it 
until you got it. 


in other words

       real format(300)
       integer J(10)
       integer ca llmysub(10)
       integer X5H=1
7      format(X5H)=J(2)

123    call mysub(3)=format(1)
       write(6,7)


      are valid statements....

      loverly language...

      but It works....

so here is the code....




hollerith_constant!
	//options {defaultErrorHandler=false;}
	{String scount="0";String hol="";}
	:
	scount=int_const_int[scount] h:H
	(
	
(counted_hollerith_field[Integer.parseInt(scount)-(h.getText().length()-1)])=>
	
hol=counted_hollerith_field[Integer.parseInt(scount)-(h.getText().length()-1)]
		{
			//System.out.println("What we saw
was:"+h.getText()+":"+scount+":"+hol);
			if(h.getText().length()>1){	// prepend the trailing blanks from H
				hol=h.getText().substring(1,h.getText().length())+hol;
			}
			hol=hol.substring(0,Integer.parseInt(scount));
			//System.out.println("Returning:"+hol+":");
		}
	)?	{  ## = #[HOLLERITH_CONST]; ##.setText(hol); }
	;
	
counted_hollerith_field![int count] returns [String s]
	{
		int decrease=LT(1).getText().length();
		s=LT(1).getText();String r;
	}
	:
	 	{0<count}?
		( quotedchar | DQ | SQ )
		(
			{0<(count-decrease)}?
			(
				r=counted_hollerith_field[count-decrease]
				{s=s+r;}
			)
		)?
	|
	;
	
int_const_int! [String tohere] returns [String added]
	{ 	added=tohere;
		if("0123456789".indexOf(LT(1).getText())>=0){
			added=tohere+LT(1).getText();
		}
	}
	:

quotedchar	:	digit_char	|	letter	|	specialchar	;

DQ and SQ are "double/single quote"

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list