Parser un fichier xml avec Flex/Bison
amine91000
-
Pomme_Dapi -
Pomme_Dapi -
Bonjour,
je cherche a parser un fichier xml avec les deux analysateur Flex et Bison
je suis débutant et à chaque fois je cherche sur le net il me sort le format général d'un fichier Flex(et Bison)
alors que j'ai besoin d'un code précis voila se que je trouve à chaque fois:
######################################################################################################################
parseurINI.l
######################################################################################################################
%{
#define _SKIP_YYFLEXLEXER_
#include "Scanner.h"
#include "ParseurINI.h"
%}
%option noyywrap
%option c++
%option yyclass="Scanner"
%%
\n {yylineno++;}
[ \t]* {; /* spaces */}
;[^\n]* {; /* comments */}
[^ \t\n\r;=\[\]]+ {return YY_ParseurINI_CLASS::CHAINE ;}
. {return yytext[0] ;}
%%
######################################################################################################################
parseurINI.y
######################################################################################################################
%name ParseurINI
%define LSP_NEEDED
%define CONSTRUCTOR_PARAM istream* pFile
%define CONSTRUCTOR_INIT : scanner(pFile)
%define CONSTRUCTOR_CODE {yyparse() ;}
%define LEX_BODY {return scanner.yylex();}
%define MEMBERS \
virtual ~ParseurINI() {} \
private: \
Scanner scanner ;\
map<string, map<string, string> > mapSections ; \
string sectionCourante ;\
public:\
const map<string, string> operator[] (string section) {return mapSections[section] ;}
%define ERROR_BODY {cerr << "error on line : " << scanner.lineno() << ", last token : '" << scanner.YYText() << "'" << endl;}
%header{
// Inspiré de :
// https://www.mirbsd.org/htman/i386/man1/lex.htm
// http://pwet.fr/man/linux/commandes/bison/
// http://docs.linux.cz/programming/c++/www.icce.rug.nl/documents/cplusplus/cplusplus19.html
// Et beaucoup de courage !
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include "Scanner.h"
#include <string.h>
using namespace std;
%}
%union { char chaine[500];}
%token <chaine> CHAINE
%type <chaine> chaine
%start fichier
%%
fichier:
| section fichier
| regle fichier
;
section:
'[' chaine ']' {sectionCourante = $2;}
;
regle:
chaine '=' chaine {(mapSections[sectionCourante])[$1] = $3;}
;
chaine:
CHAINE {strcpy($$, scanner.YYText());}
;
%%
######################################################################################################################
Scanner.h
######################################################################################################################
#ifndef _SCANNER_H_
#define _SCANNER_H_
#if ! defined(_SKIP_YYFLEXLEXER_)
#include <FlexLexer.h>
#endif
using namespace std ;
class Scanner: public yyFlexLexer
{
public:
Scanner (istream* pFile) : yyFlexLexer(pFile) {}
int yylex();
};
#endif
######################################################################################################################
Main.cpp
######################################################################################################################
#include <map>
#include <string>
#include <vector>
#include <iostream>
#include "ParseurINI.h"
using namespace std;
int main()
{
try
{
ifstream config ("config.ini") ;
// &cin for stdin
ParseurINI parseur(&config) ;
map<string, string> sectionMain = parseur["Main"] ;
cout << "Serveur MySQL : " << sectionMain["ServeurMySQL"] << endl ;
}
catch(string s)
{
cout << s << "\n" ;
return 1 ;
}
catch(...)
{
cout << "Erreur innatendue\n" ;
return 1 ;
}
return 0 ;
}
######################################################################################################################
Makefile
######################################################################################################################
COMPILE := -c
FLAGS := -Wall
CC := g++
EXEC := main.exe
all: ParseurINI.o lex.yy.o Main.o
${CC} ${FLAGS} $^ -o ${EXEC}
%.o:%.cpp
${CC} ${FLAGS} ${COMPILE} $^ -o $@
ParseurINI.o: parseurINI.l parseurINI.y Scanner.h
bison++ -o ParseurINI.cpp -h ParseurINI.h -d parseurINI.y
flex++ parseurINI.l
g++ ${COMPILE} lex.yy.cc
g++ ${COMPILE} ParseurINI.cpp
clean:
rm -f *~ *.o *.gch ParseurINI.h ParseurINI.cpp lex.yy.cc ${EXEC}
voila je comprend pas comment ça marche
Merci de me répondre
je cherche a parser un fichier xml avec les deux analysateur Flex et Bison
je suis débutant et à chaque fois je cherche sur le net il me sort le format général d'un fichier Flex(et Bison)
alors que j'ai besoin d'un code précis voila se que je trouve à chaque fois:
######################################################################################################################
parseurINI.l
######################################################################################################################
%{
#define _SKIP_YYFLEXLEXER_
#include "Scanner.h"
#include "ParseurINI.h"
%}
%option noyywrap
%option c++
%option yyclass="Scanner"
%%
\n {yylineno++;}
[ \t]* {; /* spaces */}
;[^\n]* {; /* comments */}
[^ \t\n\r;=\[\]]+ {return YY_ParseurINI_CLASS::CHAINE ;}
. {return yytext[0] ;}
%%
######################################################################################################################
parseurINI.y
######################################################################################################################
%name ParseurINI
%define LSP_NEEDED
%define CONSTRUCTOR_PARAM istream* pFile
%define CONSTRUCTOR_INIT : scanner(pFile)
%define CONSTRUCTOR_CODE {yyparse() ;}
%define LEX_BODY {return scanner.yylex();}
%define MEMBERS \
virtual ~ParseurINI() {} \
private: \
Scanner scanner ;\
map<string, map<string, string> > mapSections ; \
string sectionCourante ;\
public:\
const map<string, string> operator[] (string section) {return mapSections[section] ;}
%define ERROR_BODY {cerr << "error on line : " << scanner.lineno() << ", last token : '" << scanner.YYText() << "'" << endl;}
%header{
// Inspiré de :
// https://www.mirbsd.org/htman/i386/man1/lex.htm
// http://pwet.fr/man/linux/commandes/bison/
// http://docs.linux.cz/programming/c++/www.icce.rug.nl/documents/cplusplus/cplusplus19.html
// Et beaucoup de courage !
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include "Scanner.h"
#include <string.h>
using namespace std;
%}
%union { char chaine[500];}
%token <chaine> CHAINE
%type <chaine> chaine
%start fichier
%%
fichier:
| section fichier
| regle fichier
;
section:
'[' chaine ']' {sectionCourante = $2;}
;
regle:
chaine '=' chaine {(mapSections[sectionCourante])[$1] = $3;}
;
chaine:
CHAINE {strcpy($$, scanner.YYText());}
;
%%
######################################################################################################################
Scanner.h
######################################################################################################################
#ifndef _SCANNER_H_
#define _SCANNER_H_
#if ! defined(_SKIP_YYFLEXLEXER_)
#include <FlexLexer.h>
#endif
using namespace std ;
class Scanner: public yyFlexLexer
{
public:
Scanner (istream* pFile) : yyFlexLexer(pFile) {}
int yylex();
};
#endif
######################################################################################################################
Main.cpp
######################################################################################################################
#include <map>
#include <string>
#include <vector>
#include <iostream>
#include "ParseurINI.h"
using namespace std;
int main()
{
try
{
ifstream config ("config.ini") ;
// &cin for stdin
ParseurINI parseur(&config) ;
map<string, string> sectionMain = parseur["Main"] ;
cout << "Serveur MySQL : " << sectionMain["ServeurMySQL"] << endl ;
}
catch(string s)
{
cout << s << "\n" ;
return 1 ;
}
catch(...)
{
cout << "Erreur innatendue\n" ;
return 1 ;
}
return 0 ;
}
######################################################################################################################
Makefile
######################################################################################################################
COMPILE := -c
FLAGS := -Wall
CC := g++
EXEC := main.exe
all: ParseurINI.o lex.yy.o Main.o
${CC} ${FLAGS} $^ -o ${EXEC}
%.o:%.cpp
${CC} ${FLAGS} ${COMPILE} $^ -o $@
ParseurINI.o: parseurINI.l parseurINI.y Scanner.h
bison++ -o ParseurINI.cpp -h ParseurINI.h -d parseurINI.y
flex++ parseurINI.l
g++ ${COMPILE} lex.yy.cc
g++ ${COMPILE} ParseurINI.cpp
clean:
rm -f *~ *.o *.gch ParseurINI.h ParseurINI.cpp lex.yy.cc ${EXEC}
voila je comprend pas comment ça marche
Merci de me répondre
A voir également:
- Parser un fichier xml avec Flex/Bison
- Fichier bin - Guide
- Comment ouvrir un fichier epub ? - Guide
- Comment réduire la taille d'un fichier - Guide
- Fichier rar - Guide
- Fichier .dat - Guide
1 réponse
Bonjours,
Je cherche a faire la même chose...
Je suis actuellement en stage dans le cadre d'un DUT GEII (Génie Électrique et Informatique Industrielle).
Je n'avais encore jamais étudié le langage XML, ni Flex et Bison.
Ce que je cherche a faire pour ma part, c'est parser la partie commentaire du XML. Je sais qu'il existe XSL mais ce dernier ne parse pas la partie commentaire.
Si vous avez des informations, cela m'intéresse au plus haut point :-)
Je cherche a faire la même chose...
Je suis actuellement en stage dans le cadre d'un DUT GEII (Génie Électrique et Informatique Industrielle).
Je n'avais encore jamais étudié le langage XML, ni Flex et Bison.
Ce que je cherche a faire pour ma part, c'est parser la partie commentaire du XML. Je sais qu'il existe XSL mais ce dernier ne parse pas la partie commentaire.
Si vous avez des informations, cela m'intéresse au plus haut point :-)