Accueil     À propos     Téléchargement     Ressources     Contactez-nous

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] What's wrong with my parser ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2002-07-13 (11:19) From: Nicolas FRANCOIS Subject: Re: [Caml-list] What's wrong with my parser ?
```OK, this is my new polynom parser, it seems to work OK, thanks to your
explanations.

let rec parse s =
let parse_puissance = parser
| [< ''^'; e = parse_int >] -> e
| [< >] -> 1
in
let parse_x = parser
| [< ''X'; e = parse_puissance >] -> e
| [< >] -> 0
in
let parse_coeff = parser
| [< n = R.parse >] -> n
| [< >] -> R.one
in
let parse_monome = parser
| [< n = parse_coeff; e = parse_x >] ->
monome n e
| [< e = parse_x >] ->
monome R.one e
in
let rec parse_suite = parser
| [< ''+'; m = parse_monome; p = parse_suite >] ->
m ++ p
| [< ''-'; m = parse_monome; p = parse_suite >] ->
(opp m) ++ p
| [< >] -> zero
in
match s with parser
| [< m = parse_monome >] ->
match parse_suite s with
| p -> m ++ p

and the results :

# print (P.parse (Stream.of_string "-1+3X+4X^2"));;
[3: -1, 3, 4]- : unit = ()
# print (P.parse (Stream.of_string ""));;
[1: 1]- : unit = ()

Oups !!

What do you think about this ?

\bye

--

Nicolas FRANCOIS
http://nicolas.francois.free.fr
A TRUE Klingon programmer does NOT comment his code
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

```