grammar EquationSystem; options { language=C; output=AST; backtrack=true; } tokens { PLUS = '+' ; SUB = '-' ; MULT = '*' ; COMMA = ';' ; RANGE = 'range' ; GUARD = 'guard' ; GREATER_EQUAL = '>=' ; QUESTION_MARK = '?' ; MAXIMUM = 'max' ; MINIMUM = 'min' ; NEWLINE = '\n' ; } equation_system : equation ( NEWLINE! + equation )* (NEWLINE!)* ; equation : VARIABLE '='! maxExpr ; maxExpr : MAXIMUM^ '('! minExpr ( ','! minExpr )* ')'! | minExpr ; minExpr : MINIMUM^ '('! maxExpr ( ','! maxExpr )* ')'! | expr ; expr : '(' expr GREATER_EQUAL expr QUESTION_MARK expr ')' -> ^(GUARD expr expr expr) | term ( (PLUS | MULT | COMMA)^ expr )* ; term : '[' NUMBER ',' NUMBER ']' -> ^( RANGE NUMBER NUMBER ) | VARIABLE | '('! expr ')'! | SUB^ term ; NUMBER : (SUB) (DIGIT)+ | (DIGIT)+ ; VARIABLE: (LETTER) (LETTER | DIGIT)* ; WHITESPACE : ( '\t' | ' ' | '\u000C' )+ { $channel = HIDDEN; } ; fragment DIGIT : '0' .. '9' ; fragment LETTER: 'a' .. 'z' ;