--- capa/capa51/pProj/capaGrammarDef.y 1999/10/20 19:57:37 1.5 +++ capa/capa51/pProj/capaGrammarDef.y 2000/08/23 16:55:12 1.14 @@ -1,6 +1,30 @@ +/* main CAPA parser + Copyright (C) 1992-2000 Michigan State University + + The CAPA system is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The CAPA system is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public + License along with the CAPA system; see the file COPYING. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + As a special exception, you have permission to link this program + with the TtH/TtM library and distribute executables, as long as you + follow the requirements of the GNU GPL in regard to all of the + software in the executable aside from TtH/TtM. +*/ + /* ========================================================================== */ /* capaGrammarDef.y created by Isaac Tsai */ -/* 1998, 1999 copyrighted by Isaac Tsai */ +/* 1998, 1999 by Isaac Tsai */ /* no longer there is a length constrain on string concatenation July 13 1998 */ /* /RMAP() function */ /* TODO: new mechanism to collect answer informations */ @@ -36,17 +60,18 @@ case S_VAR: case S_CONSTANT: \ printf("STR(%s)\n",(xx)->s_str); break; \ } } -#else +#else /* YACC_DBUG */ #define YYDBUG_PR1(xx) { } #define YYDBUG_PR2(xx,yy) { } #define YYDBUG_PR3(xx,yy,zz) { } #define YYDBUG_PR4(xx,yy,zz,aa) { } #define YYDBUG_SYM(xx) { } -#endif +#endif /* YACC_DBUG */ int yylex(); void yyerror(char*); void free_calc_expr(Symbol*); +void assign_pts (Symbol *, Symbol *, Symbol *); /******************************************************************************/ /* GLOBAL VARIABLES */ /******************************************************************************/ @@ -117,7 +142,7 @@ PointsList_t *LastPtsList; %token ANS_OFF ANS_ON ANS_OR ANS_ORDERED %token ANS_PATH ANS_PCREDIT ANS_PLUS ANS_RANGE %token ANS_SHOW_BR ANS_SIG ANS_TOLERANCE ANS_TRY ANS_TYPE -%token ANS_UNFMT ANS_UNIT ANS_WEIGHT +%token ANS_UNFMT ANS_UNIT ANS_VERBATIM ANS_WEIGHT %token VAR_RANGE VERBATIM %token SLASH FORMAT %token EQ_op NE_op GT_op GE_op LT_op LE_op AND_op OR_op EoL @@ -287,36 +312,36 @@ statement : IDENTIFIER '=' calc_expr ; rqo_def : rqo_speca { rqo_1spec(); } - | rqo_speca "," rqo_def { rqo_2spec(); } + | rqo_speca ',' rqo_def { rqo_2spec(); } ; rqo_speca : an_integer {start_rqo_type(SINGLE);append_rqo($1);} - | an_integer "!" {start_rqo_type(IMMOBILE);append_rqo($1);} - | an_integer "-" an_integer {start_rqo_type(RANGE);append_rqo($1); + | an_integer '!' {start_rqo_type(IMMOBILE);append_rqo($1);} + | an_integer '-' an_integer {start_rqo_type(RANGE);append_rqo($1); append_rqo($3); } - | an_integer "~" rqo_specb {prefix_rqo($1);} - | an_integer "+" rqo_specc {prefix_rqo($1);} + | an_integer '~' rqo_specb {prefix_rqo($1);} + | an_integer '+' rqo_specc {prefix_rqo($1);} ; rqo_specb : an_integer {start_rqo_type(ALL_MIX);append_rqo($1);} - | an_integer "~" rqo_specb {prefix_rqo($1);} - | an_integer "+" an_integer {start_rqo_type(LAST_FIXED);append_rqo($1); + | an_integer '~' rqo_specb {prefix_rqo($1);} + | an_integer '+' an_integer {start_rqo_type(LAST_FIXED);append_rqo($1); append_rqo($3);} ; rqo_specc : an_integer {start_rqo_type(ALL_FIXED);append_rqo($1);} - | an_integer "+" rqo_specd {prefix_rqo($1);} - | an_integer "~" rqo_spece {prefix_rqo($1);} + | an_integer '+' rqo_specd {prefix_rqo($1);} + | an_integer '~' rqo_spece {prefix_rqo($1);} ; rqo_specd : an_integer {start_rqo_type(ALL_FIXED);append_rqo($1);} - | an_integer "+" rqo_specd {prefix_rqo($1);} + | an_integer '+' rqo_specd {prefix_rqo($1);} ; rqo_spece : an_integer {start_rqo_type(FIRST_FIXED);append_rqo($1);} - | an_integer "~" rqo_spece {prefix_rqo($1);} - | an_integer "+" an_integer {start_rqo_type(BOTH_FIXED);append_rqo($1); + | an_integer '~' rqo_spece {prefix_rqo($1);} + | an_integer '+' an_integer {start_rqo_type(BOTH_FIXED);append_rqo($1); append_rqo($3);} ; @@ -505,6 +530,10 @@ ans_infospec : ANS_TOLERANCE '=' a_numbe } | ANS_SHOW_BR '=' ANS_OFF { LexiProblem_p->show_br = DONOT_SHOW; } + | ANS_VERBATIM '=' ANS_ON { LexiProblem_p->verbatim = DO_VERBATIM; + } + | ANS_VERBATIM '=' ANS_OFF { LexiProblem_p->verbatim = DONOT_VERBATIM; + } | ANS_BOX_SHOW '=' ANS_ON { LexiProblem_p->show_ans_box = DO_SHOW; } | ANS_BOX_SHOW '=' ANS_OFF { LexiProblem_p->show_ans_box = DONOT_SHOW; @@ -554,54 +583,64 @@ pt_list : pt_list ',' point_coo | pt_range { } ; -pt_range : point_coord ':' point_coord '#' IDENTIFIER { PointsList_t *pt; - - if( LastPtsList != NULL ) { - LastPtsList->pts_next = gen_ptslist( $1, $3, $5 ); - pt = LastPtsList->pts_next; - while( pt->pts_next != NULL ) { - pt = pt->pts_next; - } - LastPtsList = pt; - } else { - CurrPtsList = gen_ptslist( $1, $3, $5 ); - LastPtsList = CurrPtsList; - } - if( $1->s_type == S_CONSTANT ) { - capa_mfree($1->s_str); capa_mfree((char *)$1); - } - if( $3->s_type == S_CONSTANT ) { - capa_mfree($3->s_str); capa_mfree((char *)$3); - } - } - | point_coord ':' point_coord '#' a_number { PointsList_t *pt; - - if( LastPtsList != NULL ) { - LastPtsList->pts_next = gen_ptslist( $1, $3, $5 ); - pt = LastPtsList->pts_next; - while( pt->pts_next != NULL ) { - pt = pt->pts_next; - } - LastPtsList = pt; - } else { - CurrPtsList = gen_ptslist( $1, $3, $5 ); - LastPtsList = CurrPtsList; - } - if( $1->s_type == S_CONSTANT ) { - capa_mfree($1->s_str); capa_mfree((char *)$1); - } - if( $3->s_type == S_CONSTANT ) { - capa_mfree($3->s_str); capa_mfree((char *)$3); - } - if( $5->s_type == I_CONSTANT || $5->s_type == R_CONSTANT) { - capa_mfree((char *)$5); - } - } +pt_range : point_coord ':' point_coord '#' IDENTIFIER + { + assign_pts($1,$3,$5); + /*PointsList_t *pt; + if( LastPtsList != NULL ) { + LastPtsList->pts_next = gen_ptslist( $1, $3, $5 ); + pt = LastPtsList->pts_next; + while( pt->pts_next != NULL ) { + pt = pt->pts_next; + } + LastPtsList = pt; + } else { + CurrPtsList = gen_ptslist( $1, $3, $5 ); + LastPtsList = CurrPtsList; + } + if( $1->s_type == S_CONSTANT ) { + capa_mfree($1->s_str); capa_mfree((char *)$1); + } + if( $3->s_type == S_CONSTANT ) { + capa_mfree($3->s_str); capa_mfree((char *)$3); + } + */ + } + | point_coord ':' point_coord '#' ARRAY_ID '[' calc_expr ']' + { assign_pts($1,$3,get_array_symbol($5,$7,1)); } + | point_coord ':' point_coord '#' a_number + { + assign_pts($1,$3,$5); + /*PointsList_t *pt; + + if( LastPtsList != NULL ) { + LastPtsList->pts_next = gen_ptslist( $1, $3, $5 ); + pt = LastPtsList->pts_next; + while( pt->pts_next != NULL ) { + pt = pt->pts_next; + } + LastPtsList = pt; + } else { + CurrPtsList = gen_ptslist( $1, $3, $5 ); + LastPtsList = CurrPtsList; + } + if( $1->s_type == S_CONSTANT ) { + capa_mfree($1->s_str); capa_mfree((char *)$1); + } + if( $3->s_type == S_CONSTANT ) { + capa_mfree($3->s_str); capa_mfree((char *)$3); + } + if( $5->s_type == I_CONSTANT || $5->s_type == R_CONSTANT) { + capa_mfree((char *)$5); + } + */ + } ; -point_coord : IDENTIFIER { $$ = $1; } - | S_CONSTANT { $$ = $1; } +point_coord : IDENTIFIER { $$ = $1; } + | ARRAY_ID '[' calc_expr ']' { $$ = get_array_symbol($1,$3,1); } + | S_CONSTANT { $$ = $1; } ; @@ -665,6 +704,7 @@ map_expr : startM '(' basic_constr ' a_sp=build_array_list($5,$7->s_argc); result=do_map(key, a_sp->s_argp, $7->s_argp, a_sp->s_argc, FORWARD_MAP); free_arglist(a_sp->s_argp); + a_sp->s_argp=NULL; } else { sprintf(warn_msg,"/MAP arg. counts are not matched.\n"); capa_msg(MESSAGE_ERROR,warn_msg); @@ -676,7 +716,9 @@ map_expr : startM '(' basic_constr ' capa_msg(MESSAGE_ERROR,warn_msg); } free_arglist($5->s_argp); - free_arglist($7->s_argp); + $5->s_argp=NULL; + free_arglist($7->s_argp); + $7->s_argp=NULL; } | startR '(' basic_constr ';' var_list ';' arg_list ')' { char key[SMALL_LINE_BUFFER]; @@ -694,6 +736,7 @@ map_expr : startM '(' basic_constr ' a_sp=build_array_list($5,$7->s_argc); result=do_map(key, a_sp->s_argp, $7->s_argp, a_sp->s_argc, FORWARD_MAP); free_arglist(a_sp->s_argp); + a_sp->s_argp=NULL; } else { sprintf(warn_msg,"/RMAP arg. counts are not matched.\n"); capa_msg(MESSAGE_ERROR,warn_msg); @@ -705,7 +748,9 @@ map_expr : startM '(' basic_constr ' capa_msg(MESSAGE_ERROR,warn_msg); } free_arglist($5->s_argp); + $5->s_argp=NULL; free_arglist($7->s_argp); + $7->s_argp=NULL; } ; @@ -757,6 +802,7 @@ basic_constr : FUNCTION_ID '(' ')' { $$ = do_function(tmp, $3->s_argc, $3->s_argp); capa_mfree(FuncStack[Func_idx].s_name); free_arglist($3->s_argp); + $3->s_argp=NULL; } YYDBUG_PR1(" basic_constr <= RETURN FUNCT "); YYDBUG_SYM($$); @@ -834,7 +880,10 @@ an_integer : I_CONSTANT a_real : R_CONSTANT { $$ = $1; } ; -an_array : ARRAY_ID '[' calc_expr ']' { $$=get_array_symbol($1,$3,1); } +an_array : ARRAY_ID '[' calc_expr ']' { + YYDBUG_PR1(" an_array <= ARRAY_ID '['calc_expr ']' "); + $$=get_array_symbol($1,$3,1); + } ; startQ : { /* first matching will occur before first line of input text */ @@ -917,7 +966,7 @@ Symbol *name,*index;int free_symbols; sprintf(key,"%s[%s]",name->s_name,tmp); a_p = find_arrayid(name->s_name); /* use the array name to search array tree */ - /* */ + /* did not check for error! */ s_p = find_array_by_index(a_p,key); /* use the index portion to search along array linked list */ capa_mfree((char *)tmp); capa_mfree((char *)key); @@ -1078,7 +1127,7 @@ append_warn(type,str) int type;char *str /*********** if *b is a constant symbol, destroy (free) b ********************/ /*********** if *a is a var symbol, create a new symbol **********************/ /* do not free(*a) */ -/* */ +/* If either a or b is invalid it propagates the error up the parse tree */ Symbol * symbols_op(a, b, op) Symbol *a; Symbol *b; int op; { @@ -1093,12 +1142,11 @@ if( a->s_type == IDENTIFIER || b->s_type if(a->s_type == IDENTIFIER) { /* a is IDENTIFIER */ sprintf(warn_msg,"var \"%s\" not defined before use.\n", a->s_name); capa_msg(MESSAGE_ERROR,warn_msg); - return (b); + return (a); } else { /* b is IDENTIFIER */ - sprintf(warn_msg, - "var \"%s\" not defined before use.\n",b->s_name); + sprintf(warn_msg,"var \"%s\" not defined before use.\n",b->s_name); capa_msg(MESSAGE_ERROR,warn_msg); - return (a); + return (b); } } else { /* a and b are neither identifiers */ if( (a->s_type == I_VAR) || @@ -1789,6 +1837,32 @@ assign_id_list( s ) Symbol *s; } } +/* void assign_pts ( Symbol* coord1, Symbol* coord2, Symbol* num) { */ +void assign_pts (coord1, coord2, num)Symbol *coord1;Symbol *coord2;Symbol *num; +{ + PointsList_t *pt; + if( LastPtsList != NULL ) { + LastPtsList->pts_next = gen_ptslist( coord1, coord2, num ); + pt = LastPtsList->pts_next; + while( pt->pts_next != NULL ) { + pt = pt->pts_next; + } + LastPtsList = pt; + } else { + CurrPtsList = gen_ptslist( coord1, coord2, num ); + LastPtsList = CurrPtsList; + } + if(coord1->s_type == S_CONSTANT) { + capa_mfree(coord1->s_str); capa_mfree((char *)coord1); + } + if(coord2->s_type == S_CONSTANT) { + capa_mfree(coord2->s_str); capa_mfree((char *)coord2); + } + if(num->s_type == I_CONSTANT || num->s_type == R_CONSTANT) { + capa_mfree((char *)num); + } +} + /* =========================================================================== */