title: ANTLR - allow any char between to keywords
body: <p>I'd like to define a grammar for a simple language.</p>
  
  <p>The language allows some sort of assignments.</p>
  
  <p>Example</p>
  
  <pre><code>keyworda: this is the 1 keyword-A
  keywordb: this is the second keywordb
  ...
  </code></pre>
  
  <p>The thing is, that after the keyword and the <code>':'</code> any char should be possible (the keyword, too)</p>
  
  <p>I've tried many thing but I think I'm still not that into the lexer and parser thinking...</p>
  
  <p>My last idea failed:</p>
  
  <pre><code>rule 
    :  'keyworda' ':' anychar* 'keywordb' ':' anychar* EOF 
    ;
  
  anychar
    :  .
    ;
  
  NEWLINE
    :  ('\r'? '\n') {$channel=HIDDEN;}
    ;
  </code></pre>
  
  <h2>EDIT</h2>
  
  <p>First of all: thanks for your answer!</p>
  
  <p>I read through the manual and looked the tutorials by scott stanchfield.</p>
  
  <p>The problem is, that I don't get the "anychar" thing!</p>
  
  <p>You are right, the grammar I postet above was wrong because I was in hurry.</p>
  
  <p>A better try is this ahead. Problem is still, that the Tokenizer recognizes e.g. keyworda in a definition ala</p>
  
  <pre><code>keyworda : this is keyworda.
  keywordb : this is another key!
  ...
  </code></pre>
  
  <p>The grammar:</p>
  
  <pre><code>rule
      :   KEYA ':' STRING_LITERAL* NEWLINE
          keybdefinition*
           EOF
      ;
  
  keybdefinition
      :   KEYB ':' STRING_LITERAL* NEWLINE
      ;
  
  
  KEYA: 'keyworda';
  KEYB:'keywordb';
  STRING_LITERAL: 'a'..'z' | 'A'..'Z' | '0'..'9' | ':' | '.' | '&amp;' | '/' | '\\' | ';';
  
  NEWLINE: '\r'? | '\n'; 
  SPACE:  (' ' | '\t') {$channel=HIDDEN;};
  </code></pre>
  
  <h2>EDIT II</h2>
  
  <p>Oh my god, it's quiet obvious to do it the way you explained it. Don't know why I didn't get it myself! Great thanks Tim for your explanation!</p>
  
  <p>I've got just one more question left:
  If I define my tokens for the lexer and my grammar for the parser. Is it the common way to check the semantic in the tree parser or in the parser itself?</p>
  
  <p>For example, let's assume I have the same grammar defined as you posted.</p>
  
  <pre><code>keyworda : ab
  keywordb : xy
  keyworda : ab1
  keywordb : xy1
  ...
  </code></pre>
  
  <p>Now I want to check if after every keyworda definition a keywordb is defined.
  Later I do want to check if the meaning if the value is proper.
  Lets assume we do have a keyword extends : 'keyword value' and I need to check if 'keyword value' is already defined.</p>
  
  <p>I could do this in 2 ways: First, change your grammar rule for the parser and add java code for the checkings right there. Second the grammar stays as it is and I define a tree parser grammer to check those conditions.</p>
  
  <p>I do not really know which way is the better one and what the advantages or disadvantages are...</p>
  
  <p>Thanks a lot for your help</p>