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