Je fais un devoir sur le compilateur. J'ai donc trois fichiers. A SymbolInfo.h
un fichier parser.y
et un fichier lex.l
fichier.
SymbolInfo.h
fichier :
<include files....>
using namespace std;
#ifndef SYMBOLINFO_H_
#define SYMBOLINFO_H_
<more code...>
class SymbolTable{
static int id;
int bucket;
ScopeTable* parentScope;
<constructors and methods....>
}
#endif /* SYMBOLINFO_H_ */
J'ai besoin d'initialiser la variable statique id
. J'ai donc d'abord essayé de l'initialiser dans la section .h
fichier :
int SymbolTable::id = 0;
#endif /* SYMBOLINFO_H_ */
Ensuite, lorsque j'ai essayé de le compiler, il a donné l'erreur de compilation suivante :
l.o:(.bss+0x28): multiple definition of `SymbolTable::id'
y.o:(.bss+0x0): first defined here
l.o:(.bss+0x30): multiple definition of `id'
y.o:(.bss+0x430): first defined here
collect2: error: ld returned 1 exit status
./script.sh: line 14: ./a.out: No such file or directory
J'ai donc supprimé l'initialisation de la .h
et les a déplacés vers .l
y .y
fichier.
El .y
fichier :
%{
<some include files>
#include "SymbolInfo.h"
int SymbolTable::id = 0;
#define YYSTYPE SymbolInfo*
using namespace std;
int yyparse(void);
int yylex(void);
extern FILE *yyin;
extern int line_count;
FILE *fp;
ofstream logout,errorout;
const int bucketSize = 10;
SymbolTable symbolTable(bucketSize);
%}
Et le .l
fichier :
%{
<some include files...>
#include "SymbolInfo.h"
int SymbolTable::id = 0;
#include "y.tab.h"
using namespace std;
int line_count = 1;
int TOTAL_ERROR = 0;
extern SymbolTable symbolTable;
extern FILE *yyin;
extern YYSTYPE yylval;
extern ofstream logout,errorout;
......
%}
Mais cela donne toujours la même erreur et je ne comprends pas pourquoi. Désolé pour ce long message, mais toute aide serait appréciée.
script.sh
pour les commandes de compilation :
bison -d -y -v parser.y
g++ -std=c++11 -w -c -o y.o y.tab.c
flex "Lexical Analyzer".l
g++ -std=c++11 -w -c -o l.o lex.yy.c
g++ -std=c++11 -o a.out y.o l.o -lfl -ly
./a.out
Lien vers le code complet :