help-bison
[Top][All Lists]
Advanced

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

Re: Modularized parser files covering similar grammars


From: Akim Demaille
Subject: Re: Modularized parser files covering similar grammars
Date: Mon, 13 Jun 2005 16:02:06 +0200
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

>>> "Frans" == Frans Englich <address@hidden> writes:

 > My wondering is how I practically should modularize the code in order to 
 > efficiently support these different languages.

In the future, I would like to have something like %import in Bison,
but currently, you'll have to put everything into a single file (or
run your own process beforehand).

 > What are people's experiences with these kind of problems? What are the 
 > approaches for solving them?

I don't know how similar/different are your different languages, but
if they share some large parts, say there are common sublanguages
covered by equal nonterminals, then the following technique might
useful.

I have two similar languages (in fact it's almost a single grammar
with two entry points).  First, in the parser I have:

%start program
%%
program:
  /* Parsing a source program.  */
  "seed-source" exp                     { tp.exp_ = $2; }
| /* Parsing an imported file.  */
  "seed-import" "let" decs "end"        { tp.decs_ = $3; }
;

In fact I'm looking either for `exp' or `"let" decs "end"', but I have
fake tokens seed-*.

Then in the (Flex) scanner, I have:

...
%%
%{
  /* Be ready to insert the seed. */
  if (seed)
    {
      int res = seed;
      seed = 0;
      return res;
    }
%}

where seed is initialized in some way to the first token you want to
send (that depends whether your parser is pure or not etc.).

There is no limitation on the number of initial tokens, i.e., the
actual number of start symbols.





reply via email to

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