[antlr-interest] Solution to " How to have strings enclosed in " and/or ""

matthew ford Matthew.Ford at forward.com.au
Sat Dec 11 22:08:17 PST 2004


This is what I came up with which seems to work

The interesting bit is
  | ~('\n'|'\r')
        { if ( (LA(1) == '"') || ( (LA(1) == '&')
              && (LA(2) == 'q')
              && (LA(3) == 'u')
              && (LA(4) == 'o')
              && (LA(5) == 't')
              && (LA(6) == ';'))
        ) {break;} }
where the break; is used to break out of antlr' ( )* block

Note: in my case no other token start with '&'
matthew

/* *********************  antlr lexer code *******************/
STRING
options {
    paraphrase="STRING";
}
  :  {  ( (LA(1) == '"') || ( (LA(1) == '&')
              && (LA(2) == 'q')
              && (LA(3) == 'u')
              && (LA(4) == 'o')
              && (LA(5) == 't')
              && (LA(6) == ';'))
        ) }?
   STRING_PART
 (STRING_PART)*
 { String t = $getText;

    if (t.startsWith(""")) {
       t = t.substring(""".length());
    } else {
        t=t.substring(1);
    }
   $setText(t);
 }
 ;

protected
STRING_PART
 : {  ( (LA(1) == '"') || ( (LA(1) == '&')
              && (LA(2) == 'q')
              && (LA(3) == 'u')
              && (LA(4) == 'o')
              && (LA(5) == 't')
              && (LA(6) == ';'))
        ) }?
        ('"' | ('&' 'q' 'u' 'o' 't' ';')) (
   options {
    generateAmbigWarnings=false;
   }
  : '\r' '\n'  {newline();}
  | '\r'   {newline();}
  | '\n'   {newline();}
  | ~('\n'|'\r')
        { if ( (LA(1) == '"') || ( (LA(1) == '&')
              && (LA(2) == 'q')
              && (LA(3) == 'u')
              && (LA(4) == 'o')
              && (LA(5) == 't')
              && (LA(6) == ';'))
        ) {break;} }
  )*
 ('"' ! | ('&' ! 'q' ! 'u' ! 'o' ! 't' ! ';' !))
 ;





 
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