Title: How to handle Quoted Strings?
Weiler-Thiessen, David, SASKATOON, Engineering wrote:

Hi

I am having trouble adding Lexer/Parser rules to a grammar that will deal with quoted strings.

A phrase I would like to parse is:

    create register FAULT_CODE initial value '301'

I am having trouble with the quoted string '301'.  When I build my AST, I would like to have just the inner value, 301 passed, but when I tried to get the text in my tree grammar, I get the full token with quotes, i.e. '301'.

This is a portion of my Parser/Lexer grammar where I am trying to define this.

stringValue
        :       SINGLE_QUOTE! (~SINGLE_QUOTE|BACK_SLASH SINGLE_QUOTE)* SINGLE_QUOTE!
        ;

Don't deal with it in the parser, use a lexer rule for single quotes, then either use the trick below or just set the text of the token ( $text = $text.substring( ... etc) to remove the quotes. It is returning the quotes in your tree parser because you are asking for the text of the subtree, which includes the SINGLE_QUOTE tokens.

STRING_LITERAL
@init
{
    int    theStart = $start;
}
        : '\''
            {
                // Start the token text here, when we don't want the opening '
                //
                theStart = getCharIndex();
            
            }

             ~'\''*
            { $start = theStart; emit(); }
            '\''
         ;


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