help-bison
[Top][All Lists]
Advanced

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

Re: changing lex rules from yacc


From: Hans Aberg
Subject: Re: changing lex rules from yacc
Date: Thu, 19 Apr 2001 11:12:28 +0200

At 08:27 +0200 2001/04/19, address@hidden wrote:
>       Is it possible to change the lex rules (via the starts) through
>yacc.

I have done it with Flex and Bison, but not with Lex and Yacc.

> toto.lex
> -------
>> a {return A}}  // A typo here.
>> b {return B;}
>> <AA>[^ab$]* {return T;}
>> [^abd]* {return T;}
>>
>> toto.yacc
>> ---------
>> %token A B T
>> %start message
>> %%
>> message : expr;
>> expr :       A{BEGIN AA;}BT{BEGIN 0;} | BT;

As Bison isn't back-tracking, this looks OK.

One can also make the context switching in Flex like (in the .lex file):
%{
int compiler_context = 0;
%}

%%
  switch (compiler_context) {
    case cpp_header_name: BEGIN(cpp_header_lex); break;
    case cpp_std_header_name: BEGIN(cpp_std_header_lex); break;
    default: break;
  }

<cpp_header_lex>{ ... }
<cpp_std_header_lex>{ ... }

This way, the Bison parser need not know about the cpp_header_lex etc macro
values, but can set the variable compiler_context instead. As I compile the
Flex and Bison output C/C++ files separately, I preferred this approach.

The Bison input file looks like:

%token cpp_header_name, cpp_std_header_name

%%
...
include_header:
    '"' { compiler_context = cpp_header_name } cpp_header_name '"'
      { (*table)["include_header"][$3.text]["file_name"] = $3.text; }
  | '<' { compiler_context = cpp_std_header_name } cpp_std_header_name '>'
      { (*table)["include_std"][$3.text]["file_name"] = $3.text; }

The macro values cpp_header_name, cpp_std_header_name are put in a .h file
that Flex lexer reads.

So it is possible to fiddle around with various possibilities.

  Hans Aberg





reply via email to

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