Grammaire ETF fichier lex %{ %} let [a-zA-Z] ch [0-9] %% [ \t\n]+ ; {let}({let}
Grammaire ETF fichier lex %{ %} let [a-zA-Z] ch [0-9] %% [ \t\n]+ ; {let}({let}|{ch}|_)* return (IDENT); {ch}+ {yylval=atoi(yytext);return (N_I);} "+" return (ADD); "*" return (MUL); "-" return (MOINS); \( return (P_O); \) return (P_F); "#" return (DIEZE); %% //_______________________________________________________________________ Grammaire ETF fichier bison %{ #include <stdio.h> %} %token ADD %token MOINS %token MUL %token SUB %token IDENT %token N_I %token P_O %token P_F %token DIEZE %start L %% L : L Ep {printf("r01\n");}| ; Ep: E DIEZE {$$=$1; printf("r0");printf("=%d ",$$);} ; E: E ADD T {$$=$1+$3; printf("r1,");} | E MOINS T {$$=$1-$3; printf("r11,");} | T {$$=$1; printf("r2,");} ; T: T MUL F {$$=$1*$3; printf("r3,");} | F {$$=$1; printf("r4,");} ; F: P_O E P_F {$$=$2; printf("r5,");} | N_I {$$=$1; printf("r6,");} ; %% #include "lex.yy.c" int yywrap(void){ return 1; } int main (int argc, char *argv[]){ yyin = fopen(argv[1], "r"); yyparse(); fclose(yyin); return 0; } advanced_cal.l %{ #include <stdio.h> %} ch [0-9] %% [ \t]+ ; {ch}+ {sscanf(yytext,"%lf",&yylval);return (NB);} "#" {return (DIEZE);} \n|. {return(yytext[0]);} %% //_____________________________________________________________________ advanced_calc.y %{ #include <ctype.h> #include <stdio.h> #define YYSTYPE double /* type de la pile de yacc*/ %} %token NB DIEZE %left '+' '-' %left '*' '/' %right moins_unaire %% L : L Ep '\n' {printf("r01\n\n");}| {printf("r02\n\n") ;} ; Ep: E DIEZE {$$=$1; printf("r0");printf("=%lf\n",$$);} ; E: E '+' E {$$=$1+$3;printf("r4\n");} |E '-' E {$$=$1-$3;printf("r5\n");} |E '*' E {$$=$1*$3;printf("r6\n");} |E '/' E {$$=$1/$3;printf("r7\n");} |'(' E ')' {$$=$2;printf("r8\n");} | '-' E %prec moins_unaire {$$= -$2;printf("r9\n");} | NB {printf("r10\n");} ; %% #include "lex.yy.c" int yywrap(void){return 1;} int main (int argc, char *argv[]){ yyin = fopen(argv[1], "r"); yyparse(); fclose(yyin); return 0; } /* flex nom.l bison nom.y -d -v cc -o nom nom.tab.c -ly -ll*/ Grammaire expressions régulières %{ #include <stdio.h> %} let [a-z] %% [ \t]+ ; {let}+ {return (ID);} "#" {return (DIEZE);} \n|. {return(yytext[0]);} %% fichier.y %{ #include <ctype.h> #include <stdio.h> #define YYSTYPE double /* type de la pile de yacc*/ %} %token ID DIEZE %left '|' %left '.' %left '*' %% L : L Ep '\n' {printf("r01\n\n");} |{printf("r02\n");} ; Ep: E DIEZE {printf("r0\n");} ; E: E '|' E {printf("r1\n");} |E '.' E {printf("r2\n");} |E'*' {printf("r3\n");} |'(' E ')' {printf("r4\n");} | ID {printf("r5\n");} ; %% #include "lex.yy.c" int yywrap(void){return 1;} int main (int argc, char *argv[]){ yyin = fopen(argv[1], "r"); yyparse(); fclose(yyin); return 0; } /* flex nom.l bison nom.y -d -v cc -o nom nom.tab.c -ly -ll*/ si alors sinon %{ #include <stdio.h> %} %% [ \t]+ ; "a" {return (A);} "i" {return (IF);} "e" {return (ELSE);} "#" {return (DIEZE);} \n|. {return(yytext[0]);} %% //_____________ grammaire ambigue____________________ %{ #include <stdio.h> %} %token IF ELSE A DIEZE %% L : IS L { printf("r1\n");} | { printf("r0\n");} IS :I DIEZE '\n'{ printf("r2\n");} ; I : IF I { printf("r3\n");} |IF I ELSE I { printf("r4\n");} | A { printf("r5\n");} ; %% si alors sinon priorité au décalge % à la réduction____________________ %{ #include <stdio.h> %} %token A IF DIEZE %nonassoc REDUCE %nonassoc ELSE %% L : IS L { printf("r1\n");} |{ printf("r0\n");} ; IS :I DIEZE '\n'{ printf("r2\n");} ; I : IF I ELSE I { printf("r3\n");} | IF I %prec REDUCE { printf("r4\n");} | A { printf("r5\n");} ; %% uploads/s3/ tp-bison.pdf
Documents similaires










-
19
-
0
-
0
Licence et utilisation
Gratuit pour un usage personnel Attribution requise- Détails
- Publié le Apv 06, 2021
- Catégorie Creative Arts / Ar...
- Langue French
- Taille du fichier 0.0229MB