Kota UENISHI avatar Kota UENISHI committed 4d91c01

hello sql parserhg st

Comments (0)

Files changed (7)

 # OCAML_BYTE_LINK_FLAGS +=
 # OCAML_NATIVE_LINK_FLAGS +=
 
-OCamlGeneratedFiles(parser.ml lexer.ml)
+OCamlGeneratedFiles(sql_parser.ml sql_lexer.ml)
 
 ################################################
 # Build an OCaml library
 #
 
 FILES[] =
+   main
    sql
-#    file2
-#
+   sql_parser
+   sql_lexer
+
 PROGRAM = sql
 # OCAML_LIBS +=
 # OCAML_CLIBS +=

sql/lexer.mll

-
-{
-
-open Parser
-let debugprint = ref false;;
-let print str =  if !debugprint then print_endline str;;
-
-}
-
-let digit = ['0'-'9']*
-let literal = ['a'-'z']['A'-'Z' 'a'-'z' '_' '0'-'9']*
-
-rule token = parse
-  | "select" { SELECT }
-  | "SELECT" { SELECT }
-  | "from" { FROM }
-  | "FROM" { FROM }
-  | ","    { COMMA }
-  | literal as s { LITERAL(s) }
-  | eof		{ print "eof."; EOF }
-  | _   { token lexbuf }
+
+let parse q =
+  let lexbuf = Lexing.from_string q in
+  try
+    Sql_parser.input Sql_lexer.token lexbuf
+  with
+    | exn ->
+      print_endline (Printexc.to_string exn);
+      raise exn;;
+
+let sample_query = "select * from mytable";;
+
+let _ =
+  print_endline sample_query;
+(*  Sql.pp_query (Sql.Select(Sql.Columns(["hoge"; "hage"]), "tttttttttttttt")); *)
+  let q = parse sample_query in
+  Sql.pp_query q;
+  print_endline " => hello";;

sql/parser.mly

-
-%{
-
-  open Printf
-  let _ = Parsing.set_trace false;;
-  let debugprint = ref false;;
-  
-  let print str = if !debugprint then print_endline str;;
-  
-  let parse_error s = print ("parse_error->"^s);;
-
-%}
-
-%token SELECT
-%token INSERT
-%token DELETE
-%token UPDATE
-%token INTO
-%token VALUES
-%token SET
-%token FROM
-%token WHERE
-%token LIMIT
-%token DOT
-%token LBRACE RBRACE
-%token COMMA
-%token ASTERISK
-%token GT LT EQ NEQ NOT
-
-%token <int> INT
-
-%token <string> LITERAL
-
-
-%type <Sql.query> input
-
-%%
-
-input: | exp { $1 };
-
-exp:
-  | SELECT columns FROM TABLE { Sql.Select($2, $4) };
-
-columns:
-  | COLUMN { [$1] }
-  | COLUMN COMMA columns { $1::$3 }
-;
 
-type query = Select of string list * string (* columns, table *)
+type columns = All_col | Columns of string list
+type query = Select of columns * string (* columns, table *)
 
 let pp_query = function
-  | Select(columns, table) ->
+  | Select(All_col, table) ->
+    Printf.printf "select: * from %s\n" table;
+  | Select(Columns(columns), table) ->
     Printf.printf "select: %s from %s\n" (String.concat "," columns) table;;
-
-let parse q =
-  let lexbuf = Lexing.from_string q in
-  try
-    Parser.input Lexer.token lexbuf
-  with
-    | exn ->
-      print_endline (Printexc.to_string exn);
-      raise exn;;
-
-let sample_query = "select * from mytable";;
-
-let _ =
-  print_endline sample_query;
-  pp_query (Select(["hoge"; "hage"], "tttttttttttttt"));
-  let _ = parse sample_query in
-  print_endline " => hello";;

sql/sql_lexer.mll

+
+{
+
+open Sql_parser
+let debugprint = ref true;;
+let print str =  if !debugprint then print_endline str;;
+
+}
+
+let digit = ['0'-'9']*
+let literal = ['a'-'z']['A'-'Z' 'a'-'z' '_' '0'-'9']*
+
+rule token = parse
+  | "select" { SELECT }
+  | "SELECT" { SELECT }
+  | "from"   { FROM }
+  | "FROM"   { FROM }
+  | ","      { COMMA }
+  | "*"      { ASTERISK }
+  | literal as s { LITERAL(s) }
+  | "\n"         { print "f"; token lexbuf }
+(*  | eof	         { print "eof."; EOF } *)
+  | _            { token lexbuf }

sql/sql_parser.mly

+
+%{
+
+  open Printf
+  let _ = Parsing.set_trace false;;
+  let debugprint = ref true;;
+  
+  let print str = if !debugprint then print_endline str;;
+  
+  let parse_error s = print ("parse_error->"^s);;
+
+%}
+
+%token SELECT
+%token INSERT
+%token DELETE
+%token UPDATE
+%token INTO
+%token VALUES
+%token SET
+%token FROM
+%token WHERE
+%token LIMIT
+%token DOT
+%token LBRACE RBRACE
+%token COMMA
+%token ASTERISK
+%token GT LT EQ NEQ NOT
+%token EOF
+
+%token <int> INT
+
+%token <string> LITERAL
+
+%start input
+%type <Sql.query> input
+
+%%
+
+input: | exp { $1 };
+
+exp:
+  | SELECT ASTERISK FROM LITERAL { Sql.Select(Sql.All_col, $4) };
+  | SELECT columns  FROM LITERAL { Sql.Select(Sql.Columns($2), $4) };
+
+columns:
+  | LITERAL  { [$1] }
+  | LITERAL COMMA columns { $1::$3 }
+;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.