Yacc程序给出错误:"语法错误"

时间:2015-08-04 01:47:08

标签: c bison yacc lex text-parsing

我有以下需要解析的配置文件。

[ main ]
e_type=0x1B

lex(test.l)和yacc(test.y)文件在下面给出

test.l

%option noyywrap
%option yylineno
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "y.tab.h"

int yylinenu = 1;
int yycolno=1;

/**
 * Forward declerations
 **/
void Number ();
void HexaNumber ();
unsigned char getHexaLex (char c);
unsigned int strtol16 (char * str);


%}

%option nounput
%option noinput
%option case-insensitive

/*-----------------------------------------------------------------
   Some macros (standard regular expressions)
------------------------------------------------------------------*/

DIGIT       [0-9]
HEXALETTER  [a-fA-F]
HEXANUMBER  [0][x](({DIGIT}|{HEXALETTER})+)
NUM         {DIGIT}+
HEXA        ({DIGIT}|{HEXALETTER}|[*])
STR         \"[^\"]*\"
WSPACE      [ \t]*
NEWLINE     [\n\r]        

/*----------------------------------------------------------------
   The lexer rules
------------------------------------------------------------------*/
%%

e_type                   { yylval.str = yytext; return T_E_TYPE; }
main                     { yylval.str = yytext; return T_MAIN_SECTION;}
{HEXANUMBER}             { yylval.n = atoi(yytext);  HexaNumber(); return T_NUMBER; }
=                        { return T_EQUAL; }
"["                      { return T_OPEN_BRACKET; }
"]"                      { return T_CLOSE_BRACKET;}

[^\t\n\r]                { }
{WSPACE}                 { } /* whitespace: (do nothing) */
{NEWLINE}                {  
                            yylinenu++;
                            return T_EOL;
                         }

%%

void Number () {
    yylval.n = atol(yytext);
}

test.y

%{
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include "y.tab.h"
#include "lex.h"

#define E_PARSE_DEBUG
#ifdef E_PARSE_DEBUG
// Some yacc (bison) defines
#define YYDEBUG 1       // Generate debug code; needed for YYERROR_VERBOSE
#define YYERROR_VERBOSE // Give a more specific parse error message 
#endif

#define E_DEBUG_STRINGIFY(x) #x
#define E_DEBUG_TOSTRING(x) E_DEBUG_STRINGIFY(x)
#define E_DEBUG_AT (__FILE__ ":" E_DEBUG_TOSTRING(__LINE__))

extern  int yyparse (void);
extern int yylinenu;
extern int yycolno;
void yyerror(const char *str)
{
  fprintf(stderr,"line: %d column: %d error: %s %s \n",yylinenu,yycolno,str,yylval.str);
}

int yywrap()
{
  return 1;
}

int main()
{
  printf("> "); 

  // open a file handle to a particular file:
  FILE *myfile = fopen("tmp.conf", "r");
  // make sure it is valid:
  if (!myfile) {
      printf("I can't open tmp.conf \n");
      return -1;
  }else
  {
    printf("opened config file \n");
  }
  // set lex to read from it instead of defaulting to STDIN:
  yyin = myfile;

  yyparse();

  return 0;
}
int lineno = 0;
%}

/*------------------------------------------------------------------
  Yacc declarations
------------------------------------------------------------------ */

/* The structure for passing value between lexer and parser */
/* In the lexer we know it by the name 'yylval'*/
%union {
   char *str;
   unsigned int n;
   void * distr;
   void * command;
}

%token T_E_TYPE T_HOSTID_TYPE T_MAIN_SECTION T_EQUAL T_OPEN_BRACKET T_CLOSE_BRACKET T_EOL
%token <str> STRING
%token <n> T_NUMBER
%type <n> number


%%
config_file
    : /* empty */
    {

    }
    | config_file config_file_section
    {

    }
    ;

config_file_section
    : T_OPEN_BRACKET T_MAIN_SECTION T_CLOSE_BRACKET attribute_list
    {

    }
    ; 

attribute_list
    : /* empty */
    {

    }
    | attribute_list attribute_pair
    {

    }
    ;
attribute_pair
    : T_E_TYPE T_EQUAL number
    {

    }
    ;

number
    : T_NUMBER
    {

    }
    ;
%%

我有一个包含在test.y

中的include lex.h文件
#ifndef _LEX_H_
#define _LEX_H_

#include <stdio.h>

#ifdef _LEX_CPP_
int lineno = 1; // line number count; this will be used for error messages later
#else
   // Import some variables
extern int lineno;
extern FILE *yyin;  // the input stream

// Function prototype
int yylex ();
#endif

ENDIF

我按如下方式编译文件。

flex test.l
yacc -d  test.y
gcc lex.yy.c y.tab.c -Wall  -ll -o test -ldag

我得到的二进制文件test正在提供

> opened config file 
line: 1 column: 1 error: syntax error main

1 个答案:

答案 0 :(得分:2)

你的问题是规则:

attribute_pair
    : T_E_TYPE '=' number

你的词法分析器返回T_EQUAL,但语法需要'=',两者不相等。我很容易发现;当我运行程序时,我收到了报告:

> opened config file 

error: syntax error, unexpected T_EQUAL, expecting '='

因此找到问题非常容易。我正在使用的yacc实际上是bison

$ yacc --version
bison (GNU Bison) 2.3
…
$

当我修复它时,错误发生了变化:

> opened config file 

error: syntax error, unexpected T_NUMBER, expecting NUMBER

修复涉及更改:

%token T_E_TYPE T_HOSTID_TYPE T_MAIN_SECTION T_EQUAL T_OPEN_BRACKET T_CLOSE_BRACKET
%token <str> STRING
%token <n> T_NUMBER

(从第一行删除T_NUMBER;在第三行中将NUMBER更改为T_NUMBER。)还有:

number
    : T_NUMBER

(将NUMBER更改为T_NUMBER。)

通过这两项更改,您可以获得成功的运行(除了打开的消息之外没有输出,但也没有错误)。