0

I am working on a project to create a DOM parser. At the initial stage I am simply trying to figure out how many tags are there in the given file. Let's say I have an XML file whose content is somewhat like this : <abc>this is test file</abc> , for this I only want to parse the two tags <abc> and </abc>. For this happen I am using Flex and Bison to write a grammar so that whenever this grammar occurs I execute my code. This is my Bison code :

%{
    #include <stdio.h>
    #include <conio.h>
    int yylex();
    int yyparse();
    FILE *yyin;
    int yylineno;
    void yyerror(const char*);
%}

%token START_TAG END_TAG 

%%
tag:
    sTag
    | eTag
    ;
sTag:
    START_TAG {printf("start tag encountered");}
    ;
eTag:
    END_TAG
    ;
%%
int main(){
    FILE *myFile;
    myFile = fopen("G:\\MCA-2\\project\\09-03-2013\\demo.txt","r");
    if(!myFile){
        printf("error opening file");
    }
    yyin = myFile;
    do{
        yyparse();
    } while(!feof(yyin));
    fclose(myFile);
    return 0;
}

And this is my Flex code :

%{
    #include "xml.tab.h"
    #define YY_DECL extern "C" int yylex()
%}

%option noyywrap
%option yylineno
alpha [a-zA-Z]
digit [0-9]
%%
[ \t] {}
[ \n] {}
{alpha}({alpha}|{digit})* return START_TAG;
%%

When I am trying to compile this I am getting an error like this :

lex.yy.c : 529:1: error: expected identifier or '(' before string constant.

Can anyone tell me what is the mistake I am making?

4

1 回答 1

0

(评论中回答的问题;请参阅:没有答案的问题,但问题已在评论中解决(或在聊天中扩展)

@rici 提供了答案。由于 flex 正在为 gcc 编译器生成 C 代码,因此完全摆脱了该#define YY_DECL行,因为默认声明会很好。每次更改 flex 文件时不要忘记运行 flex 以重新生成 lex.yy.c。该声明extern "C"...仅在 C++ 中有效,在 C 中无效,通常file.c会编译为 C。

于 2015-01-24T13:06:58.593 回答