[antlr-interest] Re: Help on syntactic predicate
lgcraymer
lgc at mail1.jpl.nasa.gov
Thu Dec 11 20:23:15 PST 2003
Kevin--
Add a "tokens" section to the lexer which defines ALPHANUM as a
valid token type. That should help.
--Loring
--- In antlr-interest at yahoogroups.com, "kelvin7600"
<kelvin7600 at y...> wrote:
> Hi all
>
> I just started on learning ANTLR, and ran into a problem with the
use
> of syntactic predicate. The .g file below defines just a few
tokens :
> WORD(string of alphabets) and NUMBER(string of digits). I tried to
> declare a new token called ALPHANUM, which is defined to be some
> alphabets followed by some digits.
>
> //Lexer
>
> WORD : ((ALPHABET)+) => (ALPHABET)+ (DIGIT)+ {$setType(ALPHANUM);}
> | (ALPHABET)+
> ;
>
> //Parser
> alphanum: (an:ALPHANUM { cout << "<alphanum>" << an->getText
().c_str
> () << "</alphanum>\n"; })
> ;
>
> The .g file compiles without a warning but when I run it, it
crashes
> with a debug warning "This application has requested the Runtime
to
> terminate it in a unusual way." This only happened after I added
the
> above code segments to the grammer file. Is there anything wrong
in
> the way I wrote my code? Thanks!
>
> Full grammer file :
> ------------------------------------------------------------------
> header {
> #include <iostream>
> using namespace std;
>
> }
>
> options {
> language="Cpp";
> }
>
> class SelfParser extends Parser;
> options {
> genHashLines = true; // include line number
> information
> k=2;
> }
>
> sentence: {cout << "<sentence>\n";}
> (word|number)+ FULLSTOP
> {cout << "</sentence>\n";}
> ;
>
> alphanum: (an:ALPHANUM { cout << "<alphanum>" << an->getText
().c_str
> () << "</alphanum>\n"; })
> ;
>
> word: (w:WORD { cout << "<word>" << w->getText().c_str()
> << "</word>\n"; })
> ;
>
> number: (n:NUMBER { cout << "<number>" << n->getText
().c_str
> () << "</number>\n"; })
> ;
>
> class SelfLexer extends Lexer;
> options {
> k=3;
> charVocabulary='\u0000'..'\u007F'; //allow ascii
> }
>
>
> WORD : ((ALPHABET)+) => (ALPHABET)+ (DIGIT)+ {$setType
> (ALPHANUM);}
> | (ALPHABET)+
> ;
>
>
>
> NUMBER : (DIGIT)+
> ;
> FULLSTOP : '.'
> ;
>
> protected
> ALPHABET : ('a'..'z'|'A'..'Z') ;
>
> protected
> DIGIT : ('0'..'9') ;
>
>
>
> WS : ( ' '
> | '\r'
> | '\r' '\n'
> | '\t'
> ) {_ttype = ANTLR_USE_NAMESPACE(antlr)
> Token::SKIP; }
> ;
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list