當我將一個list *指針嵌入到bison解析器中時,編譯器會報告'list'沒有指定類型的錯誤。這是一些必要的部分;如果你需要,我可以發佈所有代碼。我正在使用最新版本的bison和flex。flex和bison:C++類
parser.l
%{
#include <stdlib.h>
#include <string.h>
#include "parser.tab.h"
extern void yyerror(const char *);
extern int yylex();
char* outchar;
char* mychars;
unsigned int line_number=1;
%}
%option nounput
%option noyywrap
%option noinput
%%
"automaton" {return AUTOMATON;}
"var" {return VAR;}
"synclabs" {return SYNCLABS;}
"initially" {return INITIALLY;}
"loc" {return LOC;}
"while" {return WHILE;}
"wait" {return WAIT;}
"when" {return WHEN;}
"do" {return DO;}
"sync" {return SYNC;}
"goto" {return GOTO;}
"end" {return END;}
"forbidden" {return FORBIDDEN;}
"true" {return TRUE;}
"TRUE" {return TRUE;}
"false" {return FALSE;}
"FALSE" {return FALSE;}
"=" {return EQ2;}
"==" {return EQ;}
">=" {return GE;}
"<=" {return LE;}
"'" {return PRIM;}
":=" {return ASSIGN;}
([0-9]+)(("."[0-9]+)?)((([eE])([+-]?)([0-9]+))?) {yylval.mystring = strdup(yytext); return INT;}
[-+<>*/&|(){}:;,.=!]|"["|"]" {return *yytext;}
(["$""?"a-zA-Z]|[_])("$"|"?"|[a-zA-Z]|[0-9]|[_]|[~])* { yylval.mystring = strdup(yytext); return IDENT; }
"/*"([^*]|("*"+[^*/]))*"*"+"/" /* skip comment */
"//".* /* skip comment XS*/
"--".* /* skip comment */
[\n] { ++line_number;
}
[ \t\r]
%%
parser.y
%{
#include <stdio.h>
#include <stdlib.h>
#include<list>
using namespace std;
extern int yylex();
extern unsigned int line_number;
void yyerror(const char *msg)
{
printf("Parse error in line %d: ", line_number);
printf("%s\n", msg);
}
Automaton *aut;
int var_count=0;
string *s;
%}
%union{
char * mystring;
list* s;
};
%token AUTOMATON
%token VAR
%token SYNCLABS
%token INITIALLY
%token LOC
%token WHILE
%token WAIT
%token WHEN
%token DO
%token SYNC
%token GOTO
%token END
%token ASSIGN
%token FORBIDDEN
%token TRUE
%token FALSE
%token <mystring> INT
%token <mystring> IDENT
//%nonassoc '(' ')'
%left '&'
%left GE LE EQ EQ2
%left '+' '-'
%left '!'
%left '(' ')'
%left PRIM
%%
program:
commands
;
%%
compile result:
g++ -O2 -w -c -I . -g -O2 -o lex.yy.o lex.yy.c
In file included from parser.l:4:0:
parser.y:23:2: error: ‘list’does not name a type
但我看到有人在聯合中使用列表和用戶定義的類指針。 – 2013-03-23 06:02:27
這裏沒有編譯錯誤。請發佈一個可以重現問題的最小完整示例。 – 2013-03-23 07:47:18
如果你想編譯一個'.c'文件作爲C++,你需要說'-x C++'。 – 2013-03-23 17:02:12