summaryrefslogtreecommitdiff
path: root/impl/EquationSystem.g
blob: 4884ca9416999815cf929142d400c1b52039b7f7 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
grammar EquationSystem;

options {
  language=C;
  output=AST;
  backtrack=true;
}

tokens {
  PLUS = '+' ;
  SUB = '-' ;
  MULT = '*' ;
  COMMA = ';' ;
  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)
     | GUARD^ '('! maxExpr ','! maxExpr ','! maxExpr ')'!
     | 'add'^ '('! maxExpr ( ','! maxExpr )* ')'!
     | 'sub'^ '('! maxExpr ( ','! maxExpr )* ')'!
     | 'mult'^ '('! maxExpr ( ','! maxExpr )* ')'!
     | term ( (PLUS | MULT | SUB | COMMA)^ expr )* ;

term : NUMBER
     | VARIABLE
     | '-'^ term ;


NUMBER : (DIGIT)+ ;
VARIABLE: (LETTER) (LETTER | DIGIT | '-' | '\[' | ']' )* ;
WHITESPACE : ( '\t' | ' ' | '\u000C' )+
             {
               $channel = HIDDEN;
             } ;

fragment
DIGIT : '0' .. '9' ;
fragment
LETTER: 'a' .. 'z' ;