Browse thread
[Caml-list] What's wrong with my parser ?
-
Nicolas FRANCOIS
- John Prevost
- John Prevost
- Nicolas FRANCOIS
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Nicolas FRANCOIS <nicolas.francois@f...> |
| 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