Index: DefaultCompiler.java
===================================================================
--- DefaultCompiler.java	(revision 43982)
+++ DefaultCompiler.java	(revision 43983)
@@ -6,6 +6,7 @@
 import org.gvsig.expressionevaluator.LexicalAnalyzer.Token;
 import org.gvsig.expressionevaluator.Code;
 import org.gvsig.expressionevaluator.CodeBuilder;
+import org.gvsig.expressionevaluator.ExpressionSyntaxException;
 
 public class DefaultCompiler implements Compiler {
 
@@ -35,6 +36,11 @@
     public void setLexicalAnalyzer(LexicalAnalyzer lexer) {
         this.lexer = lexer;
     }
+
+    @Override
+    public LexicalAnalyzer getLexicalAnalyzer() {
+        return this.lexer;
+    }
     
     @Override
     public void setCodeBuilder(CodeBuilder codeBuilder) {
@@ -58,12 +64,17 @@
     
     @Override
     public Code compileExpression(String expression) {
-        this.lexer.setSource(expression);
-        return parseExpression();
+        this.lexer.setSource(expression.trim());
+        Code code = parseExpression();
+        if( !this.lexer.isEOF() ) {
+            throw new ExpressionSyntaxException(lexer);
+        }
+        return code;
     }
 
     private Code parseExpression() {
-        return parse_relational();
+        Code code = parse_relational();
+        return code;
     }
 
     private Code parse_relational() {
@@ -75,11 +86,17 @@
             case Token.OP_OR:
                 lexer.next();
                 op2 = parse_not();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.or(op1, op2);
                 break;
             case Token.OP_AND:
                 lexer.next();
                 op2 = parse_not();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.and(op1, op2);
                 break;
             default:
@@ -110,31 +127,49 @@
             case Token.OP_LT:
                 lexer.next();
                 op2 = parse_sum();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.lt(op1, op2);
                 break;
             case Token.OP_GT:
                 lexer.next();
                 op2 = parse_sum();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.gt(op1, op2);
                 break;
             case Token.OP_LE:
                 lexer.next();
                 op2 = parse_sum();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.le(op1, op2);
                 break;
             case Token.OP_GE:
                 lexer.next();
                 op2 = parse_sum();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.ge(op1, op2);
                 break;
             case Token.OP_EQ:
                 lexer.next();
                 op2 = parse_sum();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.eq(op1, op2);
                 break;
             case Token.OP_NE:
                 lexer.next();
                 op2 = parse_sum();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.ne(op1, op2);
                 break;
             case Token.PRED_IS: {
@@ -145,6 +180,9 @@
                         op1 = codeBuilder.not(op1);
                     } else {
                         op2 = parse_sum();
+                        if( op2==null ) {
+                            throw new ExpressionSyntaxException(lexer);
+                        }
                         op1 = codeBuilder.is(op1, op2);
                     }
                 }
@@ -156,16 +194,25 @@
             case Token.OP_REGEXP:
                 lexer.next();
                 op2 = parse_sum();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.regexp(op1, op2);
                 break;
             case Token.PRED_LIKE:
                 lexer.next();
                 op2 = parse_sum();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.like(op1, op2);
                 break;
             case Token.PRED_ILIKE:
                 lexer.next();
                 op2 = parse_sum();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.ilike(op1, op2);
                 break;
             default:
@@ -205,16 +252,25 @@
             case Token.OP_MULT:
                 lexer.next();
                 op2 = parse_getattr();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.mult(op1, op2);
                 break;
             case Token.OP_DIV:
                 lexer.next();
                 op2 = parse_getattr();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.div(op1, op2);
                 break;
             case Token.OP_MOD:
                 lexer.next();
                 op2 = parse_getattr();
+                if( op2==null ) {
+                    throw new ExpressionSyntaxException(lexer);
+                }
                 op1 = codeBuilder.mod(op1, op2);
                 break;
             default:
@@ -235,7 +291,11 @@
                 lexer.next();
                 next = lexer.look();
                 if( next.getType()!=Token.IDENTIFIER ) {
-                    throw new RuntimeException("An attribute identifier was expected and '"+next.getLiteral()+"' was found");
+                    throw new ExpressionSyntaxException(
+                        "An attribute identifier was expected and '"+next.getLiteral()+"' was found", 
+                        lexer.getSource(),
+                        lexer.getPosition()
+                    );
                 }
                 String id = (String) next.getLiteral();
                 lexer.next();
@@ -245,7 +305,11 @@
                     Arguments args = parseArgs();
                     next = lexer.next();
                     if( next.getType() != Token.PARENTHESIS_CLOSE ) {
-                        throw new RuntimeException("Closing parenthesis was expected and '"+next.getLiteral()+"' was found");
+                        throw new ExpressionSyntaxException(
+                            "Closing parenthesis was expected and '"+next.getLiteral()+"' was found", 
+                            lexer.getSource(),
+                            lexer.getPosition()
+                        );
                     }
                     return codeBuilder.method(op1, id, args);
                 } else {
@@ -266,7 +330,11 @@
                 Code value = parseExpression();
                 Token next = lexer.next();
                 if( next.getType() != Token.PARENTHESIS_CLOSE ) {
-                    throw new RuntimeException("Closing parenthesis was expected and '"+next.getLiteral()+"' was found");
+                    throw new ExpressionSyntaxException(
+                        "Closing parenthesis was expected and '"+next.getLiteral()+"' was found",
+                        lexer.getSource(),
+                        lexer.getPosition()
+                    );
                 }
                 return value;
             }
@@ -279,7 +347,11 @@
                     Arguments args = parseArgs();
                     next = lexer.next();
                     if( next.getType() != Token.PARENTHESIS_CLOSE ) {
-                        throw new RuntimeException("Closing parenthesis was expected and '"+next.getLiteral()+"' was found");
+                        throw new ExpressionSyntaxException(
+                            "Closing parenthesis was expected and '"+next.getLiteral()+"' was found", 
+                            lexer.getSource(),
+                            lexer.getPosition()
+                        );
                     }
                     return codeBuilder.function(id, args);
                 } else {
