Bonjour,
J'utilise Lex and Yacc depuis peu. J'ai fait un petit programme pour parser un fichier texte tout simple. Le parser ne fonctionne pas correctement.
Est ce quelqu'un peu me dire ci mon code est incorrect?
Voici mon fichier LEX===============================================
%{ #include <stdio.h> #include <malloc.h> // The Parser token definition #include "pkgYacc.h"
%}
%%
^#.*\n {printf("\nLEXER:Line: %d - This is a COMMENT line, ignore it [%s] ",yylineno,yytext);fflush(stdout);;}
^[\t ]*\n {printf("\nLEXER:Line: %d - This is a BLANK line, ignore it [%s] ",yylineno,yytext);fflush(stdout);;}
"=" {
printf("\nLEXER:Line: %d - This is a EQUAL OPERATOR [%s] ",yylineno,yytext);fflush(stdout);; return (EQUAL); }
\"[^"]*["] {
printf("\nLEXER:Line: %d - This is a STRING [%s] ",yylineno,yytext);fflush(stdout);; return (STRING); }
-?[0-9]*\.[0-9]* { printf("\nLEXER:Line: %d - This is a FLOAT [%s] ",yylineno,yytext); return (FLOAT); }
-?[0-9]* { printf("\nLEXER:Line: %d - This is a INTEGER [%s] ",yylineno,yytext);fflush(stdout);; return (INTEGER); }
[_a-zA-Z0-9]* { printf("\nLEXER:Line: %d - This is a DATANAME [%s] ",yylineno,yytext);fflush(stdout);; return (DATANAME); } "\n" ;
%% int yywrap () { return 1; } |
===============================================
Voici mon fichier YACC:===============================================
%{ #include <stdio.h> #include <malloc.h> %} %start File %token EQUAL STRING DATANAME FLOAT INTEGER
%%
File: integerAttr | stringAttr | floatAttr ;
integerAttr: DATANAME EQUAL INTEGER { printf("\nPARSER: Found an attribute of type INTEGER "); fflush(stdout); };
stringAttr : DATANAME EQUAL STRING { printf("\nPARSER: Found an attribute of type STRING "); fflush(stdout); };
floatAttr : DATANAME EQUAL FLOAT { printf("\nPARSER: Found an attribute of type FLOAT "); fflush(stdout); };
%% extern FILE *yyin; main(int argc,char** argv) { if (argc > 1) { FILE *file; file = fopen(argv[1], "r"); if (!file) { fprintf(stderr,"could not open %s\n",argv[1]); return 1; } yyin = file; while(!feof(yyin)) { yyparse(); } } } yyerror(char* s) { fprintf(stderr, "%s\n", s); return 0; } |
===============================================
Voici le fichier à parser: ===============================================
data1 = 1
data2 = "2"
data3 = 3.3
data4 = 4===============================================
Voici la sortie du parser: ===============================================
LEXER:Line: 1 - This is a DATANAME [data1]
LEXER:Line: 1 - This is a EQUAL OPERATOR [=]
LEXER:Line: 1 - This is a INTEGER [1]
PARSER: Found an attribute of type INTEGER
LEXER:Line: 2 - This is a DATANAME [data2]===============================================
J'ai utilisé gnu bison et gnu flex pour generer le code source en C à partir des fichiers lex et yacc.
Après compilation du code dans MSDEV, j'utilise la commande
[color=orange]
Test.exe test.input [/color]
Pour lancer le parser.
Le problem est que le parser reconnait la première ligne et s'arrête desuite après sans crash.
Merci pour votre aide,
Jean-Louis