Version française
Home     About     Download     Resources     Contact us    
Browse thread
bug in "developing applications with objective caml" (english translation)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] some comments on ocaml{lex,yacc} from a novice's POV
From: "Jack Andrews" <effbiae@ivorykite.com>

> this is a little long.  i'm new to ocaml, but like most, have been
> educated in FLs and experimented with and applied functional languages and
> techniques.  python has been the first language i turn to for a few years
> now.
> 
> i need to parse text as a sequence of records (with odd variations). i
> have used ply (python lex-yacc) most recently for parsing and believe it
> to be one of the more elegant mechanisms i've seen. 
> http://systems.cs.uchicago.edu/ply/ply.html
> 
> elegant because there are no lex and yacc input files, but rather the
> tokens and grammar rules are defined in python code -- succinctly!  eg:
> 
> # calclex.py
> import lex
> tokens = ( 'NUMBER', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'LPAREN', 'RPAREN',)
> t_PLUS    = r'\+'  # in python, the r prefix to a string literal
> t_MINUS   = r'-'   #  means as-is.  r'\' in python is "\\" in c
> [snip]

Interestingly, your example corresponds exactly to the one in the
ocaml tutorial, where it is solved using stream parsers.
Stream parsers are a bit more involved than just writing yacc rules,
but they give you more control on how to combine rules (you can write
parser combinators.) And they are completely integrated in the
language using camlp4.

Alternatively, you can also use ocamllex/ocamlyacc (a little bit more
overhead, but really easy once you're set up), or use the Scanf module
(if you don't need a real parser.)

By the way, Caml 3.1 had complete yacc integration (15 years ago).
I suppose this was considered too monolithic, but it was very nice to
use in practice.

Jacques Garrigue