caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* parsing problem.
@ 2007-01-23 23:59 Pietro Abate
  2007-01-24 21:53 ` using camlp4 api [WAS: Re: [Caml-list] parsing problem.] Pietro Abate
  0 siblings, 1 reply; 4+ messages in thread
From: Pietro Abate @ 2007-01-23 23:59 UTC (permalink / raw)
  To: ocaml ml

hi all,
I've a file that is composed of two parts: a language specification and
a program that is written using the language specified in the first
part. My goal is to parse the language spec, create a parser that uses
this language, parse the rest of the file and output ocaml code (this
should be a preprocessor).

At the moment I'm using camlp4 to read the language specification,
extend a simple fixed grammar on the fly and to parse the rest of the
file using the grammar that I've extended with the language
specification. For example if my fix grammar is a simple generic
calculator language and I want to add more operators to the language
I'll write something along these lines:

_+_ , 2
_*_ , 1
-_  , 0

and then then the second part of the file will look like

1 + 2 * -3 

and I expect that this line will be parsed accordingly to the operators
declared at the beginning.

Obviously this approach is not going to take me very far way. In the
moment I need to specify a different grammar (say to write equations)
I've to change the parser in the preprocessor.

What I really want is to write the grammar and lexer specification in
the first part of my file and then use a parser generator to create a
parser to interpret the second part of the file. The catch is that I'd
like to do it in one pass. Using Lex/Yacc technology this is not
possible (I think) as you need to write two input files in order to
generate a parser library. I'm wondering if this is possible using a
generic parser combinators library.

The idea would be to link a generic parser library and bootstrap my
custom parser by feeding it with the grammar specification and then use
this parser within or together with camlp4 to parse the rest of the
file.

The result of this pre-processing step should be ocaml code that I'll
just need to compile.
input -> campl4 extension + generic parser library -> ocaml code

Is there a (small !) library to achieve this ?

I had a quick look at 

Ocfgc (http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=492) can it be
used in this way ? 

Ostap
(http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=513) seems also promising, 
but I'm not sure if it is possible to plug it in this way.

ulex ? menhir ?  dypgen ?

thanks :)
pietro

-- 
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++ 
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html


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

* using camlp4 api [WAS: Re: [Caml-list] parsing problem.]
  2007-01-23 23:59 parsing problem Pietro Abate
@ 2007-01-24 21:53 ` Pietro Abate
  2007-01-24 22:37   ` Martin Jambon
  0 siblings, 1 reply; 4+ messages in thread
From: Pietro Abate @ 2007-01-24 21:53 UTC (permalink / raw)
  To: caml-list, ocaml ml

ok, it seems my question was a bit too general or impenetrable...

dypgen (http://perso.ens-lyon.fr/emmanuel.onzon/) has an example of something
very similar to what I want to achieve.  By defining a small parser for this
language, then it is possible to interpret the code that follows:

define
  list_contents := expr(x) = List(x,Nil)
  and list_contents := expr(x);list_contents(y) = List(x,y)
  and expr := [] = Nil
  and expr := [list_contents(x)] = x
  and expr := expr(x)::expr(y) = List(x,y)
in
match [1;2;3] with
  | a::[b;c] -> b::[]
  | _ -> [4]

where in the first part there is a grammar definition and in the second part
the grammar is used to parse the argument of the match statement.

my question: Is it possible to do something similar with camlp4 ?

In the library documentation I can see that this might be possible, but I don't
quite understand where to start. The first step should be to write a small
parser to interpret the language definition and then to generate, by using the
Grammar (extensible grammars) module, other production that extend the current
grammar to parse the rest of the file. The trick part is that I cannot write (I
think) and EXTEND statement on the fly (as it should be parsed itself), but I
have to use the quotation library directly, and this is very verbose...

Did anybody do anything similar ? One small example ?

thanks for your help...

:)
p

On Wed, Jan 24, 2007 at 10:59:40AM +1100, Pietro Abate wrote:
> hi all,
> I've a file that is composed of two parts: a language specification and
> a program that is written using the language specified in the first
> part. My goal is to parse the language spec, create a parser that uses
> this language, parse the rest of the file and output ocaml code (this
> should be a preprocessor).
> 
> At the moment I'm using camlp4 to read the language specification,
> extend a simple fixed grammar on the fly and to parse the rest of the
> file using the grammar that I've extended with the language
> specification. For example if my fix grammar is a simple generic
> calculator language and I want to add more operators to the language
> I'll write something along these lines:
> 
> _+_ , 2
> _*_ , 1
> -_  , 0
> 
> and then then the second part of the file will look like
> 
> 1 + 2 * -3 
> 
> and I expect that this line will be parsed accordingly to the operators
> declared at the beginning.
> 
> Obviously this approach is not going to take me very far way. In the
> moment I need to specify a different grammar (say to write equations)
> I've to change the parser in the preprocessor.
> 
> What I really want is to write the grammar and lexer specification in
> the first part of my file and then use a parser generator to create a
> parser to interpret the second part of the file. The catch is that I'd
> like to do it in one pass. Using Lex/Yacc technology this is not
> possible (I think) as you need to write two input files in order to
> generate a parser library. I'm wondering if this is possible using a
> generic parser combinators library.
> 
> The idea would be to link a generic parser library and bootstrap my
> custom parser by feeding it with the grammar specification and then use
> this parser within or together with camlp4 to parse the rest of the
> file.
> 
> The result of this pre-processing step should be ocaml code that I'll
> just need to compile.
> input -> campl4 extension + generic parser library -> ocaml code
> 
> Is there a (small !) library to achieve this ?
> 
> I had a quick look at 
> 
> Ocfgc (http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=492) can it be
> used in this way ? 
> 
> Ostap
> (http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=513) seems also promising, 
> but I'm not sure if it is possible to plug it in this way.
> 
> ulex ? menhir ?  dypgen ?
> 
> thanks :)
> pietro
> 
> -- 
> ++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
> ++ 
> ++ "All great truths begin as blasphemies." -George Bernard Shaw
> ++ Please avoid sending me Word or PowerPoint attachments.
>    See http://www.fsf.org/philosophy/no-word-attachments.html
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

-- 
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++ 
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html


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

* Re: using camlp4 api [WAS: Re: [Caml-list] parsing problem.]
  2007-01-24 21:53 ` using camlp4 api [WAS: Re: [Caml-list] parsing problem.] Pietro Abate
@ 2007-01-24 22:37   ` Martin Jambon
  2007-01-25  9:36     ` Nicolas Pouillard
  0 siblings, 1 reply; 4+ messages in thread
From: Martin Jambon @ 2007-01-24 22:37 UTC (permalink / raw)
  To: Pietro Abate; +Cc: caml-list, ocaml ml

On Thu, 25 Jan 2007, Pietro Abate wrote:

> where in the first part there is a grammar definition and in the second part
> the grammar is used to parse the argument of the match statement.
>
> my question: Is it possible to do something similar with camlp4 ?
>
> In the library documentation I can see that this might be possible, but I don't
> quite understand where to start. The first step should be to write a small
> parser to interpret the language definition and then to generate, by using the
> Grammar (extensible grammars) module, other production that extend the current
> grammar to parse the rest of the file. The trick part is that I cannot write (I
> think) and EXTEND statement on the fly (as it should be parsed itself), but I
> have to use the quotation library directly, and this is very verbose...
>
> Did anybody do anything similar ? One small example ?

No :-)

Take pa_extend.ml and unquote the quotations?
(and solve the type problems which might not be so bad)


Martin

--
Martin Jambon
http://martin.jambon.free.fr


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

* Re: using camlp4 api [WAS: Re: [Caml-list] parsing problem.]
  2007-01-24 22:37   ` Martin Jambon
@ 2007-01-25  9:36     ` Nicolas Pouillard
  0 siblings, 0 replies; 4+ messages in thread
From: Nicolas Pouillard @ 2007-01-25  9:36 UTC (permalink / raw)
  To: Martin Jambon; +Cc: Pietro Abate, ocaml ml

On 1/24/07, Martin Jambon <martin.jambon@ens-lyon.org> wrote:
> On Thu, 25 Jan 2007, Pietro Abate wrote:
>
> > where in the first part there is a grammar definition and in the second part
> > the grammar is used to parse the argument of the match statement.
> >
> > my question: Is it possible to do something similar with camlp4 ?
> >
> > In the library documentation I can see that this might be possible, but I don't
> > quite understand where to start. The first step should be to write a small
> > parser to interpret the language definition and then to generate, by using the
> > Grammar (extensible grammars) module, other production that extend the current
> > grammar to parse the rest of the file. The trick part is that I cannot write (I
> > think) and EXTEND statement on the fly (as it should be parsed itself), but I
> > have to use the quotation library directly, and this is very verbose...
> >
> > Did anybody do anything similar ? One small example ?
>

Yes, Coq is one example but not a small one :)

Indeed Coq parsing uses camlp4. And Coq have a syntax extensible on the fly.

Coq < Definition myeq (x : nat) (y : nat) := x = y.
myeq is defined

Coq < Notation "x '=?=' y" := (myeq x y) (at level 70, no associativity).

Coq < Check (3 =?= 4).
3 =?= 4
         : Prop

-- 
Nicolas Pouillard


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

end of thread, other threads:[~2007-01-25  9:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-23 23:59 parsing problem Pietro Abate
2007-01-24 21:53 ` using camlp4 api [WAS: Re: [Caml-list] parsing problem.] Pietro Abate
2007-01-24 22:37   ` Martin Jambon
2007-01-25  9:36     ` Nicolas Pouillard

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