title: Using ANTLR Parser and Lexer Separatly
body: <p>I used ANTLR version 4 for creating compiler.First Phase was the Lexer part. I created "CompilerLexer.g4" file and putted lexer rules in it.It works fine.</p>
  
  <p>CompilerLexer.g4:</p>
  
  <hr>
  
  <pre><code>lexer grammar CompilerLexer;
  
  INT         :   'int'   ;   //1
  FLOAT       :   'float' ;   //2
  BEGIN       :   'begin' ;   //3
  END         :   'end'   ;   //4
  To          :   'to'    ;   //5
  NEXT        :   'next'  ;   //6
  REAL        :   'real'  ;   //7
  BOOLEAN     :   'bool'  ;   //8
  .
  .
  .
  NOTEQUAL    :   '!='    ;   //46
  AND         :   '&amp;&amp;'    ;   //47
  OR          :   '||'    ;   //48
  POW         :   '^'     ;   //49
  ID          : [a-zA-Z]+ ;   //50
  
  
  
  
  WS
  :   ' ' -&gt; channel(HIDDEN)  //50
  ;
  </code></pre>
  
  <hr>
  
  <p>Now it is time for phase 2 which is the parser.I created "CompilerParser.g4" file and putted grammars in it but have dozens warning and errors.</p>
  
  <p>CompilerParser.g4:</p>
  
  <hr>
  
  <pre><code>parser grammar CompilerParser;
  
  options {   tokenVocab = CompilerLexer; }
  
  STATEMENT   :   EXPRESSION SEMIC
          |   IFSTMT
          |   WHILESTMT
          |   FORSTMT
          |   READSTMT SEMIC
          |   WRITESTMT SEMIC
          |   VARDEF SEMIC
          |   BLOCK
          ;
  
  BLOCK       : BEGIN STATEMENTS END
          ;
  
  STATEMENTS  : STATEMENT STATEMENTS*
          ;
  
  EXPRESSION  : ID ASSIGN EXPRESSION
          | BOOLEXP
          ;
  
  RELEXP      : MODEXP (GT | LT | EQUAL | NOTEQUAL | LE | GE | AND | OR) RELEXP
          | MODEXP
          ;
  
  .
  .
  .
  
  VARDEF      : (ID COMA)* ID COLON VARTYPE
          ;
  
  VARTYPE     : INT
          | FLOAT
          | CHAR
          | STRING
          ;
  compileUnit
  :   EOF
  ;
  </code></pre>
  
  <hr>
  
  <p>Warning and errors:</p>
  
  <blockquote>
    <ul>
    <li>implicit definition of token 'BLOCK' in parser</li>
    <li>implicit definition of token 'BOOLEXP' in parser</li>
    <li>implicit definition of token 'EXP' in parser   </li>
    <li>implicit definition of token 'EXPLIST' in parser</li>
    <li>lexer rule 'BLOCK' not allowed in parser</li>
    <li>lexer rule 'EXP' not allowed in parser</li>
    <li>lexer rule 'EXPLIST' not allowed in parser</li>
    <li>lexer rule 'EXPRESSION' not allowed in parser</li>
    </ul>
  </blockquote>
  
  <p>Have dozens of these warning and errors. What is the cause?</p>
  
  <p>General Questions: What is difference between using combined grammar and using lexer and parser separately? How should join separate grammar and lexer files?</p>