From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.5 required=5.0 tests=AWL,MISSING_HEADERS autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 976DBBC37 for ; Thu, 14 May 2009 12:29:47 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgoHAIePC0qBrw8EmWdsb2JhbACNOg8HiUsBAQEBAQgLCgcRt3yCYgGBHwU X-IronPort-AV: E=Sophos;i="4.41,194,1241388000"; d="scan'208";a="27716560" Received: from ext.lri.fr ([129.175.15.4]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 14 May 2009 12:29:47 +0200 Received: from localhost (localhost [127.0.0.1]) by ext.lri.fr (Postfix) with ESMTP id C5B57A4447; Thu, 14 May 2009 12:29:46 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at lri.fr Received: from ext.lri.fr ([127.0.0.1]) by localhost (ext.lri.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id d3L9+DUHObwc; Thu, 14 May 2009 12:29:46 +0200 (CEST) Received: from [129.175.4.114] (lri4-114 [129.175.4.114]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ext.lri.fr (Postfix) with ESMTP id A12E4A432A; Thu, 14 May 2009 12:29:46 +0200 (CEST) Message-ID: <4A0BF29A.80603@lri.fr> Date: Thu, 14 May 2009 12:29:46 +0200 From: Cedric Auger User-Agent: Thunderbird 2.0.0.21 (X11/20090318) MIME-Version: 1.0 Cc: Joel Christner , caml-list Subject: Re: [Caml-list] Re: Toplevel function question References: <9da743ed0905131643t98cd36co1f00498434be3c3c@mail.gmail.com> <9da743ed0905131710w5563de39i60f1d60c071ff515@mail.gmail.com> <4A0BF104.4090105@lri.fr> In-Reply-To: <4A0BF104.4090105@lri.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; lri:01 toplevel:01 toplevel:01 ocaml:01 syntax:01 lexbuf:01 lexing:01 stdin:01 expr:01 lexbuf:01 expr:01 iter:01 ocamlc:01 syntax:01 parser:01 And one last thing in your title, avoid using "toplevel" term as it often refers to the program runned by typing "ocaml", that is the interactive system, so it is a bit confusing. Cedric Auger a écrit : > Joel Christner a écrit : >> Simpler representation - tried compiling this and got the same syntax >> error - at a line number that was after the EOF. Thanks in advance >> for any help!! >> >> let add_text variablelist text = >> variablelist.contents <- text::variablelist.contents >> >> let originalprogramcontents = ref [""] >> >> let _ = >> let lexbuf = Lexing.from_channel stdin in >> let rec storeoriginalprogram = >> let expr = lexbuf in >> match expr with >> | EOF -> () >> | _ -> (add_text originalprogramcontents expr); >> storeoriginalprogram >> in let parseprogram = List.iter >> (fun n -> print_string n; print_string "\n";) >> originalprogramcontents >> >> $ ocamlc -c toplevel.ml >> File "toplevel.ml ", line 17, characters 1-1: >> Syntax error >> > Why do you use ( and ) delimiting "add_text originalprogramcontents > expr"? > in (fun n -> print_string n; print_string "\n";), last ";" is useless > as Benjamin said your "let" is missing its "in" > eventually I assume you didn't intended to write "expr = lexbuf", but > rather "expr = parse lexbuf", where parse is a function you must > define, probably the "Parser.expr Scanner.token" of your first mail, > so I think that the following would be better: > > let add_text variablelist text = > variablelist := text::!variablelist > > let originalprogramcontents = ref [""] > > let _ = > let lexbuf = Lexing.from_channel stdin in > let rec storeoriginalprogram = > let expr = Parser.expr Scanner.token lexbuf in > match expr with > | EOF -> () > | _ -> add_text originalprogramcontents expr; storeoriginalprogram > in > List.iter > (fun n -> print_string n; print_string "\n") > originalprogramcontents > > And I don't think "let _ =" is mandatory > Note that a list can be empty and you can write > let originalprogramcontents = ref [] > if you want an empty list at beginning (I don't know if you need an > empty list or a list containing an empty string), caml will guess its > type since you used add_text on this list. > If you don't use the list or want to restrict a type you also can > "cast" it: > let (originalprogramcontents : (string list) ref) = ref [] > > >> >> >> On Wed, May 13, 2009 at 4:43 PM, Joel Christner >> > wrote: >> >> Hello, >> >> I hope you guys don't mind another beginner's question, I'm >> waiting on approval from the Yahoo! group moderator for the >> beginner's section. >> >> I'm trying to implement a toplevel function that will accept input >> from stdin (someone running the program will do ./programname < >> someinputfile), store it to a ref string list, and then once >> stored, iteratively evaluate each item in the list individually. >> >> What I've put together is: >> >> let _ = >> let lexbuf = Lexing.from_channel stdin in >> let rec storeoriginalprogram = >> let expr = Parser.expr Scanner.token lexbuf in >> match expr with >> | EOF -> () >> | _ -> add_text originalprogramcontents expr; >> storeoriginalprogram >> in let parseprogram = List.iter >> (fun n -> eval expr) originalprogramcontents >> >> where... >> - Parser and Scanner are already built and working great >> - When a line of text comes in, it calls 'add_text' which adds the >> expr into the 'originalprogramcontents' ref string list, and then >> recursively calls itself to get the next line >> - let originalprogramcontents = ref [""] >> - let add_text variablelist text = variablelist.contents <- >> text::variablelist.contents >> - Then when finished reading from stdin, iteratively executes >> 'eval' against each line in the ref string list called >> 'originalprogramcontents' >> >> For some reason (probably a stupid mistake on my part) it's giving >> me a syntax error and pointing to a line of code that is AFTER the >> end of my program: >> >> $ make >> ocamlc -c program.ml >> File "program.ml ", line 203, characters 2-2: >> Syntax error >> make: *** [program.cmo] Error 2 >> >> But my program is only 202 lines long. >> >> Any ideas on how I might go about making this work? >> >> Thanks >> Joel >> >> >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> Caml-list mailing list. Subscription management: >> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list >> Archives: http://caml.inria.fr >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-bugs >> > > -- Cédric AUGER Univ Paris-Sud, Laboratoire LRI, UMR 8623, F-91405, Orsay