[antlr-interest] How to match a phrase i.e. multiple words?

Swaroop C H swaroopch at gmail.com
Sat Feb 14 07:18:49 PST 2009


Hello,

I'm trying to implement something like gmail-search where the user can
enter key:word or key:"more than one word" -> where the multiple words
are enclosed in double quotes (would like to remove that constraint if
possible, but I don't know enough of ANTLR to do that yet)

So far I have the following which correctly matches `description:"something"`:

    grammar Search;

    options {
        language=Python;
    }

    main
        : k=WORD ':' v=PHRASE { print $k.text, '=', $v.text }
        ;

    PHRASE
        : '"' WORD '"' { $text = $WORD.text }
        ;

    WORD
        : ( 'a'..'z' | 'A'..'Z' | '.' )+
        ;

    WHITESPACE
        :   (' '|'\t'|'\n'|'\r')+ { self.skip() }
        ;


The problem is that I'm unable to proceed from here. If I put

    PHRASE
        : '"' w=WORD+ '"' { $text = $w.text }
        ;


Then I get the following error:

    ANTLR Parser Generator  Version 3.1.1
    line 1:22 mismatched character u' ' expecting u'"'
    line 1:28 required (...)+ loop did not match anything at character '<EOF>'
    line 1:23 missing PHRASE at u'todo'
    description = <missing PHRASE>


I am not sure how to match a phrase as multiple words.

Any help would be greatly appreciated. Please be gentle on a newbie :-)

Regards,
Swaroop


More information about the antlr-interest mailing list