<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/07/16456967beea1ab5004955e83b294d89"
  from="Pietro Abate &lt;Pietro.Abate@a...&gt;"
  author="Pietro Abate"
  date="2003-07-08T00:34:05"
  subject="Re: [Caml-list] genlex ..."
  prev="2003/07/1eccca803cd78c2ed7f87d84c07cacd3"
  next="2003/07/d4567e135396aa37ca95126cae72c96f"
  prev-in-thread="2003/07/a271f4688bedcc68d89571a4f7b2cc1f"
  prev-thread="2003/07/ce2eb56d389991d2a9c2107a035618d4"
  next-thread="2003/07/d4567e135396aa37ca95126cae72c96f"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] genlex ...">
<msg 
  url="2003/07/a271f4688bedcc68d89571a4f7b2cc1f"
  from="Pietro Abate &lt;Pietro.Abate@a...&gt;"
  author="Pietro Abate"
  date="2003-07-07T10:13:34"
  subject="[Caml-list] genlex ...">
<msg 
  url="2003/07/16456967beea1ab5004955e83b294d89"
  from="Pietro Abate &lt;Pietro.Abate@a...&gt;"
  author="Pietro Abate"
  date="2003-07-08T00:34:05"
  subject="Re: [Caml-list] genlex ...">
</msg>
</msg>
</thread>

<contents>
Hi all, I solved my problem...
On Mon, Jul 07, 2003 at 08:13:44PM +1000, Pietro Abate wrote:
&gt; I'm re-writing the parser of my application and I'd like to write a kind
&gt; 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
    [&lt; 'Kwd x &gt;] -&gt;
        let v =
            try conn#lookup x
            with Not_found -&gt; raise Stream.Failure
        in
        if (conn#assoc v = level) then x
        else raise Stream.Failure
;;

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

</contents>

</message>

