Article View: fa.caml
Article #65[Caml-list] Re: ask for help : how to increase coding efficiency of fun as below
From: Hongbo Zhang
Date: Tue, 15 May 2012 04:56
Date: Tue, 15 May 2012 04:56
106 lines
5391 bytes
5391 bytes
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!
>
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Technical Details
Message-ID:
<fa.0ocC1a1/Jdx+snYskOamyw2CXek@ifi.uio.no>Path:
arc1-ca.pugleaf.net!pr3ni228pbb.0!nntp.google.com!news1.google.com!goblin3!goblin1!goblin.stu.neva.ru!uio.no!nntp.uio.no!.POSTED!not-for-mailReferences:
<fa.EwCdt2lg1qujtNJeohXpvYCNYzY@ifi.uio.no>Show Full Headers
Complete Article Headers
X-Google-Thread: ff861,88fb114157770769
X-Google-NewGroupId: yes
X-Google-Attributes: gidc0fe4b917e,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.68.241.98 with SMTP id wh2mr551556pbc.7.1337057795264;
Mon, 14 May 2012 21:56:35 -0700 (PDT)
Path: arc1-ca.pugleaf.net!pr3ni228pbb.0!nntp.google.com!news1.google.com!goblin3!goblin1!goblin.stu.neva.ru!uio.no!nntp.uio.no!.POSTED!not-for-mail
From: Hongbo Zhang <bobzhang1988@gmail.com>
Newsgroups: fa.caml
Subject: [Caml-list] Re: ask for help : how to increase coding efficiency of fun as
below
Date: Tue, 15 May 2012 04:56:34 UTC
Organization: Internet mailing list
Lines: 107
Message-ID: <fa.0ocC1a1/Jdx+snYskOamyw2CXek@ifi.uio.no>
References: <fa.EwCdt2lg1qujtNJeohXpvYCNYzY@ifi.uio.no>
NNTP-Posting-Host: mail-jess.uio.no
Mime-Version: 1.0
X-Trace: readme.uio.no 1337057794 28017 129.240.7.9 (15 May 2012 04:56:34 GMT)
X-Complaints-To: abuse@uio.no
NNTP-Posting-Date: Tue, 15 May 2012 04:56:34 +0000 (UTC)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20120420 Thunderbird/12.0
Cc: caml-list@inria.fr
To: xy s <sxy62849013@yahoo.com.cn>
X-IronPort-AV: E=Sophos;i="4.75,591,1330902000";
d="scan'208";a="158388897"
X-Authentication-Warning: walapai.inria.fr: sympa set sender to caml-list-owner@inria.fr using -f
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"
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==
In-Reply-To: <1337043605.43298.YahooMailClassic@web15601.mail.cnb.yahoo.com>
X-Sequence: 4104
List-Id: <caml-list.inria.fr>
List-Archive: <https://sympa-roc.inria.fr/wws/arc/caml-list>
List-Help: <mailto:sympa@inria.fr?subject=help>
List-Owner: <mailto:caml-list-request@inria.fr>
List-Post: <mailto:caml-list@inria.fr>
List-Subscribe: <mailto:sympa@inria.fr?subject=subscribe%20caml-list>
List-Unsubscribe: <mailto:sympa@inria.fr?subject=unsubscribe%20caml-list>
Original-Date: Tue, 15 May 2012 00:56:06 -0400
Original-Newsgroups: gmane.comp.lang.caml.inria
Original-Message-Id: <4FB1E1E6.6000208@gmail.com>
Original-References: <1337043605.43298.YahooMailClassic@web15601.mail.cnb.yahoo.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit