title: Why is my ANTLR4 parser grammar erroring 'no viable alternative at input'?
body: <p>When I run my grammar (lexer and parser) in powershell, it produces these errors:</p>
  <pre><code>line 1:6 no viable alternative at input 'global '
  </code></pre>
  <p>I've tried searching google and SO about the errors and the infinite loop, but I can't really understand anything (probably because I just started antlr a few days ago).</p>
  <p>Test file:</p>
  <pre><code>global a = 2
  </code></pre>
  <p>Lexer grammar:</p>
  <pre><code>lexer grammar Test;
  // tokens { SMT, SST, SCH, USM, USS, USC, NMT, NST, NCH, UNM, UNS, NAS, UNC, NAC, MLC, SLC, PXI, DXI, PXF, DXF, PXB, DXB, PXD, DXD, PXP, DXP, PRX, DEX, IMG, FLT, DBL, DCM, PRC, INT, CKW, KWR, RMH, IMH, NAN, IND, UND, NIL, NON, TRU, FLS, IDN, WSP, BKS, SMC, CMA, IVC }
  MST: '\'\'' -&gt; pushMode(multiline);
  SMT: SPS SDSQ ODQ*? SDSQ;
  SST: SPS '&quot;' NTD* '&quot;';
  SCH: [rf] SLSQ OQC SLSQ;
  USM: SPS SDSQ ODQ*? EOF;
  USS: SPS '&quot;' NTD* (EOL* ODC*)? EOF;
  USC: [rf] SLSQ OQC? EOF;
  NMT: SDSQ ODQ*? SDSQ;
  NST: '&quot;' NTD* '&quot;';
  NCH: SLSQ OQC SLSQ;
  UNM: SDSQ ODQ*? EOF;
  UNS: '&quot;' NTD* (EOL* ODC*)? EOF;
  NAS: SPS? '&quot;' NTD* EOL* ODC* '&quot;';
  UNC: SLSQ OQC? EOF;
  NAC: (('p'[f]?|[f]?'p')? SLSQ OQC+ SLSQ? | [rpf]? SLSQ OQC OQC+ SLSQ?);
  MLC: '##' NDH* '##'? -&gt; skip;
  SLC: '--' NEN* -&gt; skip;
  EOL: '\r'? '\n' | [\n\u000b\f\r\u0085\u2028\u2029];
  EML: '[]';
  EMS: '{}';
  PXI: NUM 'E' NUM 'i';
  DXI: NUM 'e' NUM 'i';
  PXF: NUM 'E' NUM 'f';
  DXF: NUM 'e' NUM 'f';
  PXB: NUM 'E' NUM 'd';
  DXB: NUM 'e' NUM 'd';
  PXD: NUM 'E' NUM 'D';
  DXD: NUM 'e' NUM 'D';
  PXP: NUM 'E' NUM 'p'
  | '+'? NUM 'E' NUM
  ;
  DXP: NUM 'e' NUM 'p'
  | '+'? NUM 'e' NUM
  ;
  PRX: '-'? NUM 'E' NUM;
  DEX: '-'? NUM 'e' NUM;
  IMG: NUM? 'i';
  FLT: NUM 'f'
  | DIGIT+ '.' DIGIT DIGITFQ DIGITTQ
  ;
  DBL: NUM 'd'
  | DIGIT+ '.' DIGITE DIGITFQ DIGITGQ
  ;
  DCM: NUM 'D'
  | DIGIT+ '.' DIGITE DIGITE DIGITEQ DIGITFQ DIGITGQ
  ;
  PRC: NUM 'p'
  | DIGIT+ '.' DIGITE DIGITE DIGITE DIGITE DIGIT*
  ; 
  INT: INTEGER | DIGIT;
  OPN: 'opn';
  OUT: 'out';
  OUTF: 'outf';
  PRINT: 'print';
  PRINTF: 'printf';
  LAMBDA: 'lambda';
  FUNC: 'func';
  ERR: 'err';
  ERRF: 'errf';
  ASSERT: 'assert';
  ASSERTF: 'assertf';
  FORMAT: 'format';
  SWITCH: 'switch';
  ABS: 'abs';
  ASCII: 'ascii';
  CALLABLE: 'callable';
  CHR: 'chr';
  DIR: 'dir';
  EVAL: 'eval';
  EXEC: 'exec';
  FILTER: 'filter';
  GET: 'get';
  HASH: 'hash';
  ID: 'id';
  INST: 'inst';
  SUB: 'sub';
  SUPER: 'super';
  MAX: 'max';
  MIN: 'min';
  OBJ: 'obj';
  ORD: 'ord';
  POWF: 'pow';
  REV: 'rev';
  REPR: 'repr';
  ROUND: 'round';
  FLOOR: 'floor';
  CEIL: 'ceil';
  MUL: 'mul';
  SORT: 'sort';
  ADD: 'add';
  ZIP: 'zip';
  WAIT: 'wait';
  SECS: 'secs';
  MILS: 'mils';
  BENCHMARK: 'benchmark';
  HAS: 'has';
  SIBLING: 'sibling';
  A: 'a';
  CHILD: 'child';
  OF: 'of';
  WHILE: 'while';
  FOR: 'for';
  DO: 'do';
  DEL: 'del';
  NEW: 'new';
  IMPORT: 'import';
  EXPORT: 'export';
  DEF: 'def';
  END: 'end';
  GLOBAL: 'global';
  BREAK: 'break';
  CONTINUE: 'continue';
  NOT: 'not';
  AND: 'and';
  OR: 'or';
  IN: 'in';
  CASE: 'case';
  DEFAULT: 'default';
  RETURN: 'return';
  TRY: 'try';
  EXCEPT: 'except';
  FINALLY: 'finally';
  ELIF: 'elif';
  IF: 'if';
  ELSE: 'else';
  AS: 'as';
  STRT: 'str';
  INTT: 'int';
  NUMT: 'num';
  DECIMALT: 'decimal';
  FLOATT: 'float';
  DOUBLET: 'double';
  PRECISET: 'precise';
  EXPNT: 'expn';
  CONST: 'const';
  REPEAT: 'repeat';
  UNTIL: 'until';
  THEN: 'then';
  CHART: 'char';
  GOTO: 'goto';
  LABEL: 'label';
  TYPET: 'type';
  USING: 'using';
  BOOLT: 'bool';
  PUBLIC: 'public';
  PROTECTED: 'protected';
  PRIVATE: 'private';
  CLASSD: 'class';
  SELF: 'self';
  STRUCTD: 'struct';
  FROM: 'from';
  XOR: 'xor';
  LISTD: 'list';
  TUPLED: 'tuple';
  DICTD: 'dict';
  SETD: 'set';
  IMAGT: 'imag';
  REALT: 'real';
  WHERE: 'where';
  PASS: 'pass';
  G_G: '_G';
  L_L: '_L';
  HEXT: 'hex';
  BINT: 'bin';
  OCTT: 'oct';
  MAP: 'map';
  ANYT: 'any';
  VOIDT: 'void';
  IS: 'is';
  A_FDV: '//=';
  A_CDV: '*/=';
  A_NOR: '||=';
  A_FAC: '=!=';
  A_LTE: '=&lt;=';
  A_GTE: '=&gt;=';
  A_EQL: '===';
  A_NEQ: '!==';
  A_CON: '..=';
  A_NXR: '$$=';
  A_BRS: '&gt;&gt;=';
  A_NND: '&amp;&amp;=';
  A_BLS: '&lt;&lt;=';
  A_DCL: '::=';
  A_CLD: ':.=';
  A_KUN: '=**';
  A_VUN: '=*';
  A_DOT: '.=';
  A_POW: '^=';
  A_NOT: '=!';
  A_BNT: '=~';
  A_LEN: '=#';
  A_PER: '=%';
  A_MUL: '*=';
  A_DIV: '/=';
  A_MOD: '%=';
  A_ADD: '+=';
  A_SUB: '-=';
  A_LET: '=&lt;';
  A_GRT: '=&gt;';
  A_BND: '&amp;=';
  A_BXR: '$=';
  A_BOR: '|=';
  A_TND: '?=';
  A_TOR: ':=';
  A_NML: '=';
  NND: '&amp;&amp;';
  NXR: '$$';
  NOR: '||';
  CLP: '()';
  SUP: '::';
  SIB: ':.';
  KUN: '**';
  INC: '++';
  DEC: '+-';
  FDV: '//';
  CDV: '*/';
  CON: '..';
  BLS: '&lt;&lt;';
  BRS: '&gt;&gt;';
  LTE: '&lt;=';
  GTE: '&gt;=';
  EQL: '==';
  NEQ: '!=';
  LPR: '(';
  RPR: ')';
  LBR: '[';
  RBR: ']';
  LBC: '{';
  RBC: '}';
  STR: '*';
  POW: '^';
  PLS: '+';
  MNS: '-';
  BNT: '~';
  EXC: '!';
  LEN: '#';
  PER: '%';
  DIV: '/';
  LET: '&lt;';
  GRT: '&gt;';
  BND: '&amp;';
  BXR: '$';
  BOR: '|';
  TND: '?';
  TOR: ':';
  DOT: '.';
  RMH: 'inf';
  IMH: 'inf*i';
  NAN: 'nan';
  IND: 'ind';
  UND: 'und';
  NIL: 'nil'
  | 'null';
  NON: 'none';
  TRU: 'true';
  FLS: 'false';
  IDN: AUL WORD*;
  WSP: (('\r'? '\n') | [\u0009\u0020\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000\n\u000b\f\r\u0085\u2028\u2029])+;
  BKS: [\u007f\u0008]+;
  SMC: ';';
  CMA: ',';
  SPC: ' ';
  IVC: .;
  mode multiline;
      SCE: ESCAPE -&gt; type(SCN);
      SND: (SDSQ | EOF) -&gt; popMode;
      SCN: '\'' ~'\'' | ~'\'';
  
  mode multicom;
      CCE: '\\##' -&gt; type(CCN);
      CND: ('##' | EOF) -&gt; popMode;
      CCN: '#' ~'#' | ~'#';
  
  fragment DIGITE: DIGITF DIGITF;
  fragment DIGITEQ: DIGITFQ DIGITFQ;
  fragment DIGITF: DIGITG DIGIT;
  fragment DIGITFQ: DIGITGQ DIGITQ;
  fragment SPS: 'pf' | 'fp' | [rpf];
  fragment OCH: ESCAPE | ALL;
  fragment OQC: ESCAPE | ~['];
  fragment ODQ: ESCAPE | SCN;
  fragment ODC: ESCAPE | ~[&quot;];
  fragment NDN: ESCAPE | NED;
  fragment AUL: [a-zA-Z_];
  fragment DIGIT: [0-9];
  fragment DECM: INTEGER '.' INTEGER;
  fragment WORD: AUL | DIGIT;
  fragment DIGITQ: DIGIT?;
  fragment DIGITG: DIGIT DIGIT DIGIT;
  fragment DIGITGQ: DIGIT? DIGIT? DIGIT?;
  fragment DIGITT: DIGIT DIGIT;
  fragment DIGITTQ: DIGIT? DIGIT?;
  fragment INTEGER: DIGITG ('_'? DIGITG)* | DIGIT+;
  fragment ALL: .;
  fragment TNQ: '\\' SLSQ | ~['];
  fragment TNDQQ: '\\&quot;' | ~[&quot;];
  fragment NDH: CCN;
  fragment NEN: ~[\n\u000b\f\r\u0085\u2028\u2029];
  fragment NED: ~[\n\u000b\f\r\u0085\u2028\u2029&quot;];
  fragment ENN: ESCAPE | NEN;
  fragment NTD: ESCAPE | NED;
  fragment HEX: DIGIT | [a-fA-F];
  fragment HEXQ: HEX?;
  fragment SPCF: ' ';
  fragment ESCAPE: '\\' ( 'x' HEX HEXQ | 'u' HEX HEXQ HEXQ HEXQ | 'U' HEX HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ | 'X' HEX HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ HEXQ| 'o' OCT OCTQ OCTQ | 'O' OCT OCTQ OCTQ OCTQ OCTQ OCTQ | ESCS );
  fragment ESCS: ['&quot;nrtbfv\\];
  fragment OCT: [0-7];
  fragment OCTQ: OCT?;
  fragment NUM: DECM | INTEGER;
  fragment SLDQ: [&quot;];
  fragment SLSQ: ['];
  fragment SDSQ: SLSQ SLSQ;
  </code></pre>
  <p>Parser grammar:</p>
  <pre><code>parser grammar TestP;
  options { tokenVocab=Test; }
  program: line (EOL line*) EOL? EOF;
  line: expression+ SMC* | expression* SMC+;
  expression: wsp* assign_expression wsp*;
  assign_expression: &lt;assoc=right&gt; GLOBAL var_list wsp* aop wsp* (var_list wsp* aop wsp*)* or_expression (wsp* CMA wsp* or_expression)*;
  or_expression: xor_expression (wsp* (NOR | OR) wsp* xor_expression)*;
  xor_expression: and_expression (wsp* (NXR | XOR) wsp* and_expression)*;
  and_expression: ternary (wsp* (NND | AND) wsp* ternary)*;
  ternary: &lt;assoc=right&gt; bit_or (wsp* (AND | TND) wsp* bit_or wsp* (OR | TOR) wsp* bit_or)?;
  bit_or: bit_xor (wsp* BOR wsp* bit_xor)*;
  bit_xor: bit_and (wsp* BXR wsp* bit_and)*;
  bit_and: eql_expr (wsp* BND wsp* eql_expr)*;
  eql_expr: com_expr (wsp* (EQL | IS | NEQ | IS wsp* NOT) wsp* com_expr)*;
  com_expr: shift_expr (wsp* (LET | LTE | GRT | GTE) wsp* shift_expr)*;
  shift_expr: con_expr (wsp* (BLS | BRS) wsp* con_expr)*;
  con_expr: one_expr (wsp* CON wsp* one_expr)*;
  one_expr: perc_expr (wsp* (PLS | MNS) wsp* perc_expr)*;
  perc_expr: fac_expr ((wsp* (STR | DIV | PER | FDV | CDV) wsp* fac_expr)+ | PER)*;
  fac_expr: unary_expr EXC?;
  unary_expr: (PLS | MNS | BNT | EXC | LEN | NOT)* crement_expr;
  crement_expr: pow_expr (wsp* (INC|DEC) wsp* pow_expr | INC | DEC)*;
  pow_expr: &lt;assoc=right&gt; unpack_expr (wsp* POW wsp* unpack_expr)*;
  unpack_expr: (STR | KUN)* brackets_expr;
  brackets_expr
      : tupled
      | LPR access_expr RPR
      | dictd
      | setd
      | LBC access_expr RBC
      | listd
      | LBR access_expr RBR
      | access_expr
  ;
  var_list: assign ( wsp* CMA wsp* assign)*;
  assign
      : idn
      | index
  ;
  access_expr
      : atom ((DOT | SUP | SIB) atom)*
  ;
  atom
      : litidn
      |datat
      | val
      | num
      | index
  ;
  literal
      : strt
      | num
  ;
  datat
      : listd
      | dictd
      | setd
      | tupled
  ;
  listd
      : LBR wsp* comma_separated wsp* RBR
      | EML
  ;
  dictd
      : LBC wsp* kvpair wsp*
      (
          CMA
          wsp*
          kvpair
          wsp*
      )+
      RBC
      | LBC wsp* kvpair wsp* RBC
  ;
  setd
      : LBC wsp* comma_separated wsp* RBC
      | EMS
  ;
  tupled: LPR wsp* comma_separated wsp* RPR;
  comma_separated
      : expression
      (
          wsp*
          CMA
          wsp*
          expression
      )*
  ;
  kvpair: expression wsp* TOR SPC wsp* expression;
  litidn
      : literal
      | idn
  ;
  typecast: typet LPR expression RPR;
  call:
      (
          dictd
          | idn
          | index
      )
      (
          CLP
      |
          tupled
      )
  ;
  strt
      : multi_line
      | single_line
      | char_string
  ;
  idn
      : IDN
      | A
  ;
  index: (datat | strt) LBR expression RBR;
  multi_line
      : SMT
      | USM
      | NMT
      | UNM
  ;
  single_line
      : SST
      | USS
      | NST
      | UNS
      | NAS
  ;
  char_string
      : SCH
      | USC
      | NCH
      | UNC
      | NAC
  ;
  num
      : exponential
      | non_exponential
  ;
  exponential
      : PXI
      | DXI
      | PXF
      | DXF
      | PXB
      | DXB
      | PXD
      | DXD
      | PXP
      | DXP
      | PRX
      | DEX
  ;
  non_exponential
      : IMG
      | FLT
      | DBL
      | DCM
      | PRC
      | INT
  ;
  typet
      : STRT
      | INTT
      | NUMT
      | DECIMALT
      | FLOATT
      | DOUBLET
      | PRECISET
      | EXPNT
      | CHART
      | IMAGT
      | REALT
      | HEXT
      | BINT
      | OCTT
      | LISTD
      | SETD
      | DICTD
      | TUPLED
      | TYPET
      | BOOLT
  ;
  wsp
      : WSP
      | EOL
  ;
  bks_or_wsp
      : wsp
      | BKS
      | SPC
  ;
  emd
      : EML
      | EMS
  ;
  sep
      : SMC
      | CMA
      | TOR
  ;
  /*
  kwr
      : WHILE
      | FOR
      | DO
      | DEL
      | NEW
      | IMPORT
      | EXPORT
      | DEF
      | END
      | GLOBAL
      | BREAK
      | CONTINUE
      | NOT
      | AND
      | OR
      | IN
      | CASE
      | DEFAULT
      | RETURN
      | TRY
      | EXCEPT
      | FINALLY
      | ELIF
      | IF
      | ELSE
      | AS
      | CONST
      | REPEAT
      | UNTIL
      | THEN
      | GOTO
      | LABEL
      | USING
      | PUBLIC
      | PROTECTED
      | PRIVATE
      | SELF
      | FROM
      | XOR
      | IMAGT
      | REALT
      | WHERE
      | PASS
      | G_G
      | L_L
      | MAP
      | IS
  ;
  ckw
      : OPN
      | OUT
      | OUTF
      | PRINT
      | PRINTF
      | LAMBDA
      | FUNC
      | ERR
      | ERRF
      | ASSERT
      | ASSERTF
      | FORMAT
      | SWITCH
      | ABS
      | ASCII
      | CALLABLE
      | CHR
      | DIR
      | EVAL
      | EXEC
      | FILTER
      | GET
      | HASH
      | ID
      | INST
      | SUB
      | SUPER
      | MAX
      | MIN
      | OBJ
      | ORD
      | POWF
      | REV
      | REPR
      | ROUND
      | FLOOR
      | CEIL
      | MUL
      | SORT
      | ADD
      | ZIP
      | WAIT
      | SECS
      | MILS
      | BENCHMARK
  ;
  */
  val
      : RMH // 'inf'
      | IMH // 'inf*i'
      | NAN // 'nan'
      | IND // 'ind'
      | UND // 'und'
      | NIL // 'nil'
      | NON // 'none'
      | TRU // 'true'
      | FLS // 'false'
  ;
  aop
      : A_FDV // '//='
      | A_CDV // '*/='
      | A_NOR // '||='
      | A_FAC // '=!='
      | A_LTE // '=&lt;='
      | A_GTE // '=&gt;='
      | A_EQL // '==='
      | A_NEQ // '!=='
      | A_CON // '..='
      | A_NXR // '$$='
      | A_BRS // '&gt;&gt;='
      | A_NND // '&amp;&amp;='
      | A_BLS // '&lt;&lt;='
      | A_DCL // '::='
      | A_CLD // ':.='
      | A_KUN // '=**'
      | A_VUN // '=*'
      | A_DOT // '.='
      | A_POW // '^='
      | A_NOT // '=!'
      | A_BNT // '=~'
      | A_LEN // '=#'
      | A_PER // '=%'
      | A_MUL // '*='
      | A_DIV // '/='
      | A_MOD // '%='
      | A_ADD // '+='
      | A_SUB // '-='
      | A_LET // '=&lt;'
      | A_GRT // '=&gt;'
      | A_BND // '&amp;='
      | A_BXR // '$='
      | A_BOR // '|='
      | A_TND // '?='
      | A_TOR // ':='
      | A_NML // '='
  ;
  opr
      : NND // '&amp;&amp;'
      | NXR // '$$'
      | NOR // '||'
      | CLP // '()'
      | SUP // '::'
      | SIB // ':.'
      | KUN // '**'
      | INC // '++'
      | DEC // '+-'
      | FDV // '//'
      | CDV // '*/'
      | CON // '..'
      | BLS // '&lt;&lt;'
      | BRS // '&gt;&gt;'
      | LTE // '&lt;='
      | GTE // '&gt;='
      | EQL // '=='
      | NEQ // '!='
      | LPR // '('
      | RPR // ')'
      | LBR // '['
      | RBR // ']'
      | LBC // '{'
      | RBC // '}'
      | STR // '*'
      | POW // '^'
      | PLS // '+'
      | MNS // '-'
      | BNT // '~'
      | EXC // '!'
      | LEN // '#'
      | PER // '%'
      | DIV // '/'
      | LET // '&lt;'
      | GRT // '&gt;'
      | BND // '&amp;'
      | BXR // '$'
      | BOR // '|'
      | TND // '?'
      | TOR // ':'
      | DOT // '.'
  ;
  /*
  inl
      : strt
      | num
      | ckw
      | kwr
      | val
      | idn
      | bks_or_wsp
      | sep
      | emd
      | aop
      | opr
      | typet
      | IVC
  ;
  */
  </code></pre>
  <p>EDIT: Changed parser grammar, changed errors</p>