caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] ask for help : how to  increase coding efficiency  of fun as below
@ 2012-05-15  1:00 xy s
  2012-05-15  4:56 ` [Caml-list] " Hongbo Zhang
  0 siblings, 1 reply; 2+ messages in thread
From: xy s @ 2012-05-15  1:00 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1832 bytes --]

We have defined types:
 
type position = {line : int; col : int };;
type 'a ast = {data : 'a; kids : 'a ast list; posf : position; post: position};;
 
 
And then we transform string to ast:
 
let construct_common_ast s=
    let len = String.length s in
    let rec aux i j=
      if i>j then []
      else (
        let k = find_first s "(,)" i in
        let str = trim (String.sub s i (k-i)) in
        if k>j || s.[k]=')' then (
          if str="" then []
          else {data=str; kids=[]; posf=(calc_pos s i); post=(calc_pos s j)}::[]
        )
        else if s.[k]='(' then (
          let l = next_matched s '(' ')' (k+1) in
          let m = find_first_not s " \t\n\r" (l+1) in
          if m<=j && s.[m]<>',' then 
              fail_ast (calc_pos s k) (calc_pos s l) "construct_common_ast: invalid input string"
          else
              {data=str; kids=aux (k+1) (l-1); posf=(calc_pos s i); post=(calc_pos s l)} :: aux (m+1) j
        )
        else {data=str; kids=[]; posf=(calc_pos s i); post=(calc_pos s k)} :: aux (k+1) j
      )
    in
    let trees=aux 0 (len-1) in
List.hd trees;;
 
For construct_common_ast : 
# construct_common_ast;;
- : string -> string ast = <fun>
# construct_common_ast "node (function, params(), returns())";;
- : string ast =
{data = "node";
 kids =
  [{data = "function"; kids = []; posf = {line = 1; col = 8};
    post = {line = 1; col = 16}};
   {data = "params"; kids = []; posf = {line = 1; col = 17};
    post = {line = 1; col = 25}};
   {data = "returns"; kids = []; posf = {line = 1; col = 27};
    post = {line = 1; col = 36}}];
 posf = {line = 1; col = 2}; post = {line = 1; col = 37}}.
 
But, it is intolerable to deal with long string. how to increase coding efficiency of this function?
 
Thank you anyway. 
Best wishes!

[-- Attachment #2: Type: text/html, Size: 9030 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Caml-list] Re: ask for help : how to  increase coding efficiency  of fun as below
  2012-05-15  1:00 [Caml-list] ask for help : how to increase coding efficiency of fun as below xy s
@ 2012-05-15  4:56 ` Hongbo Zhang
  0 siblings, 0 replies; 2+ messages in thread
From: Hongbo Zhang @ 2012-05-15  4:56 UTC (permalink / raw)
  To: xy s; +Cc: caml-list

Hi,
    google camlp4 and quotation. if you don't need antiquotation 
support, it's fairly easy.
On 5/14/12 9:00 PM, xy s wrote:
> We have defined types:
>
> type position = {line : int; col : int };;
>
> type 'a ast = {data : 'a; kids : 'a ast list; posf : position; post:
> position};;
>
> And then we transform string to ast:
>
> let construct_common_ast s=
>
> let len = String.length s in
>
> let rec aux i j=
>
> if i>j then []
>
> else (
>
> let k = find_first s "(,)" i in
>
> let str = trim (String.sub s i (k-i)) in
>
> if k>j || s.[k]=')' then (
>
> if str="" then []
>
> else {data=str; kids=[]; posf=(calc_pos s i); post=(calc_pos s j)}::[]
>
> )
>
> else if s.[k]='(' then (
>
> let l = next_matched s '(' ')' (k+1) in
>
> let m = find_first_not s " \t\n\r" (l+1) in
>
> if m<=j && s.[m]<>',' then
>
> fail_ast (calc_pos s k) (calc_pos s l) "construct_common_ast: invalid
> input string"
>
> else
>
> {data=str; kids=aux (k+1) (l-1); posf=(calc_pos s i); post=(calc_pos s
> l)} :: aux (m+1) j
>
> )
>
> else {data=str; kids=[]; posf=(calc_pos s i); post=(calc_pos s k)} ::
> aux (k+1) j
>
> )
>
> in
>
> let trees=aux 0 (len-1) in
>
> List.hd trees;;
>
> For construct_common_ast :
>
> # construct_common_ast;;
>
> - : string -> string ast = <fun>
>
> # construct_common_ast "node (function, params(), returns())";;
>
> - : string ast =
>
> {data = "node";
>
> kids =
>
> [{data = "function"; kids = []; posf = {line = 1; col = 8};
>
> post = {line = 1; col = 16}};
>
> {data = "params"; kids = []; posf = {line = 1; col = 17};
>
> post = {line = 1; col = 25}};
>
> {data = "returns"; kids = []; posf = {line = 1; col = 27};
>
> post = {line = 1; col = 36}}];
>
> posf = {line = 1; col = 2}; post = {line = 1; col = 37}}.
>
> But, it is intolerable to deal with long string. how to increase coding
> efficiency of this function?
>
> Thank you anyway.
>
> Best wishes!
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-05-15  4:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-15  1:00 [Caml-list] ask for help : how to increase coding efficiency of fun as below xy s
2012-05-15  4:56 ` [Caml-list] " Hongbo Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).