首页 > 解决方案 > How to match whitespace and comments with re2c

问题描述

I started very recently to use bison for writing small compiler exercises. I am having some issues with white spaces ans comments. I was trying to debug the problem and I arrived to this source that looks like what I am looking for. I tried to chnage and erase some characters as advised but didn't work. Also during compilation I have the following error: re2c: error: line 2963, column 0: can only difference char sets. Below the part of the code:

yy::conj_parser::symbol_type yy::yylex(lexcontext& ctx)
{
        const char* anchor = ctx.cursor;
        ctx.loc.step();
        // Add a lambda function to avoid repetition
        auto s = [&](auto func, auto&&... params) { ctx.loc.columns(ctx.cursor - anchor); return func(params..., ctx.loc); };

%{  /* Begin re2c lexer : Tokenization process starts */

re2c:yyfill:enable    = 0;
re2c:define:YYCTYPE   = "char";
re2c:define:YYCURSOR  = "ctx.cursor";

"return"          { return s(conj_parser::make_RETURN); }
"while" | "for"   { return s(conj_parser::make_WHILE);  }
"var"             { return s(conj_parser::make_VAR);    }
"if"              { return s(conj_parser::make_IF);     }

// Identifiers
[a-zA-Z_] [a-zA-Z_0-9]*  { return s(conj_parser::make_IDENTIFIER, std::string(anchor, ctx.cursor)); }

// String and integers:
"\""" [^\"]* "\""      { return s(conj_parser::make_STRINGCONST, std::string(anchor+1, ctx.cursor-1)); }
[0-9]+                 { return s(conj_parser::make_NUMCONST, std::stol(std::string(anchor, ctx.cursor))); }

// Whitespace and comments:
"\000"               { return s(conj_parser::make_END);      }
"\r\n" | [\r\n]      { ctx.loc.lines();   return yylex(ctx); }
"//" [^\r\n]*        {                    return yylex(ctx); }
[\t\v\b\f ]          { ctx.loc.columns(); return yylex(ctx); }

Thank you very much for pointing in the right direction or shading some lights on why this error could be solved.

标签: parsinglexical-analysisre2c

解决方案


You really should mention which line is line 2963. Perhaps it is here, because there seems to be an extra quotation mark in that line.

"\""" [^\"]* "\""
    ^

推荐阅读