Index: FCS-QL/trunk/src/main/antlr4/eu/clarin/sru/fcs/qlparser/FCSLexer.g4
===================================================================
--- FCS-QL/trunk/src/main/antlr4/eu/clarin/sru/fcs/qlparser/FCSLexer.g4	(revision 6863)
+++ FCS-QL/trunk/src/main/antlr4/eu/clarin/sru/fcs/qlparser/FCSLexer.g4	(revision 6865)
@@ -7,6 +7,6 @@
  */
 
-LPAREN: '(';
-RPAREN: ')';
+L_PAREN: '(';
+R_PAREN: ')';
 L_SQUARE_BRACKET: '[';
 R_SQUARE_BRACKET: ']';
@@ -15,18 +15,22 @@
 NOT: '!';
 FWD_SLASH: '/';
+L_CURLY_BRACKET: '{';
+R_CURLY_BRACKET: '}';
+Q_ONE_OR_MORE: '+';
+Q_ZERO_OR_MORE: '*';
+Q_ZERO_OR_ONE: '?';
+Q_COMMA: ',';
+OPERATOR_EQ: '=';
+OPERATOR_NE: '!=';
+COLON: ':';
 
-OPERATOR
-    : '='
-    | '!='
+REGEXP_FLAGS
+    : ( 'i'    /* case-insensitive; Poliqarp/Perl compat */
+      | 'I'    /* case-sensitive; Poliqarp compat */
+      | 'c'    /* case-insensitive, CQP compat */
+      | 'C'    /* case-sensitive */
+      | 'l'    /* literal matching, CQP compat*/
+      | 'd')+  /* diacritic agnostic matching, CQP compat */
     ;
-
-QUANTIFIER
-    : '+'                           /* one-or-more */
-    | '*'                           /* zero-or-more */
-    | '?'                           /* zero-or-one */
-    | '{' INTEGER '}'               /* exactly n-times */
-    | '{' INTEGER? ',' INTEGER '}'  /* at most */
-    | '{' INTEGER ',' INTEGER? '}'  /* min-max */
-    ;   
 
 REGEXP
@@ -34,10 +38,11 @@
     ;
 
-SIMPLE_ATTRIBUTE
-    : IDENTIFIER
-    ;
-QUALIFIED_ATTRIBUTE
-    :  IDENTIFIER ':' IDENTIFIER
-    ;
+//SIMPLE_ATTRIBUTE
+//    : IDENTIFIER
+//    ;
+
+//QUALIFIED_ATTRIBUTE
+//    :  IDENTIFIER ':' IDENTIFIER
+//    ;
 
 IDENTIFIER
@@ -70,13 +75,4 @@
     | 'text'
     | 'session'
-    ;
-
-REGEXP_FLAG
-    : 'i'  /* case-insensitive; Poliqarp/Perl compat */
-    | 'I'  /* case-sensitive; Poliqarp compat */
-    | 'c'  /* case-insensitive, CQP compat */
-    | 'C'  /* case-sensitive */
-    | 'l'  /* literal matching, CQP compat*/
-    | 'd'  /* diacritic agnostic matching, CQP compat */
     ;
 
@@ -129,3 +125,2 @@
     : WS -> skip
     ;
-
Index: FCS-QL/trunk/src/main/antlr4/eu/clarin/sru/fcs/qlparser/FCSParser.g4
===================================================================
--- FCS-QL/trunk/src/main/antlr4/eu/clarin/sru/fcs/qlparser/FCSParser.g4	(revision 6863)
+++ FCS-QL/trunk/src/main/antlr4/eu/clarin/sru/fcs/qlparser/FCSParser.g4	(revision 6865)
@@ -6,5 +6,5 @@
  */ 
 query
-    : main_query within_part?
+    : main_query (WITHIN within_part)? EOF
     ;
 
@@ -17,38 +17,52 @@
   ;
 
-//main_query
-//    : simple_query 
-//    | main_query OR main_query     /* or */
-//    | main_query main_query         /* sequence */
-//    | main_query QUANTIFIER         /* quatification */ 
-//    ;
 
 main_query
-    : simple_query 
-    | simple_query OR main_query     /* or */
-    | simple_query main_query         /* sequence */
-    | simple_query QUANTIFIER         /* quatification */ 
+    : query_simple /* #simpleQuery */
+    | query_group /* #queryGroup */
+    | query_sequence /* #querySequence */
+    | query_disjunction /* #queryDisjunction */
     ;
 
-/*
-simple_query
-    : implicit_query
-    | segment_query 
-    ;
-*/
 
-simple_query
-    : LPAREN main_query RPAREN            /* grouping */
-    | implicit_query
-    | segment_query 
+query_disjunction
+    : (query_simple | query_sequence | query_group)
+            (OR (query_simple | query_sequence | query_group))+
     ;
 
-implicit_query
+
+query_sequence
+    : (query_simple | query_group)+
+    ;
+
+
+query_group
+    : L_PAREN (query_disjunction | query_sequence) R_PAREN quantifier?
+    ;
+
+
+query_simple
+    : (query_implicit | query_segment) quantifier?
+    ;
+
+
+quantifier
+    : (Q_ONE_OR_MORE | Q_ZERO_OR_MORE | Q_ZERO_OR_ONE |
+        ( L_CURLY_BRACKET
+            (INTEGER |
+             INTEGER? Q_COMMA INTEGER |
+             INTEGER Q_COMMA INTEGER?) R_CURLY_BRACKET))
+    ;
+
+
+query_implicit
     : flagged_regexp
     ;
 
-segment_query
+
+query_segment
     : L_SQUARE_BRACKET expression? R_SQUARE_BRACKET     /* [ expression? ] */
     ;
+
 
 within_part
@@ -56,42 +70,59 @@
     ;
 
+
 simple_within_part
-    : WITHIN SIMPLE_WITHIN_SCOPE
-    ;
-
-//expression
-//    : basic_expression          # basicExpression
-//    | expression OR expression  # orExpression /* or */
-//    | expression AND expression # andExpression /* and */
-//    | LPAREN expression RPAREN  # groupedExpression /* grouping */
-//    | NOT expression            # notExpression /* not */ 
-//    ;
-
-expression
-    : basic_expression          # basicExpression
-    | expression OR expression  # orExpression /* or */
-    | expression AND expression # andExpression /* and */
-    ;
-
-/*
-basic_expression
-    : attribute OPERATOR flagged_regexp
-    ;
-*/
-
-basic_expression
-    : LPAREN expression RPAREN  # groupedExpression /* grouping */
-    | NOT expression            # notExpression /* not */ 
-    | attribute OPERATOR flagged_regexp #basicAttrOpFlaggedRegex
+    : SIMPLE_WITHIN_SCOPE
     ;
 
 
+expression
+    : expression_basic
+    | expression_not
+    | expression_group
+    | expression_or
+    | expression_and
+    ;
+
+
+expression_or
+    : (expression_basic | expression_group | expression_not | expression_and) 
+        (OR (expression_basic | expression_group | expression_not | expression_and))+ 
+    ;    
+
+
+expression_and
+    : (expression_basic | expression_group | expression_not)
+        (AND (expression_basic | expression_group | expression_not))+ 
+    ;    
+
+
+
+expression_group
+    : L_PAREN (expression_basic | expression_not | expression_or | expression_and) R_PAREN
+    ;
+
+
+expression_not
+    : NOT (expression_basic | expression_not | expression_or | expression_and)
+    ;
+
+
+expression_basic
+    : attribute (OPERATOR_EQ | OPERATOR_NE) flagged_regexp
+    ;
+
 attribute
-    : SIMPLE_ATTRIBUTE    # simpleAttribute
-    | QUALIFIED_ATTRIBUTE # qualifiedAttribute
+    : (qualifier COLON)? identifier
+    ; 
+
+qualifier
+    : (IDENTIFIER | WITHIN | SIMPLE_WITHIN_SCOPE | REGEXP_FLAGS)
+    ;
+
+identifier
+    : (IDENTIFIER | WITHIN | SIMPLE_WITHIN_SCOPE | REGEXP_FLAGS)
     ;
 
 flagged_regexp
-    : REGEXP         # regexp
-    | REGEXP FWD_SLASH REGEXP_FLAG+ # flaggedRegexp 
+    : REGEXP (FWD_SLASH REGEXP_FLAGS)? 
     ;
