[antlr-interest] StringTemplate test case: Capitalizing all words in text

Terence Parr parrt at cs.usfca.edu
Thu Sep 11 11:10:04 PDT 2008


On Sep 11, 2008, at 9:12 AM, Егор Абрамович wrote:

> Hello,
>
> In my test I am trying to create a grammar that should capitalize all
> words in text.
> Supposing text contains of english characters, punctuation marks and  
> numbers.
> Here is my grammar (with line numbers added):
>
> -------------------------------->8--- start of UpperAll.g
>
> 01: grammar UpperAll;
> 02: options {output=template; rewrite=true;}
> 03:
> 04: sent_en  :   ( en+=en_word PUNKT? INT? )+ //->
> template(words={$en}) "<words; separator=\" \">"
> 05:          ;
> 06:
> 07: en_word   :   ('A'..'Z' | 'a'..'z' )+ -> template(word={
> TestUpperAll.caps($en_word.text) }) "<word>"
> 08:           ;
> 09:
> 10: PUNKT     :   '.' | ',' | '!' | '?' | '(' | ')';
> 11: INT       :   '0'..'9'+ ;
> 12: NEWLINE  :   '\r'? '\n' ;
> 13: WS       :   (' '|'\t'|'\n'|'\r')+ {$channel = HIDDEN;} ;
>
> -------------------------------->8--- end of UpperAll.g
>
> When trying to generate, I am getting an error message:
>
> Check Grammar reported some errors:
> error(201):UpperAll.g:4:40: The following alternative can never be  
> matched: 2
>
> Here is console output:
>
> [10:40:53] warning(200): UpperAll.g:4:28: Decision can match input
> such as "PUNKT" using multiple alternatives: 1, 2

This makes no sense since en_word is not optional... oh, right. see  
next sentence.

note that you should not use .. range op in parser rules.  put EN_WORD  
in lexer.

The range of characters is somehow overlapping with parser tokens...   
Use En_word or something.
Ter



More information about the antlr-interest mailing list