Skip to content

Defines in rules block are not propagated to use blocks #291

@pwnalone

Description

@pwnalone

Example

#include <assert.h>
#include <string.h>


/*!rules:re2c
    re2c:define:YYLIMIT  = lim;
    re2c:define:YYCURSOR = cur;
    re2c:define:YYMARKER = mrk;
    re2c:yych:emit = 0;
    re2c:define:YYGETCONDITION = 'condition';
    re2c:define:YYGETCONDITION:naked = 1;
    re2c:define:YYSETCONDITION = 'condition = @@;';
    re2c:define:YYSETCONDITION:naked = 1;
    re2c:yyfill:enable = 0;
    re2c:eof = 0;

    <init> *                                        { return -1; }
    <init> $                                        { return  0; }

    <init> [/][/] .*                                { goto loop; }
    <init> [/][*]                   :=> comment

    <init> [ \t]+                                   { goto loop; }

    <init, comment> [\n]+                           { goto loop; }

    <comment> [^*\n]+ [*]*          :=> comment
    <comment> [^*\n]* [*]+          :=> comment
    <comment> [^*\n]* [*]+ [/]       => init        { goto loop; }

    <comment> *                     :=> comment
    <comment> $                                     { return -1; }
*/


/*!types:re2c */

int lex(const char* cur)
{
    const char* lim;
    const char* mrk;

    char yych;

    int condition = yycinit;

    lim = cur + strlen(cur);
loop:
    mrk = cur;

    /*!use:re2c
        re2c:indent:top = 1;
        re2c:define:YYCTYPE = char;
    */
}


int main(void)
{
    assert(!lex("/* hello, */ // world !"));

    return 0;
}

Compile

re2c -W -Werror -c -r -o bug.c bug.re && gcc -o bug bug.c

Compilation Errors

The re2c:define:... commands in the !rules block are not propagated to the !use block, and so re2c ends up outputting the default identifiers instead. These are undeclared, so we end up with the following compilation errors:

bug.c: In function ‘lex’:
bug.c:34:10: error: ‘YYGETCONDITION’ undeclared (first use in this function)
   34 |  switch (YYGETCONDITION) {
      |          ^~~~~~~~~~~~~~
bug.c:34:10: note: each undeclared identifier is reported only once for each function it appears in
bug.c:42:10: error: ‘YYCURSOR’ undeclared (first use in this function)
   42 |  yych = *YYCURSOR;
      |          ^~~~~~~~
bug.c:49:7: error: ‘YYLIMIT’ undeclared (first use in this function)
   49 |   if (YYLIMIT <= YYCURSOR) goto yyeof2_init;
      |       ^~~~~~~
bug.c:88:2: error: ‘YYSETCONDITION’ undeclared (first use in this function)
   88 |  YYSETCONDITION
      |  ^~~~~~~~~~~~~~
bug.c:88:16: error: expected ‘;’ before ‘goto’
   88 |  YYSETCONDITION
      |                ^
      |                ;
   89 |  goto yyc_comment;
      |  ~~~~
bug.c:155:16: error: expected ‘;’ before ‘{’ token
  155 |  YYSETCONDITION
      |                ^
      |                ;
bug.c:113:3: error: label ‘yyeof2_comment’ used but not defined
  113 |   if (YYLIMIT <= YYCURSOR) goto yyeof2_comment;
      |   ^~

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions