~verifypn-cpn/verifypn/colored

« back to all changes in this revision

Viewing changes to PetriEngine/PQL/PQLQueryParser.y

  • Committer: Jonas Finnemann Jensen
  • Date: 2011-09-15 13:30:00 UTC
  • Revision ID: jopsen@gmail.com-20110915133000-wnywm1odf82emiuw
Import of sources from github

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%{
 
2
#include <stdio.h>
 
3
#include "PQL.h"
 
4
#include "Expressions.h"
 
5
using namespace PetriEngine::PQL;
 
6
 
 
7
Condition* query;
 
8
extern int pqlqlex();
 
9
void pqlqerror(const char *s) {printf("ERROR: %s\n", s);}
 
10
%}
 
11
 
 
12
%name-prefix "pqlq"
 
13
%expect 2
 
14
 
 
15
/* Possible data representation */
 
16
%union {
 
17
        PetriEngine::PQL::Expr* expr;
 
18
        PetriEngine::PQL::Condition* cond;
 
19
        std::string *string;
 
20
        int token;
 
21
}
 
22
 
 
23
/* Terminal type definition */
 
24
%token <string> ID INT
 
25
%token <token> LPAREN RPAREN
 
26
%token <token> AND OR NOT
 
27
%token <token> EQUAL NEQUAL LESS LESSEQUAL GREATER GREATEREQUAL
 
28
%token <token> PLUS MINUS MULTIPLY
 
29
 
 
30
/* Terminal associativity */
 
31
%left AND OR
 
32
%right NOT
 
33
 
 
34
/* Nonterminal type definition */
 
35
%type <expr> expr term factor
 
36
%type <cond> logic compare
 
37
 
 
38
/* Operator precedence, more possibly coming */
 
39
 
 
40
%start query
 
41
 
 
42
%%
 
43
 
 
44
query   : logic                         { query = $1; }
 
45
                | error                         { yyerrok; }
 
46
                ;
 
47
 
 
48
logic   : logic AND logic       { $$ = new AndCondition($1, $3); }
 
49
                | logic OR logic        { $$ = new OrCondition($1, $3); }
 
50
                | NOT logic                     { $$ = new NotCondition($2); }
 
51
                | LPAREN logic RPAREN   { $$ = $2; }
 
52
                | compare                       { $$ = $1; }
 
53
                ;
 
54
 
 
55
compare : expr EQUAL expr               { $$ = new EqualCondition($1, $3); }
 
56
                | expr NEQUAL expr              { $$ = new NotEqualCondition($1, $3); }
 
57
                | expr LESS expr                        { $$ = new LessThanCondition($1, $3); }
 
58
                | expr LESSEQUAL expr   { $$ = new LessThanOrEqualCondition($1, $3); }
 
59
                | expr GREATER expr             { $$ = new GreaterThanCondition($1, $3); }
 
60
                | expr GREATEREQUAL expr        { $$ = new GreaterThanOrEqualCondition($1, $3); }
 
61
                ;
 
62
 
 
63
expr    : expr PLUS term        { $$ = new PlusExpr($1, $3); }
 
64
                | expr MINUS term       { $$ = new SubtractExpr($1, $3); }
 
65
                | MINUS expr            { $$ = new MinusExpr($2); }
 
66
                | term                          { $$ = $1; }
 
67
                ;
 
68
 
 
69
term    : term MULTIPLY factor  { $$ = new MultiplyExpr($1, $3); }
 
70
                | factor                                { $$ = $1; }
 
71
                ;
 
72
 
 
73
factor  : LPAREN expr RPAREN    { $$ = $2; }
 
74
                | INT                   { $$ = new LiteralExpr(atol($1->c_str())); delete $1; }
 
75
                | ID                    { $$ = new IdentifierExpr(*$1, @1.first_column); delete $1; }
 
76
                ;