From: Hongbo Zhang <bobzhang1988@gmail.com>
To: xy s <sxy62849013@yahoo.com.cn>
Cc: caml-list@inria.fr
Subject: [Caml-list] Re: ask for help : how to increase coding efficiency of fun as below
Date: Tue, 15 May 2012 00:56:06 -0400 [thread overview]
Message-ID: <4FB1E1E6.6000208@gmail.com> (raw)
In-Reply-To: <1337043605.43298.YahooMailClassic@web15601.mail.cnb.yahoo.com>
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!
>
prev parent reply other threads:[~2012-05-15 4:56 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-15 1:00 [Caml-list] " xy s
2012-05-15 4:56 ` Hongbo Zhang [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4FB1E1E6.6000208@gmail.com \
--to=bobzhang1988@gmail.com \
--cc=caml-list@inria.fr \
--cc=sxy62849013@yahoo.com.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).