diff options
author | Carlo Zancanaro <carlo@carlo-laptop> | 2012-05-03 15:35:39 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@carlo-laptop> | 2012-05-03 15:35:39 +1000 |
commit | fcecd0e7dc0bf103986c02e2f29fb518cd5571c5 (patch) | |
tree | 518bf3fcb3733bb8cc2ef584346aa409ea618a77 /impl/EquationSystem.g | |
parent | 9fd34b8cdc98ee757fc047216bd51c698cb7b82f (diff) |
Add a parser for linear equations
(Also add the antlr jar and C runtime)
Diffstat (limited to 'impl/EquationSystem.g')
-rw-r--r-- | impl/EquationSystem.g | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/impl/EquationSystem.g b/impl/EquationSystem.g new file mode 100644 index 0000000..d7e55a7 --- /dev/null +++ b/impl/EquationSystem.g @@ -0,0 +1,39 @@ +grammar EquationSystem; + +options { + language=C; + output=AST; +} + +tokens { + SYSTEM = 'system' ; + PLUS = '+' ; + SUB = '-' ; + MULT = '*' ; + 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 : term ( (PLUS | MULT | SUB)^ term )* ; + +term : NUMBER | VARIABLE ; + + +NUMBER : (DIGIT)+ ; +VARIABLE: (LETTER)+ ; +WHITESPACE : ( '\t' | ' ' | '\u000C' )+ + { + $channel = HIDDEN; + } ; + +fragment +DIGIT : '0' .. '9' ; +fragment +LETTER: 'a' .. 'z' ; |