title: ANTLR : No viable alternative error '{"type"'
body: <p>I know there are a lot of this questions and we've been going through them all but we can't seem to find a solution that fits our needs.</p>
  
  <p>We wrote a simple grammar for a javascript to Java converter, the lexer and the parser and we can't get it to consume the first token of our input file correctly.</p>
  
  <p>Here's the grammar:</p>
  
  <pre><code>lexer grammar JS2JAVALexer;
  
  STRING
     : '"' (ESC | ~ ["\\])* '"'
     ;
  fragment ESC
     : '\\' (["\\/bfnrt] | UNICODE)
     ;
  fragment UNICODE
     : 'u' HEX HEX HEX HEX
     ;
  fragment HEX
     : [0-9a-fA-F]
     ;
  NUMBER
     : '-'? INT '.' [0-9] + EXP? | '-'? INT EXP | '-'? INT
     ;
  fragment INT
     : '0' | [1-9] [0-9]*
     ;
  // no leading zeros
  fragment EXP
     : [Ee] [+\-]? INT
     ;
  // \- since - means "range" inside [...]
  WS
     : [ \t\n\r] + -&gt; skip
     ;
  
  OPENPAR : '(' ;
  CLOSEPAR : ')' ;
  OPENBRACES : '{' ;
  CLOSEBRACES : '}' ;
  OPENBRACKETS : '[' ;
  CLOSEBRACKETS : ']' ;
  TWOPOINTS : ':' ;
  QUOTATION_MARK : '"';
  COMMA : ',' ;
  TRUE : 'true';
  
  FALSE : 'false';
  NULL : 'null' ;
  
  TYPE : 'type';
  SOURCETYPE : '"sourceType"';
  BODY : '"body"';
  </code></pre>
  
  <p>After running it we get the error "line 2:4 no viable alternative at input '{"type"'</p>
  
  <p>This is our input file:</p>
  
  <pre><code>{
      "type": "Program",
      "body": [
          {
              "type": "FunctionDeclaration",
              "id": {
                  "type": "Identifier",
                  "name": "name"
              },
              "params": [
                  {
                      "type": "Identifier",
                      "name": "arg1"
                  },
                  {
                      "type": "Identifier",
                      "name": "arg2"
                  }
              ],
              "defaults": [],
              "body": {
                  "type": "BlockStatement",
                  "body": [
                      {
                          "type": "VariableDeclaration",
                          "declarations": [
                              {
                                  "type": "VariableDeclarator",
                                  "id": {
                                      "type": "Identifier",
                                      "name": "x"
                                  },
                                  "init": {
                                      "type": "Literal",
                                      "value": 1,
                                      "raw": "1"
                                  }
                              }
                          ],
                          "kind": "var"
                      },
                      {
                          "type": "VariableDeclaration",
                          "declarations": [
                              {
                                  "type": "VariableDeclarator",
                                  "id": {
                                      "type": "Identifier",
                                      "name": "y"
                                  },
                                  "init": {
                                      "type": "Literal",
                                      "value": 2,
                                      "raw": "2"
                                  }
                              }
                          ],
                          "kind": "var"
                      },
                      {
                          "type": "ReturnStatement",
                          "argument": {
                              "type": "BinaryExpression",
                              "operator": "+",
                              "left": {
                                  "type": "Identifier",
                                  "name": "x"
                              },
                              "right": {
                                  "type": "Identifier",
                                  "name": "y"
                              }
                          }
                      }
                  ]
              },
              "generator": false,
              "expression": false
          }
      ],
      "sourceType": "script"
  }
  </code></pre>
  
  <p>Parser's code:</p>
  
  <pre><code>parser grammar JS2JAVAParser;
  
  options {
      tokenVocab = JS2JAVALexer;
  }
  
  json
     : object
     | array
     ;
  
  object
     : OPENBRACES pair (COMMA pair)* CLOSEBRACES
     | OPENBRACES CLOSEBRACES
     ;
  
  left_operand 
      : QUOTATION_MARK left_name QUOTATION_MARK
      ;
  
  left_name 
      : TYPE 
      | BODY 
      | SOURCETYPE
      | DECLARATIONS
      | ID
      | INIT
      | OPERATOR
      | LEFT
      | RIGHT
      | VALUE
      | RAW
      | KIND
      ;
  
  pair
     : left_operand TWOPOINTS value
     ;
  
  array
     : OPENBRACKETS value (COMMA value)* CLOSEBRACKETS
     | OPENBRACKETS CLOSEBRACKETS
     ;
  
  value
     : QUOTATION_MARK value_name QUOTATION_MARK
     | object
     | array
     | TRUE
     | FALSE
     | NULL
     | STRING
     | IDENTIFIER
     | LITERAL
     | VAR
     | STRING
     ;
  
  value_name 
      : SCRIPT
      | PROGRAM
      ;
  </code></pre>
  
  <p>Sorry for such a long and repetitive question but we're running out of ideas. Thank you in advance for your patience guys.</p>