grammar cfg; PIPE : '|'; PLUS : '+'; STAR : '*'; LPAREN : '('; RPAREN : ')'; LBRACE : '{'; RBRACE : '}'; COLON : ':'; SEMICOLON : ';'; NUMBER : '0'..'9'+ ; fragment LETTER : 'a'..'z' | 'A'..'Z' | '0'..'9' | '_' | '-' ; // Encompasses both terminals and nonterminals. Distinguishing them is done later. // This is way more broad than your typical programming language identifier: leading // digits are OK and hyphens are OK. SYMBOL : LETTER+ ; // For separating productions and rules ARROW : '->' | COLON ; WHITESPACE : '\r' | '\n' | ' ' { $channel = HIDDEN; } ; // Either a symbol or id:symbol, or a parenthisized expression. atom : SYMBOL | NUMBER COLON SYMBOL | LPAREN disjunction RPAREN ; term : atom (STAR|PLUS)? ; // Represents concatenation termString : term+ ; disjunction : termString (PIPE termString)* ; production : SYMBOL ARROW disjunction SEMICOLON ; rules : production+ ;