caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Chris Hecker <checker@d6.com>
To: caml-list@inria.fr
Subject: [Caml-list] explain this Genlex behavior
Date: Thu, 26 Apr 2001 16:53:06 -0700	[thread overview]
Message-ID: <200104262353.QAA16702@smtp3-cm.mail.eni.net> (raw)


I was writing a parser with Genlex, and came across strange behavior.
I was trying to parse two numbers separated by a colon (:), but it was
failing when the second number was negative.  So, "1.0:1.0" would
parse, but "1.0:-1.0" would not.  I know about the greedy lexing of
minus (-) to the number, and in fact that should help me here.  The
strange thing is a semi-colon works fine, so "1.0;-1.0" parses.

I decided to figure out which characters worked and which didn't.
Here's the output of the program below:

# test_it ();;
Passed: } { ` _ ] [ ; . , ) ( 
Failed: ~ | ^ \ @ ? > = < : / - + * ' & % $ # " ! 
Passed: ~ } | { ` _ ^ ] \ [ @ ? > = < ; : / . - , + * ) ( & % $ # ! 
Failed: ' " 

The first pair of pass/fails are for no space between the separator,
so "n:-n", etc.  The second pair are with a space "n: -n".

What's the deal here?

Chris

----------------
open Genlex

(* make a simple expression parser for "n?n" where "?" is a
   parameterized separator *)

let do_it sep space =
  let s = "1.0" ^ sep ^ space ^ "-2.0" in
  let rec parse_it = parser
      [< num1 = parse_num; 'Kwd sep ; num2 = parse_num >] -> num1,num2
  and parse_num = parser
      [< 'Float f1 >] -> f1
    | [< 'Int i1 >] -> float i1 in
  let lex = make_lexer [sep] in
  parse_it (lex(Stream.of_string s))
    
let seps = [
  "!"; "\""; "#"; "$"; "%"; "&"; "'"; "("; ")"; "*"; "+"; ",";
  "-"; "."; "/"; ":"; ";"; "<"; "="; ">"; "?"; "@"; "["; "\\"; "]";
    "^"; "_"; "`"; "{"; "|"; "}"; "~";]

let test_it () =
  let do_test s = 
    let pass,fail = ref [],ref [] in
    List.iter
      ~f:(fun el ->
        try
          ignore (do_it el s);
          pass := el :: !pass;
        with _ ->
          fail := el :: !fail
         ) seps;
    print_string "Passed: ";
    List.iter ~f:(fun el -> Printf.printf "%s " el) !pass;
    print_string "\nFailed: ";
    List.iter ~f:(fun el -> Printf.printf "%s " el) !fail;
    print_newline ()
  in
  do_test "";
  do_test " "
    


-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


                 reply	other threads:[~2001-04-26 23:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=200104262353.QAA16702@smtp3-cm.mail.eni.net \
    --to=checker@d6.com \
    --cc=caml-list@inria.fr \
    /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).