help-bison
[Top][All Lists]
Advanced

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

Re: Calling a parser from a parser


From: Laurence Finston
Subject: Re: Calling a parser from a parser
Date: Sat, 23 Oct 2004 11:46:07 +0200 (MEST)

On Fri, 22 Oct 2004, Antoine Fink wrote:

> Hi,
>
> Is it possible with Bison/Yacc to parse a language using 2 parsers for
> different sections ?
>

It depends.

> Example :
> I have this language :
> ----------------------
> LG[0] {
>  0 : abc : 1, 2;
>  1 : bcd : 3, 4, 5;
>  2 : x   : 3;
>  3 : abc : 3, 4;
>  BB[x] {
>    a -> 1,2;
>    b -> 2,4;
>    c -> 3;
>    d -> ;
>    x -> 1,2,3,4,5;
>  }
>  4 : bc  : 5;
>  5 : abc : 1,4,5;
> };
> ----------------------
>
> I would like to have lg_parser.y / lg_lexer.lex to parse all the lines
> including colons (":") and use bb_parser.y / bb_parser.lex to parse all
> the lines including arrows ("->").


> How can I invoke yyparse from bb_parser when alreading in the middle of
> parsing the same file with lb_parser, and then return to lb_parser to
> read the remaining lines ?

You can have an arbitrary number of input streams attached to a file,
subject to restrictions that your system may impose.  If the file is
opened read-only, then it wouldn't be a problem to read it in twice
using different streams.  You will have to keep track of your position in
the file and the functions that read in from it will both have to have
access to this information and be able to update it.  If you are using
threads, this variable will have to be protected by a mutex.

> Say, the top level parsing function would
> read "LG", and invoke lg_parser, and when lg_parser reads "BB", it would
> have the following lines parsed by bb_parser up to next closing braket,
> and return to lg_parser to parse the rest.
>

Are the parses completely separate, or do they influence one
another?  If the former, then the problem is simple.  However, in this
case, there would probably be no point in interleaving the input.  If the
latter, then
what you seem to want is similar to the effect achieved by using "start
conditions" in Flex.  It may be that using this feature, if you're using
Flex, or implementing a similar one, if you've written your own version of
`yylex()', might be a better way of solving the problem.  In short, if
it's just a matter of interpreting tokens differently under certain
circumstances, I believe this is a scanning problem rather than a parsing
problem.

Laurence




reply via email to

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