#ifndef lint static char yysccsid[] = "@(#)yaccpar 1.9 (Berkeley) 02/21/93"; #endif #define YYBYACC 1 #define YYMAJOR 1 #define YYMINOR 9 #define yyclearin (yychar=(-1)) #define yyerrok (yyerrflag=0) #define YYRECOVERING (yyerrflag!=0) #define YYPREFIX "yy" #line 6 "derive.y" struct info { char *expr; char *derive; }; #define YYSTYPE struct info #line 20 "y.tab.c" #define NUMBER 257 #define PLUS 258 #define POINTV 259 #define MULT 260 #define PARO 261 #define PARF 262 #define PARAM 263 #define VAR 264 #define EGAL 265 #define MOINS 266 #define DIV 267 #define YYERRCODE 256 short yylhs[] = { -1, 0, 1, 1, 1, 2, 2, 3, 3, 3, }; short yylen[] = { 2, 2, 3, 3, 1, 3, 1, 1, 1, 3, }; short yydefred[] = { 0, 0, 7, 8, 0, 0, 0, 6, 0, 0, 1, 0, 0, 9, 0, 0, 5, }; short yydgoto[] = { 4, 5, 6, 7, }; short yysindex[] = { -238, -238, 0, 0, 0, -245, -251, 0, -242, -238, 0, -238, -238, 0, -251, -251, 0, }; short yyrindex[] = { 0, 0, 0, 0, 0, 0, -258, 0, 0, 0, 0, 0, 0, 0, -256, -247, 0, }; short yygindex[] = { 0, 16, -4, 6, }; #define YYTABLESIZE 26 short yytable[] = { 4, 4, 2, 2, 4, 14, 2, 15, 4, 12, 2, 3, 3, 9, 10, 3, 9, 8, 16, 3, 13, 11, 0, 1, 11, 2, 3, }; short yycheck[] = { 258, 259, 258, 259, 262, 9, 262, 11, 266, 260, 266, 258, 259, 258, 259, 262, 258, 1, 12, 266, 262, 266, -1, 261, 266, 263, 264, }; #define YYFINAL 4 #ifndef YYDEBUG #define YYDEBUG 0 #endif #define YYMAXTOKEN 267 #if YYDEBUG char *yyname[] = { "end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"NUMBER","PLUS","POINTV","MULT", "PARO","PARF","PARAM","VAR","EGAL","MOINS","DIV", }; char *yyrule[] = { "$accept : statement", "statement : expression POINTV", "expression : expression PLUS terme", "expression : expression MOINS terme", "expression : terme", "terme : terme MULT facteur", "terme : facteur", "facteur : PARAM", "facteur : VAR", "facteur : PARO expression PARF", }; #endif #ifndef YYSTYPE typedef int YYSTYPE; #endif #ifdef YYSTACKSIZE #undef YYMAXDEPTH #define YYMAXDEPTH YYSTACKSIZE #else #ifdef YYMAXDEPTH #define YYSTACKSIZE YYMAXDEPTH #else #define YYSTACKSIZE 500 #define YYMAXDEPTH 500 #endif #endif int yydebug; int yynerrs; int yyerrflag; int yychar; short *yyssp; YYSTYPE *yyvsp; YYSTYPE yyval; YYSTYPE yylval; short yyss[YYSTACKSIZE]; YYSTYPE yyvs[YYSTACKSIZE]; #define yystacksize YYSTACKSIZE #line 167 "derive.y" #include "lex.yy.c" main() { yyparse(); } yyerror(char *s) { printf ("%s\n",s); } #line 137 "y.tab.c" #define YYABORT goto yyabort #define YYREJECT goto yyabort #define YYACCEPT goto yyaccept #define YYERROR goto yyerrlab int yyparse() { register int yym, yyn, yystate; #if YYDEBUG register char *yys; extern char *getenv(); if (yys = getenv("YYDEBUG")) { yyn = *yys; if (yyn >= '0' && yyn <= '9') yydebug = yyn - '0'; } #endif yynerrs = 0; yyerrflag = 0; yychar = (-1); yyssp = yyss; yyvsp = yyvs; *yyssp = yystate = 0; yyloop: if (yyn = yydefred[yystate]) goto yyreduce; if (yychar < 0) { if ((yychar = yylex()) < 0) yychar = 0; #if YYDEBUG if (yydebug) { yys = 0; if (yychar <= YYMAXTOKEN) yys = yyname[yychar]; if (!yys) yys = "illegal-symbol"; printf("%sdebug: state %d, reading %d (%s)\n", YYPREFIX, yystate, yychar, yys); } #endif } if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 && yyn <= YYTABLESIZE && yycheck[yyn] == yychar) { #if YYDEBUG if (yydebug) printf("%sdebug: state %d, shifting to state %d\n", YYPREFIX, yystate, yytable[yyn]); #endif if (yyssp >= yyss + yystacksize - 1) { goto yyoverflow; } *++yyssp = yystate = yytable[yyn]; *++yyvsp = yylval; yychar = (-1); if (yyerrflag > 0) --yyerrflag; goto yyloop; } if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 && yyn <= YYTABLESIZE && yycheck[yyn] == yychar) { yyn = yytable[yyn]; goto yyreduce; } if (yyerrflag) goto yyinrecovery; #ifdef lint goto yynewerror; #endif yynewerror: yyerror("syntax error"); #ifdef lint goto yyerrlab; #endif yyerrlab: ++yynerrs; yyinrecovery: if (yyerrflag < 3) { yyerrflag = 3; for (;;) { if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 && yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE) { #if YYDEBUG if (yydebug) printf("%sdebug: state %d, error recovery shifting\ to state %d\n", YYPREFIX, *yyssp, yytable[yyn]); #endif if (yyssp >= yyss + yystacksize - 1) { goto yyoverflow; } *++yyssp = yystate = yytable[yyn]; *++yyvsp = yylval; goto yyloop; } else { #if YYDEBUG if (yydebug) printf("%sdebug: error recovery discarding state %d\n", YYPREFIX, *yyssp); #endif if (yyssp <= yyss) goto yyabort; --yyssp; --yyvsp; } } } else { if (yychar == 0) goto yyabort; #if YYDEBUG if (yydebug) { yys = 0; if (yychar <= YYMAXTOKEN) yys = yyname[yychar]; if (!yys) yys = "illegal-symbol"; printf("%sdebug: state %d, error recovery discards token %d (%s)\n", YYPREFIX, yystate, yychar, yys); } #endif yychar = (-1); goto yyloop; } yyreduce: #if YYDEBUG if (yydebug) printf("%sdebug: state %d, reducing by rule %d (%s)\n", YYPREFIX, yystate, yyn, yyrule[yyn]); #endif yym = yylen[yyn]; yyval = yyvsp[1-yym]; switch (yyn) { case 1: #line 19 "derive.y" { printf("dérivée %s\n", yyvsp[-1].derive);} break; case 2: #line 23 "derive.y" { if (strcmp(yyvsp[-2].expr,"0")==0) yyval = yyvsp[0]; else if (strcmp(yyvsp[0].expr,"0")==0) yyval = yyvsp[-2]; else { yyval.expr = (char *) malloc(strlen(yyvsp[-2].expr)+strlen(yyvsp[0].expr)+2); strcpy(yyval.expr,yyvsp[-2].expr); strcat(yyval.expr,"+"); strcat(yyval.expr,yyvsp[0].expr); yyval.derive = (char *) malloc(strlen(yyvsp[-2].derive)+ strlen(yyvsp[0].derive)+2); if (strcmp(yyvsp[-2].derive,"0")==0) strcpy(yyval.derive,yyvsp[0].derive); else if (strcmp(yyvsp[0].derive,"0")==0) strcpy(yyval.derive,yyvsp[-2].derive); else { strcpy(yyval.derive,yyvsp[-2].derive); strcat(yyval.derive,"+"); strcat(yyval.derive,yyvsp[0].derive); } } printf("EXPRESSION expr %s, derive %s\n", yyval.expr, yyval.derive); } break; case 3: #line 47 "derive.y" { if (strcmp(yyvsp[-2].expr,"0")==0) { yyval.expr = (char *) malloc(strlen(yyvsp[0].expr)+2); strcpy(yyval.expr,"-"); strcat(yyval.expr,yyvsp[0].expr); } else if (strcmp(yyvsp[0].expr,"0")==0) yyval = yyvsp[-2]; else { yyval.expr = (char *) malloc(strlen(yyvsp[-2].expr)+strlen(yyvsp[0].expr)+2); strcpy(yyval.expr,yyvsp[-2].expr); strcat(yyval.expr,"-"); strcat(yyval.expr,yyvsp[0].expr); yyval.derive = (char *) malloc(strlen(yyvsp[-2].derive)+ strlen(yyvsp[0].derive)+2); if (strcmp(yyvsp[-2].derive,"0")==0) { strcpy(yyval.derive,"-"); strcat(yyval.derive,yyvsp[0].derive); } else if (strcmp(yyvsp[0].derive,"0")==0) strcpy(yyval.derive,yyvsp[-2].derive); else { strcpy(yyval.derive,yyvsp[-2].derive); strcat(yyval.derive,"-"); strcat(yyval.derive,yyvsp[0].derive); } } printf("EXPRESSION expr %s, derive %s\n", yyval.expr, yyval.derive); } break; case 4: #line 76 "derive.y" {yyval = yyvsp[0];} break; case 5: #line 79 "derive.y" { if (strcmp(yyvsp[-2].expr,"1")==0) yyval = yyvsp[0]; else if (strcmp(yyvsp[0].expr,"1")==0) yyval = yyvsp[-2]; else if (strcmp(yyvsp[-2].expr,"0")==0 || strcmp(yyvsp[0].expr,"0")==0){ strcpy(yyval.expr,"0"); strcpy(yyval.derive,"0"); } else { yyval.expr = (char *) malloc(strlen(yyvsp[-2].expr)+strlen(yyvsp[0].expr)+2); strcpy(yyval.expr,yyvsp[-2].expr); strcat(yyval.expr,"*"); strcat(yyval.expr,yyvsp[0].expr); yyval.derive = (char *) malloc(strlen(yyvsp[-2].expr)*strlen(yyvsp[0].derive)+ strlen(yyvsp[0].expr)*strlen(yyvsp[-2].derive)+ 4); if (strcmp(yyvsp[0].derive,"1")==0) { strcpy(yyval.derive,yyvsp[-2].expr); if (strcmp(yyvsp[-2].derive,"1")==0) { strcat(yyval.derive,"+"); strcat(yyval.derive,yyvsp[0].expr); } else if (strcmp(yyvsp[-2].derive,"0")!=0) { strcat(yyval.derive,"+"); strcat(yyval.derive,yyvsp[-2].derive); strcat(yyval.derive,"*"); strcat(yyval.derive,yyvsp[0].expr); } } else if (strcmp(yyvsp[0].derive,"0")!=0) { strcpy(yyval.derive,yyvsp[-2].expr); strcat(yyval.derive,"*"); strcat(yyval.derive,yyvsp[0].derive); if (strcmp(yyvsp[-2].derive,"1")==0) { strcat(yyval.derive,"+"); strcat(yyval.derive,yyvsp[0].expr); } else if (strcmp(yyvsp[-2].derive,"0")!=0) { strcat(yyval.derive,"+"); strcat(yyval.derive,yyvsp[-2].derive); strcat(yyval.derive,"*"); strcat(yyval.derive,yyvsp[0].expr); } } else if (strcmp(yyvsp[-2].derive,"1")==0) { strcpy(yyval.derive,yyvsp[0].expr); } else if (strcmp(yyvsp[-2].derive,"0")!=0) { strcpy(yyval.derive,yyvsp[-2].derive); strcat(yyval.derive,"*"); strcat(yyval.derive,yyvsp[0].expr); } else strcpy(yyval.derive,"0"); } printf("TERME expr %s, derive %s\n", yyval.expr, yyval.derive); } break; case 6: #line 137 "derive.y" {yyval = yyvsp[0];} break; case 7: #line 140 "derive.y" { yyvsp[0].expr = (char *) malloc(2); strcpy(yyvsp[0].expr,yytext); yyvsp[0].derive = (char *) malloc(2); strcpy(yyvsp[0].derive,"0"); printf("PARAM expr %s, derive %s\n", yyvsp[0].expr, yyvsp[0].derive); yyval = yyvsp[0];} break; case 8: #line 147 "derive.y" { yyvsp[0].expr = (char *) malloc(2); strcpy(yyvsp[0].expr,yytext); yyvsp[0].derive = (char *) malloc(2); strcpy(yyvsp[0].derive,"1"); printf("VAR expr %s, derive %s\n", yyvsp[0].expr, yyvsp[0].derive); yyval = yyvsp[0];} break; case 9: #line 154 "derive.y" { yyval.expr = (char *) malloc(strlen(yyvsp[-1].expr)+3); strcpy(yyval.expr,"("); strcat(yyval.expr,yyvsp[-1].expr); strcat(yyval.expr,")"); yyval.derive = (char *) malloc(strlen(yyvsp[-1].derive)+3); strcpy(yyval.derive,"("); strcat(yyval.derive,yyvsp[-1].derive); strcat(yyval.derive,")"); printf("(exp) expr %s, derive %s\n", yyval.expr, yyval.derive);} break; #line 444 "y.tab.c" } yyssp -= yym; yystate = *yyssp; yyvsp -= yym; yym = yylhs[yyn]; if (yystate == 0 && yym == 0) { #if YYDEBUG if (yydebug) printf("%sdebug: after reduction, shifting from state 0 to\ state %d\n", YYPREFIX, YYFINAL); #endif yystate = YYFINAL; *++yyssp = YYFINAL; *++yyvsp = yyval; if (yychar < 0) { if ((yychar = yylex()) < 0) yychar = 0; #if YYDEBUG if (yydebug) { yys = 0; if (yychar <= YYMAXTOKEN) yys = yyname[yychar]; if (!yys) yys = "illegal-symbol"; printf("%sdebug: state %d, reading %d (%s)\n", YYPREFIX, YYFINAL, yychar, yys); } #endif } if (yychar == 0) goto yyaccept; goto yyloop; } if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 && yyn <= YYTABLESIZE && yycheck[yyn] == yystate) yystate = yytable[yyn]; else yystate = yydgoto[yym]; #if YYDEBUG if (yydebug) printf("%sdebug: after reduction, shifting from state %d \ to state %d\n", YYPREFIX, *yyssp, yystate); #endif if (yyssp >= yyss + yystacksize - 1) { goto yyoverflow; } *++yyssp = yystate; *++yyvsp = yyval; goto yyloop; yyoverflow: yyerror("yacc stack overflow"); yyabort: return (1); yyaccept: return (0); }