2016-04-14 11 views
0
%option case-insensitive 
%option noyywrap 
%option yylineno 


id    [a-zA-Z][a-zA-Z_0-9]* 

integer   (0[xX][0-9A-Fa-f]+)|([0-9]+) 

real   [0-9]*\.[0-9](e|E)?[0-9]* 

undefined  [^"\r\n \t] 

%x STR 
%% 
\" string_buf_ptr = string_buf; BEGIN(STR); 

<STR>\"  { /* saw closing quote - all done */ 
    BEGIN(INITIAL); 
    *string_buf_ptr = '\0'; 
    /* return string constant token type and 
    * value to parser 
    */ 

    yylval.strVal = strdup(string_buf); 
    sprintf(yyout, yylval.strVal); 
    return STR; 
    } 

<STR>\\n *string_buf_ptr++ = '\n'; 
<STR>\\t *string_buf_ptr++ = '\t'; 
<STR>\\r *string_buf_ptr++ = '\r'; 
<STR>\\b *string_buf_ptr++ = '\b'; 
<STR>\\f *string_buf_ptr++ = '\f'; 

<STR>\\(.|\n) *string_buf_ptr++ = yytext[1]; 

<STR>[^\\\n\"]+  { 
    char *yptr = yytext; 

    while (*yptr) 
      *string_buf_ptr++ = *yptr++; 
    } 

これは文字列の字句解析のコードの一部です。このコードの目的は、文字列(Cのような文字列、例えば"Hello World")を見つけて、処理するためにそれらをパーサーに戻すことです。 文字列を処理するときの解析エラー

は、私はまた、 Yaccツールを使用してパーサを構築してきたし、それは我々が変数に文字列を代入しているから除いてほぼ完璧です(例。 x="Hello World";) 私が行うと、 yyerror(const char*)関数が呼び出され、メッセージが unexpected $undefined. Iであることをなぜこれが起こっているのか理解できません。この減少は、 left value xassignmentおよび constant String "Hello World"を認識するはずです。何が間違っていますか?

答えて

2

問題は明らかに、STRをトークンとレックスの開始状態の両方として定義していることです。これらはどちらも実際には整数定数リテラルに展開されるマクロなので、コンパイラの出力のどこかでマクロSTRが再定義されているという警告が出ていると思います。上記のコードのreturn STR;は、bison生成パーサが有効なトークンコードとして認識しないトークンコードではなく、STRの開始状態コードを返します。

+0

ありがとうございます!それだった! – Jack