From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q4F4uF0A024609 for ; Tue, 15 May 2012 06:56:15 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Au0BAFLhsU/RVdi2imdsb2JhbABEs3QIIgEBAQoJDQcSBiOCFQEBAQQBAQEPAhMRCAEbHAEBAwwGBQsNCRYPCQMCAQIBEREBBQEcBg0BBwEBHoddAQMLnEIJA4wkgnOFCQoZJw1XiHYBBQyRBgSVfY5gPYQo X-IronPort-AV: E=Sophos;i="4.75,591,1330902000"; d="scan'208";a="158388821" Received: from mail-qc0-f182.google.com ([209.85.216.182]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 15 May 2012 06:56:09 +0200 Received: by qcsg15 with SMTP id g15so5838236qcs.27 for ; Mon, 14 May 2012 21:56:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:newsgroups:to:cc :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=bmQEV2D72MPwWdSeHKTvxv5PKfeC+XGkj8ITszWCz3U=; b=MwKrTzvl2n2JvZ3sTbH2e6VsuR4FdrZ836tOzXfMB3QxAk22R3RXGS6lnJ9t3jD/WI t7jMIMGW1jxejgzDkBX64OTznCyX3mkqMpH6yEaZbe1rJTxzi/ya9QoOcYqGmnnkDpJ+ 8x+nKQFXO8Iun8Y1TiBmBFm17AX4o4JT0NWh9bd5/2vTOOtHxMKDvrbvMFrsRUVL9DT3 uVP4VOj2mZk/iUVPdf1MqOmPTXieBY4yaJnWJBjjR9dpm9qRzUiL3rl/QfksxRzOcK4o 2OOghyG33psQVGPx0iWzEtzUf21gPuyMBCrfR7w74XoHvXIZ57GcyejuvC8HwxJyr+Le Yw1g== Received: by 10.224.185.204 with SMTP id cp12mr789576qab.42.1337057768703; Mon, 14 May 2012 21:56:08 -0700 (PDT) Received: from Hongbos-MacBook-Air.local ([66.250.143.212]) by mx.google.com with ESMTPS id x10sm1050825qan.1.2012.05.14.21.56.07 (version=SSLv3 cipher=OTHER); Mon, 14 May 2012 21:56:07 -0700 (PDT) Message-ID: <4FB1E1E6.6000208@gmail.com> Date: Tue, 15 May 2012 00:56:06 -0400 From: Hongbo Zhang User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20120420 Thunderbird/12.0 MIME-Version: 1.0 Newsgroups: gmane.comp.lang.caml.inria To: xy s CC: caml-list@inria.fr References: <1337043605.43298.YahooMailClassic@web15601.mail.cnb.yahoo.com> In-Reply-To: <1337043605.43298.YahooMailClassic@web15601.mail.cnb.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: [Caml-list] Re: ask for help : how to increase coding efficiency of fun as below 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 = > > # 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! >