From fcecd0e7dc0bf103986c02e2f29fb518cd5571c5 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Thu, 3 May 2012 15:35:39 +1000 Subject: Add a parser for linear equations (Also add the antlr jar and C runtime) --- impl/EquationSystem.g | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 impl/EquationSystem.g (limited to 'impl/EquationSystem.g') 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' ; -- cgit v1.2.3