[antlr-interest] Strange behaviour - pls help.

John B. Brodie jbb at acm.org
Mon Mar 15 18:04:40 PDT 2010


Greetings!

(very sorry, I have broken the mail list threading. I deleted the
original message and then now decided to reply. Sorry. I could not find
a way to reply from the archive. Sorry)

Original message:

> Here's a problem I am having and can't figure out: I am trying to parse a hexadecimal string and zeroes
> 
> I build a very small example to highlight what the problem is.
> 
> Suppose an elemental log file for your check account. It just has credits and debits. Debits are identified by
> a prelude of '0001' and credits by '0002' Then comes the amount, up to $ 65535.
> 
> 
> We make a modest start with a $100 credit and a $50 debit. So our log is '0001006400020030'

__________________________________________________^^^___________________________________^^^^

$48 debit right? anyway, does not really matter i think.

> 
> But the input window shows 0001064000103, some zeroes are missing - which of course is not what we want.
____________________________________^^^^ huh? 0002 right?

I assume by `input window` you mean some interaction dialog from within ANTLRWorks? if so, maybe the
problem is with ANTLRWorks? and not the generated parser/lexer?

> 
> If we try with a $ 4369 deposit, the log is 0001111100020030.
> 
> Now the input window shows 0001111100010030. The credit is not altered, the debit is still losing zeroes.
_____________________________________^^^^ huh? ... and where is the loss of zeroes?

if this is really what you are seeing, I suggest that there is some issue outside of the ANTLR generated parser
and lexer, because the Tool generated code does not display any "input window" dialog.

> 
> My question is ¿Why do the zeroes get this "special treatment"? ¿How can I avoid it or work around it?
> 
> 
> I would really appreciate either an explanation or some pointers to learn what is wrong here.
....remainder snipped....

Have you tried running your generated code from the command line? As indicated above I think your problem is
outside of any ANTLR generated parser/lexer.

To prove that, please find attached a grammar that just works when Tool'd, compiled, and executed from the
command line. The linux command line I used is: java org.antlr.Tool Test.g ; javac *.java ; java TestParser

Hope this helps....
   -jbb
-------------- next part --------------
grammar Test;

options {
   output = AST;
   ASTLabelType = CommonTree;
}

@members {
   private static final String [] x = new String[] {
      "0001006400020030"
   };

   public static void main(String [] args) {
      for( int i = 0; i < x.length; ++i ) {
         try {
            System.out.println("about to parse:`"+x[i]+"`");
            TestLexer lexer = new TestLexer(new ANTLRStringStream(x[i]));
            CommonTokenStream tokens = new CommonTokenStream(lexer);

            TestParser parser = new TestParser(tokens);
            TestParser.start_return p_result = parser.start();

            CommonTree ast = p_result.tree;
            if( ast == null ) {
               System.out.println("resultant tree: is NULL");
            } else {
               System.out.println("resultant tree: " + ast.toStringTree());
            }
            System.out.println();
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
   }
}

start : (credit | debit)+ EOF!;

credit : '0001'^ HEX ;
debit  : '0002'^ HEX ;

fragment HEXIT : ('0'..'9') | ('a'..'f') | ('A'..'F') ;
HEX : HEXIT HEXIT HEXIT HEXIT ;


More information about the antlr-interest mailing list