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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
%{
#include <BALL/KERNEL/expressionParser.h>
#include <BALL/KERNEL/expressionTree.h>
using namespace BALL;
using namespace std;
extern int ExpressionParserlex();
extern void ExpressionParsererror(char const* s)
throw(Exception::ParseError);
%}
%union {
char* text;
ExpressionParser::SyntaxTree* node;
}
%left <text> TK_OR
%left <text> TK_AND
%left <text> TK_NOT
%left <text> TK_WHITESPACE;
%left <text> TK_SOMETHING
%token <text> TK_PREDICATE_NAME
%token <text> TK_OPEN_BRACKET
%token <text> TK_CLOSE_BRACKET
%type <node> kernel_expression
%type <node> expression
%type <node> predicate
%type <text> balanced_brackets
%type <text> outer_brackets
%type <text> inside_brackets
%type <text> something
%start kernel_expression
%%
kernel_expression: expression
{
ExpressionParser::state.tree = $1;
}
;
expression:
predicate {
$$ = $1;
}
| TK_WHITESPACE expression {
$$ = $2;
}
| expression TK_WHITESPACE {
$$ = $1;
}
| TK_NOT expression {
$$ = $2;
$2->negate = !$2->negate;
}
| TK_OPEN_BRACKET expression TK_CLOSE_BRACKET {
$$ = $2;
}
| expression TK_AND expression {
$$ = new ExpressionParser::SyntaxTree($1, $3, ExpressionTree::AND);
}
| expression TK_OR expression {
$$ = new ExpressionParser::SyntaxTree($1, $3, ExpressionTree::OR);
}
;
predicate:
TK_PREDICATE_NAME outer_brackets {
*$2 = '\0';
$$ = new ExpressionParser::SyntaxTree($1, $2 + 1);
}
;
outer_brackets:
balanced_brackets {
$$ = $1;
*($1 + strlen($1) - 1) = '\0';
}
;
balanced_brackets:
TK_OPEN_BRACKET inside_brackets TK_CLOSE_BRACKET {
$$ = $1;
}
;
inside_brackets:
something balanced_brackets inside_brackets {
$$ = $1;
}
| something {
$$ = $1;
}
;
something:
{
$$ = 0;
}
| TK_SOMETHING something {
$$ = $1;
}
| TK_WHITESPACE something {
$$ = $1;
}
| TK_PREDICATE_NAME something {
$$ = $1;
}
| TK_AND something {
$$ = $1;
}
| TK_OR something {
$$ = $1;
}
| TK_NOT something {
$$ = $1;
}
;
%%
void ExpressionParsererror(char const* s)
throw(Exception::ParseError)
{
throw Exception::ParseError(__FILE__, 0,
ExpressionParser::state.buffer,
String(s) + String(" (at position ")
+ String(ExpressionParser::state.char_count) + String(")"));
}
|