Adrian Ber wrote:
Hi all,

I'm doing another attempt with the hope that somebody knows the answer and he/she wants to share.
I have an HTML grammar and I have a lexer rule for strings:

STRING    :    '"' (ESC|~('"'|'\\'|'\n'|'\r'))* '"'
        | '\'' (ESC|~('\''|'\\'|'\n'|'\r'))* '\''
        ;

But if I have a text in html like
    I'm just a text
then the lexer will try to identify a string and it will generate an error.

How can I solve this?

Add a predicate to make sure the terminating quote is present, if it is not then the alt will fail and you can let the lexer return just the single quote. HTML is not an easy thing to parse using an ANTLR grammar; I would not suggest this as your first grammar :-(

STRING   
        : (DQUOTE_STRING)?=> DQUOTE_STRING
        | (SQUOTE_STRING)?=> SQUOTE_STRING
        | '"' { $type = DQUOTE; }
        | '\' { $type = SQUOTE; }
        ;

fragment SQUOTE : '\'' ;
fragment DQUOTE : '"';

fragment SQUOTE_STRING
  : '\'' (ESC|~('\''|'\\'|'\n'|'\r'))* '\''
  ;

fragment DQUOTE_STRING
  : '"' (ESC|~('"'|'\\'|'\n'|'\r'))* '"'
  ;


It is always worth experimenting
Jim

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to