caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] genlex ...
@ 2003-07-07 10:13 Pietro Abate
  2003-07-08  0:34 ` Pietro Abate
  0 siblings, 1 reply; 2+ messages in thread
From: Pietro Abate @ 2003-07-07 10:13 UTC (permalink / raw)
  To: caml-list

Hi all,
I'm re-writing the parser of my application and I'd like to write a kind
of generic parser that can parse a language that I can change dynamically... 
In other words... My application is composed by a library, a set of
modules that are compiled separately and dynamically loaded and an
application that get the name of a module, a file and it parses the file
differently regarding the definions that it loads from the dynamic
module.

My point is that I want to have a generic parser that given a set of
keyword is able to parse different input.

so far I realized that I cannot do that with lex/yacc as they need a
compilation step (I could generate the lex/yacc file from the dynamic
module, but I think it's too messy...). But I can use Genlex.

Since I'm using Dynlink it's strightforward to load at runtime a
different set of keyword and initialized the lexer as :
let flexer = make_lexer Stub.variable_keyword_list

but at this point I don't know how to use the Genlex.parser to feed it
with my generic rules (every connective is declared as left assoc or non
assoc and the generic language is a calculator-like one ... )

I'd like to write something like

expr:
	| LBRACK expr RBRACK         { $2 } 
	| expr (keyword buf) expr    { Tree(keyword_lookup buf,$1,$3)
	| (keyword buf) expr         { Leaf(keyword_lookup buf,$2)
	| IDENT                      { Var($1)

where (keyword buf) is a function that returns 'Kwd if buf is a keyword
or fails ... Or using Genlex.parser as :

let rec parse_level1 = parser
	[< e1 = parse_level0; e = parse_level11 e1 >] -> e
and parse_level11 e1 = parser
	[< (keyword buf); e2 = parse_level1 >] ->
	| [< >] -> e1
and parse_level0 = parser
	[< (keyword buf); e = parse_level0 >] ->
	 ....
 
is it possible ? does anybody can give me a small example ?

tnx,
P

-- 
Civilization advances by extending the number
of important operations which we can perform 
without thinking. (Alfred North Whitehead)

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


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

* Re: [Caml-list] genlex ...
  2003-07-07 10:13 [Caml-list] genlex Pietro Abate
@ 2003-07-08  0:34 ` Pietro Abate
  0 siblings, 0 replies; 2+ messages in thread
From: Pietro Abate @ 2003-07-08  0:34 UTC (permalink / raw)
  To: caml-list

Hi all, I solved my problem...
On Mon, Jul 07, 2003 at 08:13:44PM +1000, Pietro Abate wrote:
> I'm re-writing the parser of my application and I'd like to write a kind
> of generic parser that can parse a language that I can change dynamically... 

that's a piece of code that gives me keyword flexibility as I wanted...
(conn is an object that stores my keyword with their associativity).

let flexer = make_lexer (
    conn#keywords @
    ["(" ; ")"; "atom" ]
);;

let find_keyword level = parser
    [< 'Kwd x >] ->
        let v =
            try conn#lookup x
            with Not_found -> raise Stream.Failure
        in
        if (conn#assoc v = level) then x
        else raise Stream.Failure
;;

let rec parse_main = parser
    [< e1 = parse_aux; e = parse_expr e1 >] -> e
and parse_expr e1 = parser
    [< n = find_keyword (Connective.Left); e2 = parse_main >] ->
        term_h#node n [e1;e2]
    | [< >] -> e1
and parse_aux = parser
    [< n = find_keyword (Connective.NonAssoc); e = parse_aux >] ->
        term_h#node n [e]
    | [< 'Ident s >] -> term_h#atom s
    | [< 'Kwd "("; e = parse_main; 'Kwd ")" >] -> e

I haven't test it yet, but it seems to make sense and compile correctly.

p
-- 
Civilization advances by extending the number
of important operations which we can perform 
without thinking. (Alfred North Whitehead)

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


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

end of thread, other threads:[~2003-07-08  0:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-07 10:13 [Caml-list] genlex Pietro Abate
2003-07-08  0:34 ` Pietro Abate

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