0

I have to create a lexical and syntax analyzer for a c-like language. In this language we define as comment "everything that exists after the symbol // until the end of line". Everytime my compiler gets to the "/" it still gives me syntax errors

%%

[ \t]                   { } 
[0-9]+                  { SAVE_TOKEN; return TINTEGER; }

"main_loop"             { return TOKEN(TMAIN); }
"{"                     { return TOKEN(TLBRACE); }
"}"                     { return TOKEN(TRBRACE); }
";"                     { return TOKEN(TSEMI); }
"("                     { return TOKEN(TLPAREN); }
")"                     { return TOKEN(TRPAREN); }

"rotate"                { return TOKEN(TROTATE); }
"forward"               { return TOKEN(TFORWARD); }

%{
/* Do not add any of your own tokens below this line!!!! */
%}

"\n"                     { g_LineNumber++; }

This is the part the is giving me syntax errors.

"//.*"                      {g_LineNumber++; }


[a-zA-Z_]+          { std::cout << "ERROR: Unknown token '" << yytext << "' on line " << g_LineNumber << std::endl; yyterminate(); }

.                   { std::cout << "ERROR: Unknown token '" << yytext << "' on line " << g_LineNumber << std::endl; yyterminate(); }

%%
4

1 回答 1

0

您的评论模式应该是这样的

"//".*"\n"  {g_LineNumber++;}

您的模式期望.*在输入中按字面意思出现,因为它在双引号内。考虑到您增加了行号,您可能还希望将换行符作为注释的一部分进行匹配。

于 2013-05-10T11:48:15.013 回答