help-bison
[Top][All Lists]
Advanced

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

Re: "Eating" comments: not with Flex but with Bison


From: Tim Van Holder
Subject: Re: "Eating" comments: not with Flex but with Bison
Date: Tue, 14 Jun 2005 17:15:11 +0200
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

Frans Englich wrote:
> On Tuesday 14 June 2005 11:36, Akim Demaille wrote:
> 
>>>>>"Frans" == Frans Englich <address@hidden> writes:
>>
>> >
>> > I would prefer to do this at the Bison/Parser level because it is
>> > convenient: I have access to various information passed to the
>> > parse function,
>>
>>You can easily make them available to the scanner.  And in fact, you
>>probably should, to have a clean, pure, interface bw the two.
> 
> 
> Ok, not fully following here, what you mean with "make them available to the 
> scanner". 
> [snip]
> Hm, then have error handling been placed in the scanner, which confuses me 
> with respect to "You can easily make them available to the scanner. And in 
> fact, you probably should, to have a clean, pure, interface bw the 
> two."(assuming my interpretation is correct).

What Akim meant is that whatever information you pass to the parser
should also be passed down to the lexer.

Example:

=> in parserctx.h:

struct my_parse_context {
  bool comments_allowed;
};

=> parser invocation:
...
struct my_parse_context pc;
  fooparse(&pc);
...

=> in foo.y:

%{
...
#include "parserctx.h"
...
%}

%parse-param {my_parse_context* context}
%lex-param {my_parse_context* context}

...

=> in foo.l:

%{
...
#include "parserctx.h"
#define YY_DECL int yylex(my_parse_context* context)
...
%}

...

%%

{COMMENT} {
  if (!context->comments_allowed) {
    /* alternative: yyerror() and exit() if this is a fatal problem */
    REJECT;
  }
}



This way it becomes easy to pass information from the caller to parser &
lexer, and/or between lexer and parser.  It also avoids using global
variables for such purposes, which keeps things thread-safe.




reply via email to

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