caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Toplevel function question
@ 2009-05-13 23:43 Joel Christner
  2009-05-14  0:10 ` Joel Christner
  0 siblings, 1 reply; 6+ messages in thread
From: Joel Christner @ 2009-05-13 23:43 UTC (permalink / raw)
  To: caml-list, Joel Christner

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

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

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

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

* Re: Toplevel function question
  2009-05-13 23:43 Toplevel function question Joel Christner
@ 2009-05-14  0:10 ` Joel Christner
  2009-05-14  5:25   ` RE : [Caml-list] " MONATE Benjamin 205998
                     ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Joel Christner @ 2009-05-14  0:10 UTC (permalink / raw)
  To: caml-list, Joel Christner

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

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




On Wed, May 13, 2009 at 4:43 PM, Joel Christner <joel.christner@gmail.com>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
>
>

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

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

* RE : [Caml-list] Re: Toplevel function question
  2009-05-14  0:10 ` Joel Christner
@ 2009-05-14  5:25   ` MONATE Benjamin 205998
  2009-05-14  7:52   ` Gregory BELLIER
  2009-05-14 10:23   ` Cedric Auger
  2 siblings, 0 replies; 6+ messages in thread
From: MONATE Benjamin 205998 @ 2009-05-14  5:25 UTC (permalink / raw)
  To: Joel Christner, caml-list

Hi,

The last "let" is missing its corresponding "in".
 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
  in parsepreogram

You may also remove the last "let": 

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 
  List.iter
    (fun n -> print_string n; print_string "\n";) originalprogramcontents

Hope this helps.
--
Benjamin Monate
CEA LIST
Head of Software Safety Lab.


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

* Re: [Caml-list] Re: Toplevel function question
  2009-05-14  0:10 ` Joel Christner
  2009-05-14  5:25   ` RE : [Caml-list] " MONATE Benjamin 205998
@ 2009-05-14  7:52   ` Gregory BELLIER
  2009-05-14 10:23   ` Cedric Auger
  2 siblings, 0 replies; 6+ messages in thread
From: Gregory BELLIER @ 2009-05-14  7:52 UTC (permalink / raw)
  To: Joel Christner; +Cc: caml-list

Hello !

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
>

Please refer to the last email from John Li.
variablelist := test :: !variablelist


> 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
Here you should call _*!*_originalprogramcontents which seems to be 
empty with your example because you declare the function 
storeoriginalprogram but you don't use/call it.

Try something like :

let myvariable = 1 in

let rec fun var =
       match var with
    | val1 -> ...
    | val2 -> ...
    | _ -> ...
in
    fun my_variable   ( <<--- this is what you forgot )

Gregory.


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

* Re: [Caml-list] Re: Toplevel function question
  2009-05-14  0:10 ` Joel Christner
  2009-05-14  5:25   ` RE : [Caml-list] " MONATE Benjamin 205998
  2009-05-14  7:52   ` Gregory BELLIER
@ 2009-05-14 10:23   ` Cedric Auger
  2009-05-14 10:29     ` Cedric Auger
  2 siblings, 1 reply; 6+ messages in thread
From: Cedric Auger @ 2009-05-14 10:23 UTC (permalink / raw)
  To: Joel Christner; +Cc: caml-list

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 <http://toplevel.ml>
> File "toplevel.ml <http://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 
> <joel.christner@gmail.com <mailto:joel.christner@gmail.com>> 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 <http://program.ml>
>     File "program.ml <http://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


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

* Re: [Caml-list] Re: Toplevel function question
  2009-05-14 10:23   ` Cedric Auger
@ 2009-05-14 10:29     ` Cedric Auger
  0 siblings, 0 replies; 6+ messages in thread
From: Cedric Auger @ 2009-05-14 10:29 UTC (permalink / raw)
  Cc: Joel Christner, caml-list

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 <http://toplevel.ml>
>> File "toplevel.ml <http://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 
>> <joel.christner@gmail.com <mailto:joel.christner@gmail.com>> 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 <http://program.ml>
>>     File "program.ml <http://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


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

end of thread, other threads:[~2009-05-14 10:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-13 23:43 Toplevel function question Joel Christner
2009-05-14  0:10 ` Joel Christner
2009-05-14  5:25   ` RE : [Caml-list] " MONATE Benjamin 205998
2009-05-14  7:52   ` Gregory BELLIER
2009-05-14 10:23   ` Cedric Auger
2009-05-14 10:29     ` Cedric Auger

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).