help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: language neutral parser


From: Axel Kittenberger
Subject: Re: language neutral parser
Date: Sat, 20 Oct 2001 14:47:41 +0200

On Saturday 20 October 2001 11:58, Hans Aberg wrote:
> At 18:08 -0500 2001/10/18, Pinku Surana wrote:
> >How about this: bison could process a grammar and generate an XML file
> >containing all relevant info for a parser: parser states, arbitrary
> > actions (in CDATA fields), etc. Then any other language implementation
> > can process the XML file and spit out a parser in their language.
>
> A scheme that I have made use of in another context, but translated as
> though implemented in Bison, is to let Bison to generate a sequence of
> environments, which a special macro language then can process. Then one can
> change output language by merely changing the macro formatting file.
>
> The point with this approach is that it is very easy to generate these
> environments. Writing directly in a computer language is always tricky,
> because one has to sort out where things have to be written.

The idea is to use XML as intermediate language, and so all the tools for it 
avaibable can be used, especially XSL. 
XSL would then be the 'macro' language to generate the specific language. 
"backends". This way we would not need to invent a new language, we take one 
which is acceptable for our needs, and good implementations are already 
available.

The actions generatet by bison could roughly look like this: (this is ie. the 
classic infix calculator)
---
%%
input:
        /* empty string */
    |
        input line
;
 
line:
        '\n'
    |
        exp '\n'  { printf ("\t%d\n", $1); }
;
 
exp:
        NUM
            { $$ = $1;                 }
    |
        exp '+' exp
            { $$ = $1 + $3;    }
    |
        exp '-' exp
            { $$ = $1 - $3;    }
    |
        exp '*' exp
            { $$ = $1 * $3;    }
    |
        exp '/' exp
;
%%
-----

would be translated by bison into something like this:
----
<actions>
   <action value="4">
        <text value="printf ("\t%d\n\", "/>
        <token value="-1" union="int32"/>
        <text value="});"/>
    </action>
   <action value="5">
        <result union="int32/">
        <text value=" = "/>
        <token value=0" union="int32"/>
        <text value=";"/>
   </action>

   ...etc...   
<actions/>
---

Which a standard XSL processor could easily turn to in to the actual code as 
in example:
---
  switch (yyn) {      
case 4:
 printf ("\t%d\n", yyvsp[-1]);
case 5:
 <int32> yyval = <int32> yyvsp[0];
case 6:
 <int32> yyval = <int32> yyvsp[-2] + <int32> yyvsp[0];
case 7:
 <int32> yyval = <int32> yyvsp[-2] - <int32> yyvsp[0];
case 8:
 <int32> yyval = <int32> yyvsp[-2] * <int32> yyvsp[0];
case 9:
 <int32> yyval = <int32> yyvsp[-2] / <int32> yyvsp[0];
case 10:
 <int32> yyval = - <int32> yyvsp[0];
case 11:
 <int32> yyval = <int32> yyvsp[-1];
}
---

I only hope that no "poltical" issues will also find it's way here hindering 
technical development, like ie. the FSF not owning itself an XML 
implementation. Libxml2 is available under the LGPL.

- Axel
-- 
|D) http://www.dtone.org



reply via email to

[Prev in Thread] Current Thread [Next in Thread]