title: Antlr4 - no viable alternative at input
body: <p>I am trying to create a simple HOCON parser (started from the existing JSON one).</p>
  
  <p>The grammar is defined as:</p>
  
  <pre><code>/** Taken from "The Definitive ANTLR 4 Reference" by Terence Parr */
  
  // Derived from http://json.org
  grammar HOCON;
  
  hocon
     : value
     | pair
     ;
  
  obj
     : object_begin pair (','? pair)* object_end
     | object_begin object_end
     ;
  
  pair
     : STRING KV? value {fmt.Println("pairstr",$STRING.GetText())}
     | KEY KV? value {fmt.Println("pairkey",$KEY.GetText())}
     ;
  
  array
     : array_begin value (',' value)* array_end
     | array_begin array_end
     ;
  
  value
     : STRING {fmt.Println($STRING.GetText())}
     | REFERENCE {fmt.Println($REFERENCE.GetText())}
     | RAWSTRING {fmt.Println($RAWSTRING.GetText())}
     | NUMBER {fmt.Println($NUMBER.GetText())}
     | obj
     | array
     | 'true'
     | 'false'
     | 'null'
     ;
  
  COMMENT
     : '#' ~( '\r' | '\n' )* -&gt; skip
     ;
  
  STRING
     : '"' (ESC | ~ ["\\])* '"'
     | '\'' (ESC | ~ ['\\])* '\''
     ;
  
  RAWSTRING
     : (ESC | ALPHANUM)+
     ;
  
  KEY
     : ( '.' | ALPHANUM | '-')+
     ;
  
  REFERENCE
     : '${' (ALPHANUM|'.')+ '}'
     ;
  
  fragment ESC
     : '\\' (["\\/bfnrt] | UNICODE)
     ;
  
  
  fragment UNICODE
     : 'u' HEX HEX HEX HEX
     ;
  
  fragment ALPHANUM
     : [0-9a-zA-Z]
     ;
  
  fragment HEX
     : [0-9a-fA-F]
     ;
  
  KV
     : [=:]
     ;
  
  array_begin
     : '[' { fmt.Println("BEGIN [") }
     ;
  
  array_end
     : ']' { fmt.Println("] END") }
     ;
  
  object_begin
     : '{' { fmt.Println("OBJ {") }
     ;
  
  object_end
     : '}' { fmt.Println("} OBJ") }
     ;
  
  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
     ;
  </code></pre>
  
  <p>the error is:</p>
  
  <pre><code>line 2:2 no viable alternative at input '{journal'
  pairkey akka.persistence
  </code></pre>
  
  <p>the sample input that gives the error is:</p>
  
  <pre><code>akka.persistence {
    journal {
      # Absolute path to the journal plugin configuration entry used by
      # persistent actor or view by default.
      # Persistent actor or view can override `journalPluginId` method
      # in order to rely on a different journal plugin.
      plugin = ""
    }
  }
  </code></pre>
  
  <p>however if I will update it to use quoted strings:</p>
  
  <pre><code>akka.persistence {
    'journal' {
      # Absolute path to the journal plugin configuration entry used by
      # persistent actor or view by default.
      # Persistent actor or view can override `journalPluginId` method
      # in order to rely on a different journal plugin.
      'plugin' = ""
    }
  }
  </code></pre>
  
  <p>everything works as expected.</p>
  
  <p>Looks like I miss something in the <code>KEY</code> definition, but I can't really find out what exactly.</p>
  
  <p>The Go code to test it out is:</p>
  
  <pre><code>package main
  
  import (
      "github.com/antlr/antlr4/runtime/Go/antlr"
      "go-hocon/parser"
  )
  
  func main() {
      is, _ := antlr.NewFileStream("test/simple1.conf")
  
      lex := parser.NewHOCONLexer(is)
      p := parser.NewHOCONParser(antlr.NewCommonTokenStream(lex, 0))
      p.BuildParseTrees = true
      p.Hocon()
  }
  </code></pre>