Skip to content

Commit

Permalink
Exercise 2: parsing, regex
Browse files Browse the repository at this point in the history
  • Loading branch information
vogler committed Apr 29, 2012
1 parent 99cf1cf commit 048641f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 26 deletions.
28 changes: 2 additions & 26 deletions .gitignore
Expand Up @@ -16,32 +16,8 @@
*.log
*.aux
*.synctex.gz
*.class

#ignore thumbnails created by windows
Thumbs.db
#Ignore files build by Visual Studio
*.obj
*.exe
*.pdb
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.cache
*.ilk
*.log
[Bb]in
[Dd]ebug*/
*.lib
*.sbr
obj/
[Rr]elease*/
_ReSharper*/
[Tt]est[Rr]esult*
[Bb]in
78 changes: 78 additions & 0 deletions 01/tinydb/samples/Exercise2.java
@@ -0,0 +1,78 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import tinydb.Database;
import tinydb.Table;

public class Exercise2 {

// select (*|attribute(,attribute)*)
// from relation binding(,relation binding)*
// where binding.attribute=(binding.attribute|constant)
// (and binding.attribute=(binding.attribute|constant))*
public static void main(String[] args) throws java.io.IOException {
Database db = Database.open("../data/uni");
System.out.println("Enter your query below. Type 'quit' to exit.");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
loop:
while(true){
String q = br.readLine();
if(q.equals("quit")) break;
Pattern p = Pattern.compile("select (.+) from (.+) where (.+)");
Matcher m = p.matcher(q);
if(!m.matches()){
System.err.println("Query doesn't match the pattern: "+p.toString());
continue;
}
String selections = m.group(1);
String relations = m.group(2);
String joincond = m.group(3);
// (*|attribute(,attribute)*)
p = Pattern.compile("\\*|\\w+(,\\w+)*");
if(!p.matcher(selections).matches()){
System.err.println("Selections don't match the pattern: "+p.toString());
System.err.println("Your input: "+selections);
continue;
}
// relation binding(,relation binding)*
p = Pattern.compile("\\w+ \\w+(,\\w+ \\w+)*");
if(!p.matcher(relations).matches()){
System.err.println("Relations don't match the pattern: "+p.toString());
System.err.println("Your input: "+relations);
continue;
}
// binding.attribute=(binding.attribute|constant)
// (and binding.attribute=(binding.attribute|constant))*
p = Pattern.compile("\\w+\\.\\w+=\\w+(and \\w+\\.\\w+=\\w+)*");
if(!p.matcher(joincond).matches()){
System.err.println("Join conditions don't match the pattern: "+p.toString());
System.err.println("Your input: "+joincond);
continue;
}

// TODO save values in file

// handle relations
Map<String, Table> h_rel = new HashMap<String, Table>();
String[] a_rel = relations.split(",");
for(String s : a_rel){
String[] binding = s.split(" ");
System.out.println("trying to open "+binding[0]);
Table table = db.getTable(binding[0]);
if(table == null){ // TODO java.io.IOException: Stream closed
System.err.println("Table "+binding[0]+" doesn't exist");
continue loop;
}
h_rel.put(binding[1], table);
}


br.close();
}
}
}

0 comments on commit 048641f

Please sign in to comment.