忽略flex和bison中的空白区域

时间:2016-02-18 09:44:56

标签: bison space flex-lexer ignore bisonc++

我正在尝试解析以下几行:

import pandas
pandas.read_csv('file.csv').groupby('Symbol').first()

我用来解析上面行的语法正在解析直到 UNI_PLACE_GLOBAL 然后它会给出解析错误。

请帮我识别我所犯的错误。

我认为它不接受空间所以如何允许解析这些行?

我已经完成了

lex.l

BEGIN WRAP WIO3     
    NAME                            lgCell_prg160_io
    CONDITION                       UNI_PLACE_GLOBAL && compBits
ENDS WIO3       

yacc.y

%{
#include <iostream>
#include <stdio.h>
const char s[2] = " ";
#include "yacca.tab.h"
char *token;
#define YY_DECL extern "C" int yylex()
int line_num = 1;

#ifdef DEBUG
#define RETURN(x)       cerr << "\n--> found " << #x << "\n"; return x;
#else
#define RETURN(x)       return x;
#endif


using namespace std;
%}

DOT             "."
COLON           ":"
SEMICOLON       ";"
COMMA           ","
ANGLE_LEFT      "<"
ANGLE_RIGHT     ">"
AT              "@"
EQUAL           "="
SQUARE_OPEN     "["
SQUARE_CLOSE    [^\\]"]"
OPENBRACE       "\("
CLOSEBRACE      "\)"
QUOTE           "\""
QUOTE_OPEN      "\""
QUOTE_CLOSE     [^\\]"\""
SPACE           " "
TAB             "\t"
CRLF            "\r\n"
QUOTED_PAIR     "\\"[^\r\n]
DIGIT           [0-9]
ALPHA           [a-zA-Z]
QTEXT           [0-9a-zA-Z!#$%&'()*+,\-.\/:;<=>?@\[\]^_`{|}~""]

/* [ \t]         ; */


/* [ \s]         ; */



%%

[ \t]       ;

"//".*      /* ignore comments */

NAME          {  yylval.sval = strdup(yytext);
    return TOK_NAME; }

SIZE          { yylval.sval = strdup(yytext);
      return TOK_SIZE; }

ITERATE       { yylval.sval = strdup(yytext);
       return TOK_ITERATE; }

DIRECTION   { yylval.sval = strdup(yytext);
      return TOK_DIRECTION; }

STRAP       { yylval.sval = strdup(yytext);
      return TOK_STRAP; }

WRAP        { yylval.sval = strdup(yytext);
      return TOK_WRAP; }

VIA     { yylval.sval = strdup(yytext);
      return TOK_VIA; }

ENDS        { yylval.sval = strdup(yytext);
      return TOK_END; }

BEGIN       { yylval.sval = strdup(yytext);
      return TOK_BEGIN; }

BLOCK       { yylval.sval = strdup(yytext);
      return TOK_BLOCK; }

VERSION     { yylval.sval = strdup(yytext);
      return TOK_VERSION; }


STRUCTURE       { yylval.sval = strdup(yytext);
              return TOK_STRUCT; }

UNIQUE          { yylval.sval = strdup(yytext);
              return TOK_UNIQUE; }


REF         { yylval.sval = strdup(yytext);
              return TOK_REF; }

POSITION            { yylval.sval = strdup(yytext);
                  return TOK_POS; }

CONDITION           { yylval.sval = strdup(yytext);
                  return TOK_CON; }

ORIENTATION         { yylval.sval = strdup(yytext);
                  return TOK_ORI; }

{QTEXT}*        { yylval.sval = strdup(yytext);
          return TOK_STRING; } 


\\\n            { printf("c> "); }      /* ignore line continuation */


^{CRLF}                         { return TOK_EMPTY_LINE; }
{CRLF}                          {}
.                               {}/* ignore unknown chars */
\n                      { ++line_num; /*RETURN(ENDL); */ }

1 个答案:

答案 0 :(得分:4)

您需要做什么使用-d选项标志启用词法分析器的调试模式,并使用-t标志启用解析跟踪模式。您还必须向主程序添加几行以启用解析器跟踪:

int main(void) {

之后将这些行添加到ayacc.y
    extern int yydebug;
    yydebug = 1;

现在使用调试和跟踪构建:

flex -d lex.l
bison -t -d ayacc.y

现在,当你跑步时,你得到这个:

Starting parse
Entering state 0
Reading a token: --(end of buffer or a NUL)
--accepting rule at line 80 ("BEGIN")
Next token is token TOK_BEGIN ()
Shifting token TOK_BEGIN ()
Entering state 1
Reading a token: --accepting rule at line 52 (" ")
--accepting rule at line 71 ("WRAP")
Next token is token TOK_WRAP ()
Shifting token TOK_WRAP ()
Entering state 7
Reading a token: --accepting rule at line 52 (" ")
--accepting rule at line 109 ("WIO3")
Next token is token TOK_STRING ()
Shifting token TOK_STRING ()
Entering state 29
Reading a token: --accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 119 ("
")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 56 ("NAME")
Next token is token TOK_NAME ()
Shifting token TOK_NAME ()
Entering state 12
Reading a token: --accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 109 ("lgCell_prg160_io")
Next token is token TOK_STRING ()
Shifting token TOK_STRING ()
Entering state 32
Reducing stack by rule 12 (line 109):
   $1 = token TOK_NAME ()
   $2 = token TOK_STRING ()

-> $$ = nterm item ()
Stack now 0 1 7 29
Entering state 27
Reducing stack by rule 10 (line 104):
   $1 = nterm item ()
-> $$ = nterm blockcontents ()
Stack now 0 1 7 29
Entering state 48
Reading a token: --accepting rule at line 119 ("
")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 103 ("CONDITION")
Next token is token TOK_CON ()
Shifting token TOK_CON ()
Entering state 23
Reading a token: --accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 52 (" ")
--accepting rule at line 109 ("UNI_PLACE_GLOBAL")
Next token is token TOK_STRING ()
Shifting token TOK_STRING ()
Entering state 43
Reducing stack by rule 23 (line 120):
   $1 = token TOK_CON ()
   $2 = token TOK_STRING ()
NAME->lgCell_prg160_io  
CONDITION->UNI_PLACE_GLOBAL  
-> $$ = nterm item ()
Stack now 0 1 7 29 48
Entering state 46
Reducing stack by rule 11 (line 105):
   $1 = nterm blockcontents ()
   $2 = nterm item ()
-> $$ = nterm blockcontents ()
Stack now 0 1 7 29
Entering state 48
Reading a token: --accepting rule at line 52 (" ")
--accepting rule at line 109 ("&&")
Next token is token TOK_STRING ()
parse error on line 3!  Message: syntax error

您可以看到使用词法分析器规则正确忽略空格。您的问题是&&符号被识别为TOK_STRING,这不是语法规则所期望的。

不知道你的语言的正确语法应该是什么(你没说)如果没有更多的信息,我无法解决这个问题。您可能希望修复TOK_STRING规则以仅匹配字符串!