 }
 
 @discardableResult
-func parseQuery(_ qfile: String, silent: Bool = false, includeComments: Bool = false) throws -> Query {
+func parseQuery(_ qfile: String, simplify: Bool, silent: Bool = false, includeComments: Bool = false) throws -> Query {
     let url = URL(fileURLWithPath: qfile)
     let sparql = try Data(contentsOf: url)
     guard var p = SPARQLParser(data: sparql, includeComments: includeComments) else { fatalError("Failed to construct SPARQL parser") }
-    let query = try p.parseQuery()
+    var query = try p.parseQuery()
+    if simplify {
+        let r = SPARQLQueryRewriter()
+        query = try r.simplify(query: query)
+    }
     let s = query.serialize()
     if !silent {
         print(s)

    }
}

func parseSPARQL(_ qfile: String, printTokens: Bool, pretty: Bool, silent: Bool) throws -> Int32 {
    do {
        warn("# \(qfile)")
        if pretty {

        } else if printTokens {
            try parseTokens(qfile, silent: silent)
        } else {
            try parseQuery(qfile, simplify: simplify, silent: silent)
        }
        //print("ok")
    } catch SerializationError.parsingError(let message) {
