[antlr-interest] DFA bytecode generation successfully stitched into ANTLR 3.0

Terence Parr parrt at cs.usfca.edu
Sun Oct 31 15:08:20 PST 2004


Howdy,

Just a small squeal of delight as I ran the following simple grammar:

lexer grammar T;

A : ('a')* 'b' {System.out.println("alt 1");}
   | ('a')* 'c' {System.out.println("alt 2");}
   ;

into the 3.0 prototype. Note the arbitrary left common prefix that 
would piss of 2.0 to no end.  It generated a DFA.class file for the 
cyclic DFA used to predict which alt of rule A will succeed.  To test 
it out, you just have to call nextToken on the generated lexer:

public class Test {
         public static void main(String[] args) {
                 T lexer = new T(new ANTLRStringStream(args[0]));
                 lexer.nextToken();
         }
}

It correctly executes the grammar actions for input such as 'aaaaab' 
and 'aac' :)

The generated lexer invokes the DFA like this:

     public void mA()
         {
             setType(A);
             int alt3=0;

             alt3 = DFA.DFA3(input); // go do DFA for decision 3
             switch (alt3) {
                 case 1 : // means alt 1 will succeed
                 ...
                 case 2 :
                 ...
             }
         }

The DFA.class file is 600 bytes including the constant pool etc... 
There are 146 bytes of DFA bytecodes.

Naturally, this is only the first one that I tried; I'm sure there are 
problems.  I know it's not doing EOF yet properly, for example.

Anyway, nice little detour into bytecode generation, but it sure looks 
like decent solution--only way to get Java doing DFAs well as far as I 
can tell.

Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

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





More information about the antlr-interest mailing list