Answering old question re "scanning binary files"
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ajit_Dingankar
Posted On:   Sunday, July 10, 2011 01:04 PM

Sorry about using up a question to answer an old one (looks like you can't answer or reply to questions that are more than 3 months old ;-) viz., 1597320 I've updated the example mentioned there to work with Antlr v3; the grammar follows. grammar Data; class P extends Parser; file: ( sh=SHORT {System.out.println(sh.getText());} | st=STRING {System.out.println(""" + st.getText() + """);} )+ ; class L extends Lexer; options { charVocabulary='u0000'..'u00FF'; k=2; } // match the marker follow   More>>

Sorry about using up a question to answer an old one
(looks like you can't answer or reply to questions that
are more than 3 months old ;-) viz.,

1597320



I've updated the example mentioned there to work with
Antlr v3; the grammar follows.


			
grammar Data;

class P extends Parser;
file: ( sh=SHORT
{System.out.println(sh.getText());}
| st=STRING
{System.out.println(""" +
st.getText() + """);}
)+
;

class L extends Lexer;
options {
charVocabulary='u0000'..'u00FF';
k=2;
}

// match the marker followed by any two bytes
SHORT returns[int v]
: 'u0000'
// pack the bytes into a two-byte short
high=. {$v = high < <8;}
low=. {$v += low;}
{
// make a string out of the value
setText("" + $v);
}
;

STRING
: 'u0001' // begin string (discard)
(options {greedy=false;} :.)*
'u0002' // end string (discard)
{
// The "!" discard operator removed in v3. :-(
String v=getText();
setText(v.substring(1, v.length()-1));
}
;
   <<Less

Re: Answering old question re ""scanning binary files"

Posted By:   Ajit_Dingankar  
Posted On:   Sunday, July 10, 2011 03:30 PM

A few clarifying points:

  1. The ""k=2"" option for lexer is not necessary since
    the ""end string"" is only one character.

  2. The characters for ""begin/end string"" must be in
    the lower ASCII range u0000-u007F.

    This may be a bug/enhancement request for those who
    want to match real binary data streams! ☺

  3. The removal of ""!"" from v2 to v3 necessitates the
    setText action at the end of the STRING rule.

About | Sitemap | Contact