1: /* ========================================================================== */
2: /* capaGrammarDef.y created by Isaac Tsai */
3: /* 1998, 1999 copyrighted by Isaac Tsai */
4: /* no longer there is a length constrain on string concatenation July 13 1998 */
5: /* /RMAP() function */
6: /* TODO: new mechanism to collect answer informations */
7: /*****************************************************************************/
8: /*****************************************************************************/
9: /*****************************************************************************/
10: %{
11: #include <stdio.h>
12: #include <ctype.h>
13: #include <string.h>
14: #include <math.h>
15: #include "capaParser.h" /* _symbol structure def */
16: #include "capaCommon.h"
17: #include "capaFunction.h"
18: #include "capaRQO.h"
19: #ifdef __hpux
20: #include <stdlib.h>
21: #include <alloca.h>
22: #endif
23:
24: #ifdef YACC_DBUG
25: #define YYDBUG_PR1(xx) { printf(xx); fflush(stdout); }
26: #define YYDBUG_PR2(xx,yy) { printf(xx,yy); fflush(stdout); }
27: #define YYDBUG_PR3(xx,yy,zz) { printf(xx,yy,zz); fflush(stdout); }
28: #define YYDBUG_PR4(xx,yy,zz,aa) { printf(xx,yy,zz,aa); fflush(stdout); }
29: #define YYDBUG_SYM(xx) { switch((xx)->s_type) { \
30: case IDENTIFIER: \
31: printf("ID(%s)\n",(xx)->s_name); break; \
32: case I_VAR: case I_CONSTANT: \
33: printf("INT(%d)\n",(xx)->s_int); break; \
34: case R_VAR: case R_CONSTANT: \
35: printf("REAL(%.16g)\n",(xx)->s_real); break; \
36: case S_VAR: case S_CONSTANT: \
37: printf("STR(%s)\n",(xx)->s_str); break; \
38: } }
39: #else
40: #define YYDBUG_PR1(xx) { }
41: #define YYDBUG_PR2(xx,yy) { }
42: #define YYDBUG_PR3(xx,yy,zz) { }
43: #define YYDBUG_PR4(xx,yy,zz,aa) { }
44: #define YYDBUG_SYM(xx) { }
45: #endif
46:
47: int yylex();
48: void yyerror(char*);
49: void free_calc_expr(Symbol*);
50: /******************************************************************************/
51: /* GLOBAL VARIABLES */
52: /******************************************************************************/
53: int Lexi_qnum;
54:
55: extern int Lexi_line;
56: extern int Lexi_pos[MAX_OPENED_FILE];
57: extern char Opened_filename[MAX_OPENED_FILE][QUARTER_K];
58: extern int Input_idx;
59: int Current_line[MAX_OPENED_FILE];
60: extern int Func_idx;
61: extern Symbol FuncStack[MAX_FUNC_NEST];
62: extern int IFstatus[MAX_FUNC_NEST];
63: extern int IFcount;
64: extern int gUnitError;
65:
66: int Parsemode_f; /* Parser mode flag */
67:
68: Problem_t *FirstProblem_p; /* First problem */
69: Problem_t *LastProblem_p; /* Last problem */
70: Problem_t *LexiProblem_p; /* Current problem */
71: char *EndText_p;
72: char *StartText_p;
73: char *ErrorMsg_p;
74: int ErrorMsg_count;
75: WarnMsg_t *WarnMsg_p;
76: int WarnMsg_count;
77: int Answer_infospec;
78: void (*Status_Func)();
79: AnswerInfo_t CurrAnsInfo;
80:
81: RandQO_t *QuestionOrder;
82: PointsList_t *CurrPtsList;
83: PointsList_t *LastPtsList;
84:
85:
86:
87:
88: #ifdef YYSTYPE
89: #undef YYSTYPE
90: #endif
91: #define YYSTYPE Symbol_p
92:
93:
94: #define ADD_op 1
95: #define SUB_op 2
96: #define MUL_op 3
97: #define DIV_op 4
98: #define IDIV_op 5
99: #define NOT_DEFINED_op 9
100:
101: /*#define yyerror printf*/
102:
103: %}
104: %token NEW_ID
105: %token I_CONSTANT R_CONSTANT S_CONSTANT
106: %token I_VAR R_VAR S_VAR
107:
108: %token IDENTIFIER FUNCTION_ID ARRAY_ID
109: %token HINT_LINE EXPLAIN_LINE TEXT_LINE IMPORT_LINE
110: %token CAPA_LET CAPA_DEF CAPA_DIS CAPA_END CAPA_VAR
111: %token CAPA_ESC CAPA_MAP CAPA_FIG CAPA_ANS CAPA_RMAP
112: %token CAPA_IF CAPA_ELSE CAPA_ENDIF CAPA_SUBJ CAPA_WHILE
113: %token CAPA_RQO CAPA_ENDWHILE CAPA_START
114: %token ANS_AND ANS_BOX_SHOW ANS_CALC ANS_CI ANS_COMPARE ANS_CS
115: %token ANS_EVAL ANS_EXPLAIN ANS_EXTERNAL ANS_FMT
116: %token ANS_FORMULA ANS_HINT ANS_MC ANS_MINUS
117: %token ANS_OFF ANS_ON ANS_OR ANS_ORDERED
118: %token ANS_PATH ANS_PCREDIT ANS_PLUS ANS_RANGE
119: %token ANS_SHOW_BR ANS_SIG ANS_TOLERANCE ANS_TRY ANS_TYPE
120: %token ANS_UNFMT ANS_UNIT ANS_WEIGHT
121: %token VAR_RANGE VERBATIM
122: %token SLASH FORMAT
123: %token EQ_op NE_op GT_op GE_op LT_op LE_op AND_op OR_op EoL
124:
125: %start prob_set
126:
127: %%
128:
129: prob_set : startQ questions CAPA_END { YYDBUG_PR1(" prob_set := startQ questions END\n\n"); }
130: ;
131:
132: questions : a_line { YYDBUG_PR1(" questions <= a_line");
133: if (Status_Func != NULL) Status_Func();
134: }
135: | questions a_line { YYDBUG_PR1(" questions <= questions a_line");
136: if (Status_Func != NULL) Status_Func();
137: }
138: ;
139:
140: startL : CAPA_LET { YYDBUG_PR1("\n begin_let::"); }
141: ;
142:
143: startV : CAPA_VAR { YYDBUG_PR1(" begin_var"); }
144: ;
145:
146: startA : CAPA_ANS { YYDBUG_PR1("\n START ANSWER(/ANS)::\n");
147: init_answerinfo();
148: }
149: ;
150:
151: startSA : CAPA_SUBJ { YYDBUG_PR1("\n START SUBJECT ANSWER(/SUBJECTIVE)::\n");
152: init_answerinfo();
153: }
154: ;
155:
156: startM : CAPA_MAP { YYDBUG_PR1("\n begin_map::"); }
157: ;
158:
159: startR : CAPA_RMAP { YYDBUG_PR1("\n begin_rmap::"); }
160: ;
161:
162: startRQO : CAPA_RQO { YYDBUG_PR1("\n begin_rqo::"); }
163: ;
164:
165: ans_and_op : ANS_AND { add_answer_cnt(ANS_AND); YYDBUG_PR1("(AND ,new an answer info)"); }
166: ;
167:
168: ans_or_op : ANS_OR { add_answer_cnt(ANS_OR); YYDBUG_PR1("(OR ,new an answer info)"); }
169: ;
170:
171: a_line : startL statement EoL { YYDBUG_PR1(" a_line <= startL statement CR\n"); }
172: | CAPA_END { YYDBUG_PR1(" a_line <= END\n\n"); }
173: | startRQO rqo_def EoL { YYDBUG_PR1(" aline <= CAPA_RQO\n");
174: rqo_finish();
175: }
176: | CAPA_START { YYDBUG_PR1(" aline <= CAPA_START\n");
177: start_question_over();
178: }
179: | HINT_LINE { append_hint($1->s_str);
180: YYDBUG_PR2(" a_line <= Hint_line(%s)\n",$1->s_str);
181: capa_mfree($1->s_str); capa_mfree((char *)$1);
182: }
183: | EXPLAIN_LINE { append_explain($1->s_str);
184: YYDBUG_PR2(" a_line <= Explain_line(%s)\n",$1->s_str);
185: capa_mfree($1->s_str); capa_mfree((char*)$1);
186: }
187: | IMPORT_LINE EoL { YYDBUG_PR1(" a_line <= import_line CR\n"); }
188: | q_text EoL { YYDBUG_PR1(" a_line <= Qtext CR\n"); append_text("\n"); }
189: | answer_expr { YYDBUG_PR1(" a_line <= answer_expr (init a new prob) CR\n");
190: init_new_prob(); }
191: | if_expr { YYDBUG_PR1(" a_line <= if_expr\n"); }
192: | while_expr { YYDBUG_PR1(" a_line <= while_expr\n"); }
193: | map_expr EoL { YYDBUG_PR1(" a_line <= map_expr CR\n"); }
194: | EoL { YYDBUG_PR1(" a_line <= (CR)\n"); }
195: | VERBATIM { YYDBUG_PR1(" a_line <= (VERBATIM)\n");
196: switch(Parsemode_f) {
197: case TeX_MODE: append_text("\\begin{verbatim}");
198: break;
199: case HTML_MODE: append_text("<PRE>");
200: break;
201: }
202: append_text($1->s_str);
203: capa_mfree($1->s_str); capa_mfree((char *)$1);
204: switch(Parsemode_f) {
205: case TeX_MODE: append_text("\\end{verbatim}\n");
206: break;
207: case HTML_MODE: append_text("</PRE>\n");
208: break;
209: }
210: }
211: | error EoL { char warn_msg[WARN_MSG_LENGTH];
212: YYDBUG_PR1(" a_line := ERROR(CR)\n");
213: sprintf(warn_msg," Question %d: Syntax error.\n", Lexi_qnum+1);
214: capa_msg(MESSAGE_ERROR,warn_msg);
215: begin_text();
216: }
217:
218: ;
219:
220: statement : IDENTIFIER '=' calc_expr
221: { char warn_msg[WARN_MSG_LENGTH];
222:
223: if ( $1 != $3 ) { /* /LET a = a */
224: switch($1->s_type) {
225: case IDENTIFIER:
226: case I_VAR: case I_CONSTANT:
227: case R_VAR: case R_CONSTANT: break;
228: case S_VAR: case S_CONSTANT:
229: capa_mfree($1->s_str); $1->s_str = NULL; break;
230: default: break;
231: }
232: switch($3->s_type) {
233: case IDENTIFIER:
234: sprintf(warn_msg,"var \"%s\" not defined before use.\n",$3->s_name);
235: capa_msg(MESSAGE_ERROR,warn_msg);
236: break;
237: case I_VAR: case I_CONSTANT:
238: $1->s_type = I_VAR;
239: $1->s_int = $3->s_int;
240: break;
241: case R_VAR: case R_CONSTANT:
242: $1->s_type = R_VAR;
243: $1->s_real = $3->s_real;
244: break;
245: case S_VAR: case S_CONSTANT:
246: $1->s_type = S_VAR;
247: $1->s_str = strsave($3->s_str);
248: break;
249: }
250: YYDBUG_PR1(" statement <= ID = calc_expr:: "); YYDBUG_SYM($3);
251: free_calc_expr($3);
252: }
253: }
254: | ARRAY_ID '[' calc_expr ']' '=' calc_expr
255: { Symbol *s_p;
256: char warn_msg[WARN_MSG_LENGTH];
257:
258: s_p = get_array_symbol($1,$3,1);
259: switch(s_p->s_type) {
260: case IDENTIFIER:
261: case I_VAR: case I_CONSTANT:
262: case R_VAR: case R_CONSTANT: break;
263: case S_VAR: case S_CONSTANT:
264: capa_mfree(s_p->s_str); s_p->s_str = NULL; break;
265: default: break;
266: }
267: switch($6->s_type) {
268: case IDENTIFIER:
269: sprintf(warn_msg,"var \"%s\" not defined before use.\n",$6->s_name);
270: capa_msg(MESSAGE_ERROR,warn_msg);
271: break;
272: case I_VAR: case I_CONSTANT:
273: s_p->s_type = I_VAR;
274: s_p->s_int = $6->s_int;
275: break;
276: case R_VAR: case R_CONSTANT:
277: s_p->s_type = R_VAR;
278: s_p->s_real = $6->s_real;
279: break;
280: case S_VAR: case S_CONSTANT:
281: s_p->s_type = S_VAR;
282: s_p->s_str = strsave($6->s_str);
283: break;
284: }
285: free_calc_expr($6);
286: }
287: ;
288:
289: rqo_def : rqo_speca { rqo_1spec(); }
290: | rqo_speca "," rqo_def { rqo_2spec(); }
291: ;
292:
293: rqo_speca : an_integer {start_rqo_type(SINGLE);append_rqo($1);}
294: | an_integer "!" {start_rqo_type(IMMOBILE);append_rqo($1);}
295: | an_integer "-" an_integer {start_rqo_type(RANGE);append_rqo($1);
296: append_rqo($3);
297: }
298: | an_integer "~" rqo_specb {prefix_rqo($1);}
299: | an_integer "+" rqo_specc {prefix_rqo($1);}
300: ;
301:
302: rqo_specb : an_integer {start_rqo_type(ALL_MIX);append_rqo($1);}
303: | an_integer "~" rqo_specb {prefix_rqo($1);}
304: | an_integer "+" an_integer {start_rqo_type(LAST_FIXED);append_rqo($1);
305: append_rqo($3);}
306: ;
307:
308: rqo_specc : an_integer {start_rqo_type(ALL_FIXED);append_rqo($1);}
309: | an_integer "+" rqo_specd {prefix_rqo($1);}
310: | an_integer "~" rqo_spece {prefix_rqo($1);}
311: ;
312:
313: rqo_specd : an_integer {start_rqo_type(ALL_FIXED);append_rqo($1);}
314: | an_integer "+" rqo_specd {prefix_rqo($1);}
315: ;
316:
317: rqo_spece : an_integer {start_rqo_type(FIRST_FIXED);append_rqo($1);}
318: | an_integer "~" rqo_spece {prefix_rqo($1);}
319: | an_integer "+" an_integer {start_rqo_type(BOTH_FIXED);append_rqo($1);
320: append_rqo($3);}
321: ;
322:
323:
324: q_text : TEXT_LINE { append_text($1->s_str);
325: capa_mfree($1->s_str); capa_mfree((char *)$1);
326: }
327: | var_expr { }
328: | q_text var_expr { }
329: | q_text TEXT_LINE { append_text($2->s_str);
330: capa_mfree($2->s_str); capa_mfree((char *)$2);
331: }
332: ;
333:
334: if_expr : CAPA_IF '(' calc_expr ')'
335: { int leng; /* begin_next_line(); no use, can be get rid of */
336: YYDBUG_PR2("(IF expr <IFcount=%d>)\n",IFcount);
337: switch($3->s_type) {
338: case IDENTIFIER:
339: IFstatus[IFcount] = IF_FALSE;
340: begin_next_line();
341: break;
342: case I_CONSTANT: case I_VAR:
343: if(!$3->s_int) {
344: IFstatus[IFcount] = IF_FALSE;
345: begin_if_skip();
346: } else {
347: IFstatus[IFcount] = IF_TRUE;
348: begin_next_line();
349: }
350: break;
351: case R_CONSTANT: case R_VAR:
352: if($3->s_real == 0.0) {
353: IFstatus[IFcount] = IF_FALSE;
354: begin_if_skip();
355: }else{
356: IFstatus[IFcount] = IF_TRUE;
357: begin_next_line();
358: }
359: break;
360: case S_CONSTANT:
361: leng = strlen($3->s_str);
362: capa_mfree($3->s_str);
363: if(leng == 0) {
364: IFstatus[IFcount] = IF_FALSE;
365: begin_if_skip();
366: }else{
367: IFstatus[IFcount] = IF_TRUE;
368: begin_next_line();
369: }
370: break;
371: case S_VAR:
372: leng = strlen($3->s_str);
373: if(leng == 0) {
374: IFstatus[IFcount] = IF_FALSE;
375: begin_if_skip();
376: }else{
377: IFstatus[IFcount] = IF_TRUE;
378: begin_next_line();
379: }
380: break;
381: }
382: capa_mfree((char*)$3);
383: }
384: ;
385:
386: while_expr : CAPA_WHILE '(' calc_expr ')'
387: {
388: int leng;
389: YYDBUG_PR1("(WHILE expr)\n");
390: switch($3->s_type) {
391: case IDENTIFIER: /* undefined identifier regarded as false */
392: begin_while_skip();
393: break;
394: case I_CONSTANT: case I_VAR:
395: if(!$3->s_int) {
396: begin_while_skip();
397: } else {
398: begin_next_line(); /* skip to EoL and begin S_TEXT */
399: }
400: break;
401: case R_CONSTANT: case R_VAR:
402: if($3->s_real == 0.0) {
403: begin_while_skip();
404: }else{
405: begin_next_line(); /* skip to EoL and begin S_TEXT */
406: }
407: break;
408: case S_CONSTANT:
409: leng = strlen($3->s_str);
410: capa_mfree($3->s_str);
411: if(leng == 0) {
412: begin_while_skip();
413: }else{
414: begin_next_line(); /* skip to EoL and begin S_TEXT */
415: }
416: break;
417: case S_VAR:
418: leng = strlen($3->s_str);
419: if(leng == 0) {
420: begin_while_skip();
421: }else{
422: begin_next_line(); /* skip to EoL and begin S_TEXT */
423: }
424: break;
425: }
426: capa_mfree((char*)$3);
427: }
428: ;
429:
430: var_expr : startV '(' formated_ans ')'
431: { display_var( $3 ) ; }
432: ;
433:
434: answer_expr : answer_spec { finish_answer_info();
435: }
436: | answer_expr ans_and_op answer_spec
437: { finish_answer_info();
438: YYDBUG_PR1(" answer_expr <-- AND answers (copy answerinfo)\n"); }
439: | answer_expr ans_or_op answer_spec
440: { finish_answer_info();
441: YYDBUG_PR1(" answer_expr <-- OR answers (copy answerinfo)\n");
442: } | startSA '(' answer_info ')'
443: { YYDBUG_PR1("\n subjective answer\n");
444: finish_answer_info();
445: LexiProblem_p->ans_type = ANSWER_IS_SUBJECTIVE;
446: }
447: | startSA '(' ')'
448: { YYDBUG_PR1("\n subjective answer\n");
449: finish_answer_info();
450: LexiProblem_p->ans_type = ANSWER_IS_SUBJECTIVE;
451: }
452:
453: ;
454:
455: answer_spec : startA '(' formated_ans ')'
456: { assign_answer( $3 );
457: YYDBUG_PR1("\nASSIGN Answer\n");
458: }
459: | startA '(' formated_ans ',' answer_info ')'
460: { assign_answer( $3 );
461: YYDBUG_PR1("\nASSIGN Answers + Answer Info\n");
462: }
463: ;
464:
465: answer_info : ans_infospec
466: | answer_info ',' ans_infospec
467: ;
468:
469:
470: ans_infospec : ANS_TOLERANCE '=' a_number
471: { YYDBUG_PR1(" ans_infospec:= TOL=a_number");
472: assign_tolerance(TOL_ABSOLUTE,$3);
473: }
474: | ANS_TOLERANCE '=' IDENTIFIER
475: { assign_tolerance(TOL_ABSOLUTE,$3);
476: }
477: | ANS_TOLERANCE '=' IDENTIFIER '%'
478: { assign_tolerance(TOL_PERCENTAGE,$3);
479: }
480: | ANS_TOLERANCE '=' a_number '%'
481: { assign_tolerance(TOL_PERCENTAGE,$3);
482: }
483: | ANS_COMPARE '=' answer_comp { }
484: | ANS_SIG '=' answer_sig { }
485: | ANS_WEIGHT '=' an_integer
486: { assign_weight( $3 );
487: }
488: | ANS_WEIGHT '=' IDENTIFIER
489: { assign_weight( $3 );
490: }
491: | ANS_HINT '=' an_integer { assign_hint( $3 );
492: }
493: | ANS_HINT '=' IDENTIFIER { assign_hint( $3 );
494: }
495: | ANS_PCREDIT '=' ANS_ON { LexiProblem_p->partial_cdt = 1;
496: }
497: | ANS_PCREDIT '=' ANS_OFF { LexiProblem_p->partial_cdt = 0;
498: }
499: | ANS_SHOW_BR '=' ANS_ON { LexiProblem_p->show_br = DO_SHOW;
500: }
501: | ANS_SHOW_BR '=' ANS_OFF { LexiProblem_p->show_br = DONOT_SHOW;
502: }
503: | ANS_BOX_SHOW '=' ANS_ON { LexiProblem_p->show_ans_box = DO_SHOW;
504: }
505: | ANS_BOX_SHOW '=' ANS_OFF { LexiProblem_p->show_ans_box = DONOT_SHOW;
506: }
507: | ANS_CALC '=' ANS_FMT { CurrAnsInfo.ans_calc = CALC_FORMATED;
508: }
509: | ANS_CALC '=' ANS_UNFMT { CurrAnsInfo.ans_calc = CALC_UNFORMATED;
510: }
511: | ANS_TRY '=' an_integer { assign_try_limits( $3 );
512: }
513: | ANS_TRY '=' IDENTIFIER { assign_try_limits( $3 );
514: }
515: | ANS_UNIT '=' S_CONSTANT { assign_units( $3 ); capa_mfree($3->s_str); capa_mfree((char *)$3);
516: }
517: | ANS_UNIT '=' IDENTIFIER { assign_units( $3 );
518: }
519: | ANS_EVAL '=' var_range { CurrAnsInfo.ans_pts_list = CurrPtsList;
520: CurrPtsList=NULL; LastPtsList = NULL;
521: }
522: ;
523:
524:
525: var_range : '<' S_CONSTANT '@' pt_list '>' { assign_id_list( $2 );
526: capa_mfree($2->s_str);
527: capa_mfree((char *)$2);
528: }
529: | '<' IDENTIFIER '@' pt_list '>' { assign_id_list( $2 ); }
530: ;
531:
532: pt_list : pt_list ',' point_coord { int idx;
533: idx = LastPtsList->pts_idx; idx++;
534: LastPtsList->pts_next = new_ptslist( $3 );
535: LastPtsList = LastPtsList->pts_next;
536: LastPtsList->pts_idx = idx;
537: CurrPtsList->pts_idx = idx;
538: if( $3->s_type == S_CONSTANT ) {
539: capa_mfree($3->s_str); capa_mfree((char *)$3);
540: }
541: }
542: | pt_list ',' pt_range { }
543: | point_coord { CurrPtsList = new_ptslist( $1 );
544: LastPtsList = CurrPtsList;
545: if( $1->s_type == S_CONSTANT ) {
546: capa_mfree($1->s_str); capa_mfree((char *)$1);
547: }
548: }
549: | pt_range { }
550: ;
551:
552: pt_range : point_coord ':' point_coord '#' IDENTIFIER { PointsList_t *pt;
553:
554: if( LastPtsList != NULL ) {
555: LastPtsList->pts_next = gen_ptslist( $1, $3, $5 );
556: pt = LastPtsList->pts_next;
557: while( pt->pts_next != NULL ) {
558: pt = pt->pts_next;
559: }
560: LastPtsList = pt;
561: } else {
562: CurrPtsList = gen_ptslist( $1, $3, $5 );
563: LastPtsList = CurrPtsList;
564: }
565: if( $1->s_type == S_CONSTANT ) {
566: capa_mfree($1->s_str); capa_mfree((char *)$1);
567: }
568: if( $3->s_type == S_CONSTANT ) {
569: capa_mfree($3->s_str); capa_mfree((char *)$3);
570: }
571: }
572: | point_coord ':' point_coord '#' a_number { PointsList_t *pt;
573:
574: if( LastPtsList != NULL ) {
575: LastPtsList->pts_next = gen_ptslist( $1, $3, $5 );
576: pt = LastPtsList->pts_next;
577: while( pt->pts_next != NULL ) {
578: pt = pt->pts_next;
579: }
580: LastPtsList = pt;
581: } else {
582: CurrPtsList = gen_ptslist( $1, $3, $5 );
583: LastPtsList = CurrPtsList;
584: }
585: if( $1->s_type == S_CONSTANT ) {
586: capa_mfree($1->s_str); capa_mfree((char *)$1);
587: }
588: if( $3->s_type == S_CONSTANT ) {
589: capa_mfree($3->s_str); capa_mfree((char *)$3);
590: }
591: if( $5->s_type == I_CONSTANT || $5->s_type == R_CONSTANT) {
592: capa_mfree((char *)$5);
593: }
594: }
595: ;
596:
597:
598: point_coord : IDENTIFIER { $$ = $1; }
599: | S_CONSTANT { $$ = $1; }
600: ;
601:
602:
603:
604: formated_ans : calc_expr { $1->s_distype = DEFAULT_FORMAT;
605: $$ = $1;
606: $1->s_format = NULL;
607: YYDBUG_PR2(" formated_ans := calc_expr (type %d)",$1->s_type);
608: }
609: | calc_expr FORMAT { $1->s_distype = $2->s_distype;
610: $1->s_format = strsave($2->s_str); /* **** */
611: capa_mfree($2->s_str); capa_mfree((char *)$2);
612: $$ = $1;
613: YYDBUG_PR1(" formated_ans <= calc_expr FORMAT");
614: }
615: ;
616:
617:
618:
619: answer_sig : an_integer { assign_sigs( $1->s_int,$1->s_int);
620: capa_mfree((char *)$1);
621: }
622: | an_integer ANS_PLUS an_integer
623: { assign_sigs($1->s_int,$1->s_int + $3->s_int);
624: capa_mfree((char *)$1); capa_mfree((char *)$3);
625: }
626: | an_integer ANS_MINUS an_integer
627: { assign_sigs($1->s_int - $3->s_int,$1->s_int);
628: capa_mfree((char *)$1); capa_mfree((char *)$3);
629: }
630: | an_integer ANS_PLUS an_integer ANS_MINUS an_integer
631: { assign_sigs($1->s_int - $5->s_int,$1->s_int + $3->s_int);
632: capa_mfree((char *)$1); capa_mfree((char *)$3); capa_mfree((char *)$5);
633: }
634: | an_integer ANS_MINUS an_integer ANS_PLUS an_integer
635: { assign_sigs($1->s_int - $3->s_int,$1->s_int + $5->s_int);
636: capa_mfree((char *)$1); capa_mfree((char *)$3); capa_mfree((char *)$5);
637: }
638: ;
639:
640: answer_comp : ANS_CS { CurrAnsInfo.ans_type = ANSWER_IS_STRING_CS; }
641: | ANS_CI { CurrAnsInfo.ans_type = ANSWER_IS_STRING_CI; }
642: | ANS_MC { CurrAnsInfo.ans_type = ANSWER_IS_CHOICE; }
643: | ANS_FORMULA { CurrAnsInfo.ans_type = ANSWER_IS_FORMULA; }
644: | ANS_EXTERNAL { CurrAnsInfo.ans_type = ANSWER_IS_EXTERNAL; }
645: ;
646:
647:
648: map_expr : startM '(' basic_constr ';' var_list ';' arg_list ')'
649: { char key[SMALL_LINE_BUFFER];
650: char warn_msg[WARN_MSG_LENGTH];
651: int result=0;
652:
653: YYDBUG_PR1(" map_expr body executed\n");
654: sprintf(key,"%ld",$3->s_int);
655: if( $5->s_argc == $7->s_argc ) {
656: result=do_map(key, $5->s_argp, $7->s_argp, $5->s_argc, FORWARD_MAP);
657: } else {
658: if ($5->s_argc==1) {
659: Symbol *a_sp;
660: a_sp=build_array_list($5,$7->s_argc);
661: result=do_map(key, a_sp->s_argp, $7->s_argp, a_sp->s_argc, FORWARD_MAP);
662: free_arglist(a_sp->s_argp);
663: } else {
664: sprintf(warn_msg,"/MAP arg. counts are not matched.\n");
665: capa_msg(MESSAGE_ERROR,warn_msg);
666: }
667: }
668: if (result!=0) {
669: sprintf(warn_msg,
670: "/MAP had invalid arguments.\n");
671: capa_msg(MESSAGE_ERROR,warn_msg);
672: }
673: free_arglist($5->s_argp);
674: free_arglist($7->s_argp);
675: }
676: | startR '(' basic_constr ';' var_list ';' arg_list ')'
677: { char key[SMALL_LINE_BUFFER];
678: char warn_msg[WARN_MSG_LENGTH];
679: int result=0;
680:
681: YYDBUG_PR1(" rmap_expr body executed\n");
682: sprintf(key,"%ld",$3->s_int);
683: if( $5->s_argc == $7->s_argc ) {
684: result=do_map(key, $5->s_argp, $7->s_argp, $5->s_argc, REVERSE_MAP);
685:
686: } else {
687: if ($5->s_argc==1) {
688: Symbol *a_sp;
689: a_sp=build_array_list($5,$7->s_argc);
690: result=do_map(key, a_sp->s_argp, $7->s_argp, a_sp->s_argc, FORWARD_MAP);
691: free_arglist(a_sp->s_argp);
692: } else {
693: sprintf(warn_msg,"/RMAP arg. counts are not matched.\n");
694: capa_msg(MESSAGE_ERROR,warn_msg);
695: }
696: }
697: if (result!=0) {
698: sprintf(warn_msg,
699: "/MAP had invalid arguments.\n");
700: capa_msg(MESSAGE_ERROR,warn_msg);
701: }
702: free_arglist($5->s_argp);
703: free_arglist($7->s_argp);
704: }
705: ;
706:
707:
708:
709: calc_expr : calc_expr EQ_op block { $$ = symbols_op($1, $3, EQ_op); }
710: | calc_expr NE_op block { $$ = symbols_op($1, $3, NE_op); }
711: | calc_expr GE_op block { $$ = symbols_op($1, $3, GE_op); }
712: | calc_expr GT_op block { $$ = symbols_op($1, $3, GT_op); }
713: | calc_expr LE_op block { $$ = symbols_op($1, $3, LE_op); }
714: | calc_expr LT_op block { $$ = symbols_op($1, $3, LT_op); }
715: | calc_expr AND_op block { $$ = symbols_op($1, $3, AND_op); }
716: | calc_expr OR_op block { $$ = symbols_op($1, $3, OR_op); }
717: | block { $$ = $1;
718: YYDBUG_PR1(" calc_expr <= block "); YYDBUG_SYM($1); }
719: ;
720:
721: block : block '+' term { $$ = symbols_op($1, $3, ADD_op); YYDBUG_PR1("block <= block '+' term "); YYDBUG_SYM($$); }
722: | block '-' term { $$ = symbols_op($1, $3, SUB_op); }
723: | term { $$ = $1; YYDBUG_PR2(" block <= term YYSTATE(%d) ",yystate); YYDBUG_SYM($1); }
724: ;
725:
726: term : term '*' basic_constr { $$ = symbols_op($1, $3, MUL_op); }
727: | term '/' basic_constr { $$ = symbols_op($1, $3, DIV_op); }
728: | term '%' basic_constr { $$ = symbols_op($1, $3, IDIV_op); }
729: | basic_constr { $$ = $1;
730: YYDBUG_PR1(" term <= basic_constr "); YYDBUG_SYM($1); }
731: ;
732:
733: basic_constr : FUNCTION_ID '(' ')' { int tmp;
734:
735: Func_idx--;
736: if(Func_idx >= 0 ) {
737: tmp = match_function(FuncStack[Func_idx].s_name,0);
738: $$ = do_function(tmp, 0, NULL );
739: capa_mfree(FuncStack[Func_idx].s_name);
740: }
741:
742: }
743: | FUNCTION_ID '(' arg_list ')'
744: { int tmp;
745:
746: Func_idx--;
747: YYDBUG_PR4(" basic_constr <= FUNCTION<%s><argc=%d> YYSTATE(%d) ",
748: FuncStack[Func_idx].s_name,$3->s_argc,yystate);
749:
750: if(Func_idx >= 0 ) {
751: tmp = match_function(FuncStack[Func_idx].s_name,$3->s_argc);
752: $$ = do_function(tmp, $3->s_argc, $3->s_argp);
753: capa_mfree(FuncStack[Func_idx].s_name);
754: free_arglist($3->s_argp);
755: }
756: YYDBUG_PR1(" basic_constr <= RETURN FUNCT "); YYDBUG_SYM($$);
757:
758: }
759: | an_array { $$ = $1; }
760: | IDENTIFIER { /* do not free identifier */
761: $$ = $1;
762: }
763: | '-' basic_constr { $$ = $2;
764: switch($2->s_type) {
765: case I_VAR: $$ = (Symbol *)capa_malloc(sizeof(Symbol),1);
766: $$->s_type = I_CONSTANT;
767: case I_CONSTANT: $$->s_int = - $2->s_int; break;
768: case R_VAR: $$ = (Symbol *)capa_malloc(sizeof(Symbol),1);
769: $$->s_type = R_CONSTANT;
770: case R_CONSTANT: $$->s_real = (-1.0)*($2->s_real);
771: break;
772: case S_VAR:
773: case S_CONSTANT: break;
774: default: break;
775: }
776: }
777: | '+' basic_constr { $$ = $2; }
778: | S_CONSTANT { $$ = $1; }
779: | a_number { $$ = $1; }
780: | '(' calc_expr ')' { $$ = $2; }
781: ;
782:
783: arg_list : arg_list ',' calc_expr { $$ = $1;
784: $$->s_argc++;
785: $$->s_argp = addto_arglist($1->s_argp, $3);
786: }
787: | calc_expr { $$ = $1;
788: $$->s_argc = 1;
789: $$->s_argp = new_arglist($1);
790: }
791: ;
792:
793:
794: var_list : IDENTIFIER { /* do not free identifier */
795: YYDBUG_PR1(" var_list <= ID");
796: $$ = $1;
797: $$->s_argc = 1;
798: $$->s_argp = new_arglist($1);
799: }
800: | ARRAY_ID '[' calc_expr ']'{
801: YYDBUG_PR1(" var_list <= ARRAYID,calc");
802: $$ = get_array_symbol($1,$3,1);
803: $$->s_argc = 1;
804: $$->s_argp = new_arglist($$);
805: }
806: | var_list ',' ARRAY_ID '[' calc_expr ']' {
807: YYDBUG_PR1(" var_list <= var_list,ARRAYID,calc");
808: $$ = $1;
809: $$->s_argc++;
810: $$->s_argp = addto_arglist($1->s_argp,
811: get_array_symbol($3,$5,1));
812: }
813: | var_list ',' IDENTIFIER { /* do not free identifier */
814: YYDBUG_PR1(" var_list <= var_list,ID");
815: $$ = $1;
816: $$->s_argc++;
817: $$->s_argp = addto_arglist($1->s_argp, $3);
818: }
819: ;
820:
821:
822: a_number : an_integer { $$ = $1; }
823: | a_real { $$ = $1; }
824: ;
825:
826: an_integer : I_CONSTANT { $$ = $1; }
827: ;
828:
829: a_real : R_CONSTANT { $$ = $1; }
830: ;
831:
832: an_array : ARRAY_ID '[' calc_expr ']' { $$=get_array_symbol($1,$3,1); }
833: ;
834:
835: startQ : { /* first matching will occur before first line of input text */
836: YYDBUG_PR1(" startQ := begin_question\n");
837: begin_question(); Answer_infospec = 0;
838: }
839: ;
840:
841:
842:
843: %%
844: /* ============================================================================ */
845: ExpNode_p
846: mk_node(op, left, right) int op; ExpNode_p left; ExpNode_p right;
847: {
848: ExpNode *np;
849:
850: np = (ExpNode* )malloc(sizeof(ExpNode));
851: np->e_type = op;
852: np->e_lsibp = left;
853: np->e_rsibp = right;
854: left->e_parentp = np;
855: right->e_parentp = np;
856: return (np);
857: }
858:
859: ExpNode_p
860: mk_leaf(type, valp) int type; Symbol_p valp;
861: {
862: ExpNode *np;
863:
864: np = (ExpNode*)malloc(sizeof(ExpNode));
865: np->e_type = IDENTIFIER;
866: np->e_sp = valp;
867: return (np);
868:
869: }
870:
871: /* ------------------------------------------------------------- */
872: void free_calc_expr(Symbol *expr)
873: {
874: switch(expr->s_type) {
875: case I_CONSTANT:
876: case R_CONSTANT: capa_mfree((char *)expr); break;
877: case S_CONSTANT: capa_mfree(expr->s_str); capa_mfree((char *)expr); break;
878: default: break;
879: }
880: }
881:
882: /* this is the entry point to array symbol retrieval */
883: /* array main name and index are used to locate the symbol */
884: /* name of the array is used to retrieve the array symbol */
885:
886: Symbol* get_array_symbol ( name,index,free_symbols )
887: Symbol *name,*index;int free_symbols;
888: {
889: Symbol *s_p, *a_p;
890: char *key, *tmp;
891: int leng, idx_len;
892: leng = strlen(name->s_name)+8; /* [ ] */
893:
894: switch(index->s_type) {
895: case I_VAR:
896: case I_CONSTANT: tmp = (char *)capa_malloc(64,1);
897: sprintf(tmp,"%ld",index->s_int);
898: break;
899: case R_VAR:
900: case R_CONSTANT: tmp = (char *)capa_malloc(64,1);
901: sprintf(tmp,"%g",index->s_real);
902: break;
903: case S_VAR:
904: case S_CONSTANT: idx_len = strlen(index->s_str); tmp = (char *)capa_malloc(idx_len+4,1);
905: sprintf(tmp,"\"%s\"",index->s_str); /* string array index is a bit different from integer one */
906: break;
907: default: break;
908: }
909: idx_len = strlen(tmp);
910:
911: key = (char *)capa_malloc(idx_len+leng,1);
912: sprintf(key,"%s[%s]",name->s_name,tmp);
913:
914: a_p = find_arrayid(name->s_name); /* use the array name to search array tree */
915: /* */
916: s_p = find_array_by_index(a_p,key); /* use the index portion to search along array linked list */
917: capa_mfree((char *)tmp); capa_mfree((char *)key);
918:
919: if (free_symbols) { /* free both the name symbol and index symbol */
920: if( (index->s_type == I_CONSTANT) || (index->s_type == R_CONSTANT) )
921: capa_mfree((char *)index);
922: if(index->s_type == S_CONSTANT) {
923: capa_mfree(index->s_str); capa_mfree((char *)index);
924: }
925: capa_mfree(name->s_name); capa_mfree((char *)name);
926: }
927: return (s_p);
928: }
929:
930: Symbol * build_array_list(ar_name,num_elem)
931: Symbol *ar_name;int num_elem;
932: {
933: int i;
934: Symbol *arg_list,*a_p;
935: char idx_str[MAX_BUFFER_SIZE];
936:
937: a_p = find_arrayid(ar_name->s_name);
938: i = 1;
939: sprintf(idx_str,"%s[%d]",ar_name->s_name,i); /* create array elements with integer index */
940: arg_list = find_array_by_index(a_p,idx_str); /* will create a new element if not found */
941: arg_list->s_argc=1;
942: arg_list->s_argp=new_arglist(arg_list);
943:
944: for (i=2;i<=num_elem;i++) {
945: sprintf(idx_str,"%s[%d]",ar_name->s_name,i);
946: arg_list->s_argc++;
947: arg_list->s_argp=addto_arglist(arg_list->s_argp,find_array_by_index(a_p,idx_str));
948: }
949: return arg_list;
950: }
951:
952:
953:
954:
955:
956:
957:
958: /* ------------------------------------------------------------- */
959: void
960: append_text(str) char *str;
961: {
962: char *q;
963: int i;
964:
965: if (!LexiProblem_p->question) {
966: if (!(q = capa_malloc(strlen(str)+1,1))) printf("No room to append.");
967: strncpy(q,str,strlen(str)+1);
968: } else {
969: i = strlen(LexiProblem_p->question);
970: i += (strlen(str)+1);
971: q = capa_malloc(i,1); /* *** */
972: if (!q) printf("No room to append().");
973: strcat(strncpy(q,LexiProblem_p->question, strlen(LexiProblem_p->question)+1), str);
974: capa_mfree(LexiProblem_p->question);
975: }
976: LexiProblem_p->question=q;
977: }
978:
979: /******************************************************************************/
980: /* ADD A STRING TO THE CURRENT HINT TEXT BLOCK */
981: /******************************************************************************/
982: void /* RETURNS: nothing */
983: append_hint(str) /* ARGUMENTS: */
984: char *str; /* String to add */
985: { /* LOCAL VARIABLES: */
986: char *q; /* New string */
987:
988: if (!LexiProblem_p->hint) {
989: if (!(q = capa_malloc(strlen(str)+1,1)))
990: printf("no room");
991: strncpy(q,str,strlen(str)+1);
992: } else {
993: if (!(q = capa_malloc(strlen(LexiProblem_p->hint)+strlen(str)+1,1)))
994: printf("no room");
995: strcat(strncpy(q,LexiProblem_p->hint,strlen(LexiProblem_p->hint)), str);
996: capa_mfree(LexiProblem_p->hint);
997: }
998: LexiProblem_p->hint=q;
999: /* printf("APPEND HINT: %s\n", str); */
1000: }
1001: /******************************************************************************/
1002: /* ADD A STRING TO THE CURRENT EXPLAIN TEXT BLOCK */
1003: /******************************************************************************/
1004: void /* RETURNS: nothing */
1005: append_explain(str) /* ARGUMENTS: */
1006: char *str; /* String to add */
1007: { /* LOCAL VARIABLES: */
1008: char *q; /* New string */
1009:
1010: if (!LexiProblem_p->explain) {
1011: if (!(q = capa_malloc(strlen(str)+1,1)))
1012: printf("no room");
1013: strncpy(q,str, strlen(str)+1);
1014: } else {
1015: if (!(q = capa_malloc(strlen(LexiProblem_p->explain)+strlen(str)+1,1)))
1016: printf("no room");
1017: strcat(strncpy(q,LexiProblem_p->explain,strlen(LexiProblem_p->explain)+1), str);
1018: capa_mfree(LexiProblem_p->explain);
1019: }
1020: LexiProblem_p->explain=q;
1021: /* printf("APPEND EXPLAIN: %s\n", str); */
1022: }
1023:
1024: void
1025: append_error(str) char *str;
1026: {
1027: char *q;
1028: int i;
1029:
1030: ErrorMsg_count++;
1031: if (!ErrorMsg_p) {
1032: if (!(q = capa_malloc(strlen(str)+1,1))) printf("No room in append.");
1033: strncpy(q,str,strlen(str)+1);
1034: } else {
1035: i = strlen(ErrorMsg_p);
1036: i += (strlen(str)+1);
1037: q = capa_malloc(i,1); /* *** */
1038: if (!q) printf("No room in append()");
1039: strcat(strncpy(q,ErrorMsg_p, strlen(ErrorMsg_p)+1), str);
1040: capa_mfree(ErrorMsg_p);
1041: }
1042: ErrorMsg_p=q;
1043: /* printf("APPEND ERROR: %s\n", str); */
1044: }
1045: void
1046: append_warn(type,str) int type;char *str;
1047: {
1048: WarnMsg_t *p, *t;
1049: char *q;
1050:
1051: WarnMsg_count++;
1052: if (!WarnMsg_p) {
1053: if (!(p = (WarnMsg_t *)capa_malloc(sizeof(WarnMsg_t),1))) printf("No room in create WarnMsg_t.");
1054: if (!(q = capa_malloc(strlen(str)+1,1))) printf("No room in allocating space for warn msg.");
1055: strncpy(q,str,strlen(str)+1);
1056: p->warn_next = NULL;
1057: p->warn_type = type;
1058: p->warn_str = q;
1059: WarnMsg_p=p;
1060: } else {
1061: for(t=WarnMsg_p;t->warn_next;t=t->warn_next) { } /* do nothing within for loop */
1062: if (!(p = (WarnMsg_t *)capa_malloc(sizeof(WarnMsg_t),1))) printf("No room in create WarnMsg_t.");
1063: if (!(q = capa_malloc(strlen(str)+1,1))) printf("No room in allocating space for warn msg.");
1064: strncpy(q,str,strlen(str)+1);
1065: p->warn_next = NULL;
1066: p->warn_type = type;
1067: p->warn_str = q;
1068: t->warn_next = p;
1069: }
1070: }
1071:
1072: /*****************************************************************************/
1073: /*********** if *b is a constant symbol, destroy (free) b ********************/
1074: /*********** if *a is a var symbol, create a new symbol **********************/
1075: /* do not free(*a) */
1076: /* */
1077: Symbol *
1078: symbols_op(a, b, op) Symbol *a; Symbol *b; int op;
1079: {
1080: int type, new, leng;
1081: long tmp_i, tmp_j;
1082: double tmp_p, tmp_q;
1083: char a_str[QUARTER_K], *b_str=NULL, *r_strp;
1084: char warn_msg[ONE_K];
1085: Symbol *a_symp;
1086:
1087: if( a->s_type == IDENTIFIER || b->s_type == IDENTIFIER ) {
1088: if(a->s_type == IDENTIFIER) { /* a is IDENTIFIER */
1089: sprintf(warn_msg,"var \"%s\" not defined before use.\n", a->s_name);
1090: capa_msg(MESSAGE_ERROR,warn_msg);
1091: return (b);
1092: } else { /* b is IDENTIFIER */
1093: sprintf(warn_msg,
1094: "var \"%s\" not defined before use.\n",b->s_name);
1095: capa_msg(MESSAGE_ERROR,warn_msg);
1096: return (a);
1097: }
1098: } else { /* a and b are neither identifiers */
1099: if( (a->s_type == I_VAR) ||
1100: (a->s_type == R_VAR) ||
1101: (a->s_type == S_VAR) ) {
1102: a_symp = (Symbol *)capa_malloc(sizeof(Symbol),1); /* *** */
1103: new = 1;
1104: } else {
1105: new = 0;
1106: a_symp = a; /* re-use symbol *a */
1107: }
1108: if( ((a->s_type == I_CONSTANT)||(a->s_type == I_VAR)) &&
1109: ((b->s_type == I_CONSTANT)||(b->s_type == I_VAR)) ) { /* both a and b are integer */
1110: type = I_CONSTANT;
1111: switch( op ) {
1112: case ADD_op: a_symp->s_int = a->s_int + b->s_int ; break;
1113: case SUB_op: a_symp->s_int = a->s_int - b->s_int ; break;
1114: case MUL_op: a_symp->s_int = a->s_int * b->s_int ; break;
1115: case DIV_op: if(b->s_int != 0) {
1116: a_symp->s_int = a->s_int / b->s_int ;
1117: } else {
1118: sprintf(warn_msg,"division (/) by zero!\n");
1119: capa_msg(MESSAGE_ERROR,warn_msg);
1120: } break;
1121: case IDIV_op: if(b->s_int != 0) {
1122: a_symp->s_int = (a->s_int % b->s_int );
1123: } else {
1124: sprintf(warn_msg,"integer division (%%) by zero!\n");
1125: capa_msg(MESSAGE_ERROR,warn_msg);
1126: } break;
1127: case EQ_op: a_symp->s_int = ((a->s_int == b->s_int)? 1: 0); break;
1128: case NE_op: a_symp->s_int = ((a->s_int == b->s_int)? 0: 1); break;
1129: case GT_op: a_symp->s_int = ((a->s_int > b->s_int)? 1: 0); break;
1130: case GE_op: a_symp->s_int = ((a->s_int >= b->s_int)? 1: 0); break;
1131: case LT_op: a_symp->s_int = ((a->s_int < b->s_int)? 1: 0); break;
1132: case LE_op: a_symp->s_int = ((a->s_int <= b->s_int)? 1: 0); break;
1133: case AND_op: a_symp->s_int = ((a->s_int && b->s_int)? 1: 0); break;
1134: case OR_op: a_symp->s_int = ((a->s_int || b->s_int)? 1: 0); break;
1135: }
1136: } else { /* a, b neither are integers */
1137: if( (a->s_type == S_VAR) || (a->s_type == S_CONSTANT) ||
1138: (b->s_type == S_VAR) || (b->s_type == S_CONSTANT) ) { /* either a or b is a string */
1139: type = S_CONSTANT; /* the return type is a string */
1140: if( (a->s_type == S_VAR) || (a->s_type == S_CONSTANT) ) { /* a is a string */
1141: switch( b->s_type ) {
1142: case I_VAR:
1143: case I_CONSTANT:
1144: leng = SMALL_LINE_BUFFER; /* assuming a long integer does not exceed 128 digits*/
1145: b_str= capa_malloc(sizeof(char), leng);
1146: sprintf(b_str,"%ld", b->s_int);
1147: break;
1148: case R_VAR:
1149: case R_CONSTANT:
1150: leng = SMALL_LINE_BUFFER;/*assuming a double does not exceed128chars*/
1151: b_str= capa_malloc(sizeof(char), leng);
1152: sprintf(b_str,"%.15g", b->s_real);
1153: break;
1154: case S_VAR:
1155: case S_CONSTANT: /* DONE: get rid of limitations on b_str[] */
1156: leng = strlen( b->s_str ) + 1;
1157: b_str= capa_malloc(sizeof(char), leng);
1158: sprintf(b_str,"%s",b->s_str);
1159: /*if(b->s_type == S_CONSTANT) capa_mfree(b->s_str);*/
1160: break;
1161: }
1162: switch( op ) {
1163: case ADD_op:
1164: leng = strlen( a->s_str ) + strlen(b_str) + 1;
1165: r_strp = capa_malloc(sizeof(char), leng); /* **** */
1166: strcat(r_strp, a->s_str);
1167: strcat(r_strp, b_str); /* concatenate two strings together */
1168: if( !new ) capa_mfree(a->s_str);
1169: a_symp->s_str = r_strp;
1170: break;
1171: case SUB_op:
1172: case MUL_op:
1173: case DIV_op:
1174: case IDIV_op:
1175: if( !new ) capa_mfree(a->s_str);
1176: a_symp->s_str = strsave("<<Op NOT DEFINED>>");
1177: sprintf(warn_msg,"integer division (%%) cannot accept string operand!\n");
1178: capa_msg(MESSAGE_ERROR,warn_msg);
1179: break;
1180: case EQ_op: a_symp->s_int = (strcmp(a->s_str, b_str) == 0? 1: 0);
1181: type = I_CONSTANT; break;
1182: case NE_op: a_symp->s_int = (strcmp(a->s_str, b_str) == 0? 0: 1);
1183: type = I_CONSTANT; break;
1184: case GT_op: a_symp->s_int = (strcmp(a->s_str, b_str) > 0? 1: 0);
1185: type = I_CONSTANT; break;
1186: case GE_op: a_symp->s_int = (strcmp(a->s_str, b_str) >= 0? 1: 0);
1187: type = I_CONSTANT; break;
1188: case LT_op: a_symp->s_int = (strcmp(a->s_str, b_str) < 0? 1: 0);
1189: type = I_CONSTANT; break;
1190: case LE_op: a_symp->s_int = (strcmp(a->s_str, b_str) <= 0? 1: 0);
1191: type = I_CONSTANT; break;
1192: case AND_op: if( (a->s_str[0] != 0) && (b_str[0] != 0)) {
1193: a_symp->s_int = 1;
1194: } else {
1195: a_symp->s_int = 0;
1196: }
1197: type = I_CONSTANT; break;
1198: case OR_op: if( (a->s_str[0] != 0) || (b_str[0] != 0)) {
1199: a_symp->s_int = 1;
1200: } else {
1201: a_symp->s_int = 0;
1202: }
1203: type = I_CONSTANT; break;
1204: }
1205: if (b_str!=NULL) capa_mfree(b_str);
1206: } else { /* b is string and a is either integer or real */
1207: switch( a->s_type ) {
1208: case I_VAR:
1209: case I_CONSTANT:
1210: sprintf(a_str,"%ld", a->s_int); break;
1211: case R_VAR:
1212: case R_CONSTANT:
1213: sprintf(a_str,"%.15g", a->s_real); break;
1214: }
1215: switch( op ) {
1216: case ADD_op:
1217: leng = strlen( b->s_str ) + strlen(a_str) + 1;
1218: r_strp = capa_malloc(sizeof(char), leng); /* *** */
1219: strcat(r_strp, a_str);
1220: strcat(r_strp, b->s_str);
1221: /*if( b->s_type == S_CONSTANT ) capa_mfree(b->s_str);*/
1222: a_symp->s_str = r_strp; break;
1223: case SUB_op:
1224: case MUL_op:
1225: case DIV_op:
1226: case IDIV_op:
1227: a_symp->s_str = strsave("<<Op NOT DEFINED>>");
1228: sprintf(warn_msg,"integer division (%%) cannot accept string operand!\n");
1229: capa_msg(MESSAGE_ERROR,warn_msg);
1230: break;
1231: case EQ_op: a_symp->s_int = (strcmp(a_str, b->s_str) == 0? 1: 0);
1232: type = I_CONSTANT; break;
1233: case NE_op: a_symp->s_int = (strcmp(a_str, b->s_str) == 0? 0: 1);
1234: type = I_CONSTANT; break;
1235: case GT_op: a_symp->s_int = (strcmp(a_str, b->s_str) > 0? 1: 0);
1236: type = I_CONSTANT; break;
1237: case GE_op: a_symp->s_int = (strcmp(a_str, b->s_str) >= 0? 1: 0);
1238: type = I_CONSTANT; break;
1239: case LT_op: a_symp->s_int = (strcmp(a_str, b->s_str) < 0? 1: 0);
1240: type = I_CONSTANT; break;
1241: case LE_op: a_symp->s_int = (strcmp(a_str,b->s_str) <= 0? 1: 0);
1242: type = I_CONSTANT; break;
1243: case AND_op: if( (a_str[0] != 0) && (b->s_str[0] != 0)) {
1244: a_symp->s_int = 1;
1245: } else {
1246: a_symp->s_int = 0;
1247: }
1248: type = I_CONSTANT; break;
1249: case OR_op: if( (a_str[0] != 0) || (b_str[0] != 0)) {
1250: a_symp->s_int = 1;
1251: } else {
1252: a_symp->s_int = 0;
1253: }
1254: type = I_CONSTANT; break;
1255: }
1256: }
1257:
1258: } else { /* both a and b are real */
1259: type = R_CONSTANT;
1260: if( (a->s_type == R_CONSTANT)||(a->s_type == R_VAR) ) {
1261: tmp_p = a->s_real;
1262: } else {
1263: tmp_p = (double)a->s_int;
1264: }
1265: if( (b->s_type == R_CONSTANT)||(b->s_type == R_VAR) ) {
1266: tmp_q = b->s_real;
1267: } else {
1268: tmp_q = (double)b->s_int;
1269: }
1270: switch( op ) {
1271: case ADD_op: a_symp->s_real = tmp_p + tmp_q ; break;
1272: case SUB_op: a_symp->s_real = tmp_p - tmp_q ; break;
1273: case MUL_op: a_symp->s_real = tmp_p * tmp_q ; break;
1274: case DIV_op: if(tmp_q != 0.0) {
1275: a_symp->s_real = tmp_p / tmp_q ;
1276: } else {
1277: /* printf("FDIVISION by ZERO\n"); */
1278: sprintf(warn_msg,"division (/) by zero!\n");
1279: capa_msg(MESSAGE_ERROR,warn_msg);
1280: }
1281: break;
1282: case IDIV_op: if(tmp_q != 0.0 ) {
1283: tmp_i = (long)tmp_p;
1284: tmp_j = (long)tmp_q;
1285: a_symp->s_int = tmp_i % tmp_j;
1286: type = I_CONSTANT;
1287: } else {
1288: /* printf("DIVISION by ZERO\n"); */
1289: sprintf(warn_msg,"division (/) by zero!\n");
1290: capa_msg(MESSAGE_ERROR,warn_msg);
1291: }
1292: break;
1293: case EQ_op: type = I_CONSTANT;
1294: a_symp->s_int = ((tmp_p == tmp_q)? 1: 0); break;
1295: case NE_op: type = I_CONSTANT;
1296: a_symp->s_int = ((tmp_p == tmp_q)? 0: 1); break;
1297: case GT_op: type = I_CONSTANT;
1298: a_symp->s_int = ((tmp_p > tmp_q)? 1: 0); break;
1299: case GE_op: type = I_CONSTANT;
1300: a_symp->s_int = ((tmp_p >= tmp_q)? 1: 0); break;
1301: case LT_op: type = I_CONSTANT;
1302: a_symp->s_int = ((tmp_p < tmp_q)? 1: 0); break;
1303: case LE_op: type = I_CONSTANT;
1304: a_symp->s_int = ((tmp_p <= tmp_q)? 1: 0); break;
1305: case AND_op: type = I_CONSTANT;
1306: a_symp->s_int = ((tmp_p && tmp_q)? 1: 0); break;
1307: case OR_op: type = I_CONSTANT;
1308: a_symp->s_int = ((tmp_p || tmp_q)? 1: 0); break;
1309: }
1310: }
1311: }
1312: if( (b->s_type == I_CONSTANT) ||
1313: (b->s_type == R_CONSTANT) ) capa_mfree((char *)b); /* free symbol *b only */
1314: if( (b->s_type == S_CONSTANT) ) {
1315: capa_mfree((char *)b->s_str);
1316: capa_mfree((char *)b);
1317: }
1318: a_symp->s_type = type;
1319: return (a_symp);
1320: }
1321: }
1322:
1323: /* ------------------------------------------------------ */
1324: char *
1325: format_toTeX( real ) char *real;
1326: {
1327: int idx, length, fraclength, i_exp;
1328: char *expo_p, fracS[SMALL_LINE_BUFFER], result[ONE_K], *areal;
1329: char warn_msg[WARN_MSG_LENGTH];
1330:
1331: length = strlen(real);
1332: if( index( real, 'e' ) == NULL ) {
1333: if( index( real, 'E' ) == NULL ) {
1334: sprintf(result,"%s", real);
1335: } else {
1336: expo_p = index(real, 'E'); /*** hpux complained */
1337: fraclength = length - strlen(expo_p);
1338: expo_p++; if(expo_p[0] == '+') expo_p++;
1339: sscanf(expo_p,"%d",&i_exp);
1340: for(idx=0;idx<fraclength;idx++) fracS[idx] = real[idx];
1341: fracS[fraclength] = 0;
1342: if(i_exp == 0 ) {
1343: sprintf(result,"$%s$", fracS);
1344: } else {
1345: sprintf(result,"$%s \\times 10^{%d}$", fracS, i_exp);
1346: }
1347: }
1348: } else {
1349: if( index( real, 'E' ) == NULL ) {
1350: expo_p = index(real, 'e'); /*** hpux complained */
1351: fraclength = length - strlen(expo_p);
1352: expo_p++; if(expo_p[0] == '+') expo_p++;
1353: sscanf(expo_p,"%d",&i_exp);
1354: for(idx=0;idx<fraclength;idx++) fracS[idx] = real[idx];
1355: fracS[fraclength] = 0;
1356: if(i_exp == 0 ) {
1357: sprintf(result,"$%s$", fracS);
1358: } else {
1359: sprintf(result,"$%s \\times 10^{%d}$", fracS, i_exp);
1360: }
1361: } else {
1362: sprintf(result,"<<Ill-formed REAL>>");
1363: sprintf(warn_msg,"number %s is not a valid real number!\n",real);
1364: capa_msg(MESSAGE_ERROR,warn_msg);
1365: }
1366: }
1367: areal = (char *) capa_malloc(strlen(result)+1, 1);
1368: strcpy(areal,result);
1369: return (areal);
1370: }
1371: /* ------------------------------------------------------ */
1372: char *
1373: format_toHTML( real ) char *real;
1374: {
1375: int idx, length, fraclength, i_exp;
1376: char *expo_p, fracS[SMALL_LINE_BUFFER], result[ONE_K], *areal;
1377: char warn_msg[WARN_MSG_LENGTH];
1378:
1379: length = strlen(real);
1380: if( index( real, 'e' ) == NULL ) {
1381: if( index( real, 'E' ) == NULL ) {
1382: sprintf(result,"%s", real);
1383: } else {
1384: expo_p = index(real, 'E'); /*** hpux complained */
1385: fraclength = length - strlen(expo_p);
1386: expo_p++; if(expo_p[0] == '+') expo_p++;
1387: sscanf(expo_p,"%d",&i_exp);
1388: for(idx=0;idx<fraclength;idx++) fracS[idx] = real[idx];
1389: fracS[fraclength] = 0;
1390: if(i_exp == 0 ) {
1391: sprintf(result,"%s", fracS);
1392: } else {
1393: sprintf(result,"%s×10<sup>%d</sup>", fracS, i_exp); /* × is code for x */
1394: }
1395: }
1396: } else { /* the string contains 'e' char */
1397: if( index( real, 'E' ) == NULL ) {
1398: expo_p = index(real, 'e'); /*** hpux complained */
1399: fraclength = length - strlen(expo_p);
1400: expo_p++; if(expo_p[0] == '+') expo_p++;
1401: sscanf(expo_p,"%d",&i_exp);
1402: for(idx=0;idx<fraclength;idx++) fracS[idx] = real[idx];
1403: fracS[fraclength] = 0;
1404: if(i_exp == 0 ) {
1405: sprintf(result,"%s", fracS);
1406: } else {
1407: sprintf(result,"%s×10<sup>%d</sup>", fracS, i_exp); /* × is code for x */
1408: }
1409: } else {
1410: sprintf(result,"<<Ill-formed REAL>>");
1411: sprintf(warn_msg,"number %s is not a valid real number!\n",real);
1412: capa_msg(MESSAGE_ERROR,warn_msg);
1413: }
1414: }
1415: areal = (char *) capa_malloc(strlen(result)+1, 1);
1416: strcpy(areal,result);
1417: return (areal);
1418: }
1419: /* -- This routine is called when a /ANS is encountered -- */
1420: void
1421: init_answerinfo()
1422: {
1423:
1424: CurrAnsInfo.ans_str = NULL;
1425: CurrAnsInfo.ans_type = 0;
1426: CurrAnsInfo.ans_calc = CALC_DEFAULT;
1427: CurrAnsInfo.ans_tol_type = TOL_ABSOLUTE;
1428: CurrAnsInfo.ans_tol = TOL_DEFAULT;
1429: CurrAnsInfo.ans_sig_ub = SIG_UB_DEFAULT;
1430: CurrAnsInfo.ans_sig_lb = SIG_LB_DEFAULT;
1431: CurrAnsInfo.ans_id_list = NULL;
1432: CurrAnsInfo.ans_pts_list = NULL;
1433: CurrAnsInfo.ans_fmt[0] = '\0';
1434: CurrAnsInfo.ans_unit_str[0] = '\0';
1435: CurrAnsInfo.ans_unit = NULL;
1436: CurrAnsInfo.ans_next = NULL;
1437: }
1438: /* when encountered a /DIS(variable) */
1439: void
1440: display_var( s )Symbol *s;
1441: {
1442: char *aline;
1443: char *tmp_p;
1444: char warn_msg[WARN_MSG_LENGTH];
1445:
1446:
1447: switch(s->s_type) {
1448: case IDENTIFIER:
1449: aline = (char *)capa_malloc(sizeof(char)*ONE_K,1);
1450: sprintf(aline,"VAR \"%s\" NOT DEFINED!", s->s_name);
1451: sprintf(warn_msg,"display var \"%s\" not defined before use.\n",s->s_name);
1452: capa_msg(MESSAGE_ERROR,warn_msg);
1453: break;
1454: case I_VAR: case I_CONSTANT:
1455: aline = (char *)capa_malloc(sizeof(char)*ONE_K,1);
1456: sprintf(aline, "%ld", s->s_int);
1457: break;
1458: case R_VAR: case R_CONSTANT:
1459: aline = (char *)capa_malloc(sizeof(char)*ONE_K,1);
1460: if(Parsemode_f == TeX_MODE) {
1461: if(s->s_distype == DEFAULT_FORMAT ) {
1462: sprintf(aline,"%.15g",s->s_real);
1463: } else {
1464: sprintf(aline,s->s_format,s->s_real);
1465: }
1466: tmp_p = format_toTeX(aline);
1467: sprintf(aline,"%s",tmp_p);
1468: capa_mfree( (char *)tmp_p);
1469:
1470: } else {
1471: if(Parsemode_f == HTML_MODE ) {
1472: if(s->s_distype == DEFAULT_FORMAT ) {
1473: sprintf(aline,"%.15g",s->s_real);
1474: } else {
1475: sprintf(aline,s->s_format,s->s_real);
1476: }
1477: tmp_p = format_toHTML(aline);
1478: sprintf(aline,"%s",tmp_p);
1479: capa_mfree( (char *)tmp_p);
1480: } else {
1481: if(s->s_distype == DEFAULT_FORMAT ) {
1482: sprintf(aline,"%.15g",s->s_real);
1483: } else {
1484: sprintf(aline,s->s_format,s->s_real);
1485: }
1486: }
1487: }
1488: break;
1489: case S_VAR: case S_CONSTANT:
1490: aline = (char *)capa_malloc(strlen(s->s_str)+1,1);
1491: sprintf(aline,"%s",s->s_str);
1492: break;
1493: }
1494: append_text(aline);
1495: capa_mfree((char *)aline);
1496: if(s->s_format) {
1497: capa_mfree((char *)s->s_format);
1498: }
1499: s->s_format = NULL;
1500: switch(s->s_type) { /* free up spaces taken by constants */
1501: case I_CONSTANT:
1502: case R_CONSTANT: capa_mfree((char *)s); break;
1503: case S_CONSTANT: capa_mfree(s->s_str); capa_mfree((char *)s); break;
1504: default: break;
1505: }
1506:
1507: }
1508:
1509: /* Assign the correct answer to CurrAnsInfo first */
1510: void
1511: assign_answer( s ) Symbol *s;
1512: {
1513: char aline[QUARTER_K];
1514: char warn_msg[WARN_MSG_LENGTH];
1515:
1516: /*problem_default(LexiProblem_p);*/
1517: switch(s->s_type) {
1518: case IDENTIFIER:
1519: sprintf(warn_msg,"File %s, Line %3d: in /ANS, var %s not defined before use.\n",
1520: Opened_filename[Input_idx],Current_line[Input_idx],s->s_name);
1521: capa_msg(MESSAGE_ERROR,warn_msg);
1522: CurrAnsInfo.ans_str = strsave("ANSWER NOT DEFINED!");
1523: CurrAnsInfo.ans_type = ANSWER_IS_STRING_CI;
1524: sprintf(CurrAnsInfo.ans_fmt,"%%s");
1525: if (CurrAnsInfo.ans_tol == 0.0) {
1526: sprintf(warn_msg, "File %s, Line %3d: answer has a numerical value of %ld and an implicit zero tolerance.\n",
1527: Opened_filename[Input_idx],Current_line[Input_idx],s->s_int);
1528: capa_msg(MESSAGE_WARN,warn_msg);
1529: }
1530: break;
1531: case I_VAR: case I_CONSTANT:
1532: sprintf(aline, "%ld", s->s_int);
1533: CurrAnsInfo.ans_str = strsave(aline);
1534: CurrAnsInfo.ans_type = ANSWER_IS_INTEGER;
1535: sprintf(CurrAnsInfo.ans_fmt,"%%ld");
1536: break;
1537: case R_VAR: case R_CONSTANT:
1538: if(s->s_distype == DEFAULT_FORMAT ) {
1539: sprintf(aline,"%.15g",s->s_real);
1540: sprintf(CurrAnsInfo.ans_fmt,"%%.15g");
1541: } else {
1542: sprintf(aline,"%.15g",s->s_real);
1543: strcpy(CurrAnsInfo.ans_fmt,s->s_format);
1544: }
1545: CurrAnsInfo.ans_str = strsave(aline);
1546: CurrAnsInfo.ans_type = ANSWER_IS_FLOAT;
1547: if( CurrAnsInfo.ans_tol == 0.0 ) {
1548: sprintf(warn_msg,"File %s, Line %3d: answer has a numerical value of %s and a zero tolerance.\n",
1549: Opened_filename[Input_idx],Current_line[Input_idx],aline);
1550: capa_msg(MESSAGE_WARN,warn_msg);
1551: }
1552: break;
1553: case S_VAR: case S_CONSTANT:
1554: CurrAnsInfo.ans_str = strsave(s->s_str);
1555: if (s->s_str!=NULL && (strlen(s->s_str)>ANSWER_STRING_LENG-1)) {
1556: sprintf(warn_msg,"File %s, Line %3d: answer is too long, max allowed length is %d, current answer is %d\n",
1557: Opened_filename[Input_idx],Current_line[Input_idx],
1558: ANSWER_STRING_LENG-1, strlen(s->s_str));
1559: capa_msg(MESSAGE_ERROR,warn_msg);
1560: CurrAnsInfo.ans_str[ANSWER_STRING_LENG-1]='\0';
1561: }
1562: if ( !CurrAnsInfo.ans_type ) { /* not yet specified by str= answer info */
1563: CurrAnsInfo.ans_type = ANSWER_IS_STRING_CI;
1564: }
1565: sprintf(CurrAnsInfo.ans_fmt,"%%s");
1566: break;
1567: }
1568: if(s->s_format) {
1569: capa_mfree((char *)s->s_format);
1570: }
1571: s->s_format = NULL;
1572: switch(s->s_type) {
1573: case I_CONSTANT:
1574: case R_CONSTANT: capa_mfree((char *)s); break;
1575: case S_CONSTANT: capa_mfree(s->s_str); capa_mfree((char *)s); break;
1576: default: break;
1577: }
1578:
1579: }
1580:
1581: /* Assign tolerance to CurrAnsInfo first */
1582: void
1583: assign_tolerance(tol_type, s) int tol_type; Symbol *s;
1584: {
1585: char warn_msg[WARN_MSG_LENGTH];
1586:
1587: CurrAnsInfo.ans_tol_type = tol_type;
1588: switch( s->s_type ) {
1589: case IDENTIFIER:
1590: sprintf(warn_msg,"TOL = var, \"%s\" not defined before use.\n",s->s_name);
1591: capa_msg(MESSAGE_ERROR,warn_msg);
1592: CurrAnsInfo.ans_tol = 0.0;
1593: break;
1594: case I_VAR: case I_CONSTANT:
1595: CurrAnsInfo.ans_tol =(double)s->s_int;
1596: break;
1597: case R_VAR: case R_CONSTANT:
1598: CurrAnsInfo.ans_tol = s->s_real;
1599: break;
1600: case S_VAR: case S_CONSTANT: CurrAnsInfo.ans_tol = 0.0;
1601: break;
1602: }
1603: free_calc_expr(s);
1604: }
1605:
1606: /* Problem weight is per problem based */
1607: void
1608: assign_weight( s ) Symbol *s;
1609: { char warn_msg[WARN_MSG_LENGTH];
1610: YYDBUG_PR1(" weight = identifier\n");
1611: switch( s->s_type ) {
1612: case IDENTIFIER:
1613: sprintf(warn_msg,"WGT = var, \"%s\" not defined before use.\n", s->s_name);
1614: capa_msg(MESSAGE_ERROR,warn_msg);
1615: LexiProblem_p->weight = WEIGHT_DEFAULT;
1616: break;
1617: case I_VAR: case I_CONSTANT:
1618: if( s->s_int < 0 ) {
1619: sprintf(warn_msg,"WGT = %ld, weight cannot be less than zero.\n", s->s_int);
1620: capa_msg(MESSAGE_ERROR,warn_msg);
1621: LexiProblem_p->weight = WEIGHT_DEFAULT;
1622: } else {
1623: LexiProblem_p->weight = s->s_int;
1624: }
1625: break;
1626: case R_VAR: case R_CONSTANT:
1627: if( s->s_real < 0.0 ) {
1628: sprintf(warn_msg,"WGT = %g, weight cannot be less than zero.\n", s->s_real);
1629: capa_msg(MESSAGE_ERROR,warn_msg);
1630: LexiProblem_p->weight = WEIGHT_DEFAULT;
1631: } else {
1632: LexiProblem_p->weight = (int)(s->s_real);
1633: }
1634: break;
1635: case S_VAR: case S_CONSTANT: LexiProblem_p->weight = WEIGHT_DEFAULT; break;
1636: }
1637: free_calc_expr(s);
1638: }
1639: /* Answer try limit is per problem based */
1640: void
1641: assign_try_limits( s ) Symbol *s;
1642: { char warn_msg[WARN_MSG_LENGTH];
1643: switch( s->s_type ) {
1644: case IDENTIFIER:
1645: sprintf(warn_msg,"TRY = var, \"%s\" not defined before use.\n",s->s_name);
1646: capa_msg(MESSAGE_ERROR,warn_msg);
1647: LexiProblem_p->tries = MAX_TRIES;
1648: break;
1649: case I_VAR: case I_CONSTANT:
1650: if(s->s_int <= 0) {
1651: sprintf(warn_msg,"TRIES = %ld, tries cannot be less than or equal to zero.\n",s->s_int);
1652: capa_msg(MESSAGE_ERROR,warn_msg);
1653: LexiProblem_p->tries = MAX_TRIES;
1654: } else {
1655: LexiProblem_p->tries = s->s_int;
1656: }
1657: break;
1658: case R_VAR: case R_CONSTANT:
1659: if(s->s_real <= 0.0) {
1660: sprintf(warn_msg,"TRIES = %g, tries cannot be less than or equal to zero.\n",s->s_real);
1661: capa_msg(MESSAGE_ERROR,warn_msg);
1662: LexiProblem_p->tries = MAX_TRIES;
1663: } else {
1664: LexiProblem_p->tries = (int)(s->s_real);
1665: }
1666: break;
1667: case S_VAR: case S_CONSTANT: LexiProblem_p->tries = MAX_TRIES; break;
1668: }
1669: free_calc_expr(s);
1670: }
1671: /* Answer hint is per problem based */
1672: void
1673: assign_hint( s ) Symbol *s;
1674: { char warn_msg[WARN_MSG_LENGTH];
1675:
1676: switch( s->s_type ) {
1677: case IDENTIFIER:
1678: sprintf(warn_msg,"HINT = var, \"%s\" not defined before use.\n", s->s_name);
1679: capa_msg(MESSAGE_ERROR,warn_msg);
1680: LexiProblem_p->show_hint = SHOW_HINT_DEFAULT;
1681: break;
1682: case I_VAR: case I_CONSTANT:
1683: if( s->s_int < 0 ) {
1684: sprintf(warn_msg,"HINT = %ld, show hint cannot be less than zero.\n", s->s_int);
1685: capa_msg(MESSAGE_ERROR,warn_msg);
1686: LexiProblem_p->show_hint = SHOW_HINT_DEFAULT;
1687: } else {
1688: LexiProblem_p->show_hint = s->s_int;
1689: }
1690: break;
1691: case R_VAR: case R_CONSTANT:
1692: if( s->s_real < 0.0 ) {
1693: sprintf(warn_msg,"HINT = %g, show hint cannot be less than zero.\n", s->s_real);
1694: capa_msg(MESSAGE_ERROR,warn_msg);
1695: LexiProblem_p->show_hint = SHOW_HINT_DEFAULT;
1696: } else {
1697: LexiProblem_p->weight = (int)(s->s_real);
1698: }
1699: break;
1700: case S_VAR: case S_CONSTANT: LexiProblem_p->show_hint = SHOW_HINT_DEFAULT; break;
1701: }
1702: free_calc_expr(s);
1703: }
1704:
1705: /* Assign answer units string to CurrAnsInfo first */
1706: void
1707: assign_units( s ) Symbol *s;
1708: {
1709: char symb_str[ONE_TWO_EIGHT];
1710: char warn_msg[WARN_MSG_LENGTH];
1711:
1712: switch( s->s_type ) {
1713: case IDENTIFIER:
1714: sprintf(warn_msg,"UNIT = var, \"%s\" not defined before use.\n", s->s_name);
1715: capa_msg(MESSAGE_ERROR,warn_msg);
1716: break;
1717: case I_VAR: case I_CONSTANT:
1718: sprintf(warn_msg,"UNIT = %ld, unit cannot be a number.\n", s->s_int);
1719: capa_msg(MESSAGE_ERROR,warn_msg);
1720: break;
1721: case R_VAR: case R_CONSTANT:
1722: sprintf(warn_msg,"UNIT = %g, unit cannot be a number.\n", s->s_real);
1723: capa_msg(MESSAGE_ERROR,warn_msg);
1724: break;
1725: case S_VAR: case S_CONSTANT:
1726: strcpy(symb_str,s->s_str);
1727: strcpy(CurrAnsInfo.ans_unit_str,symb_str);
1728: CurrAnsInfo.ans_unit = u_parse_unit(symb_str);
1729: if (gUnitError) {
1730: sprintf(warn_msg,"Error in unit specified: %s\n",symb_str);
1731: capa_msg(MESSAGE_ERROR,warn_msg);
1732: }
1733: break;
1734: }
1735: }
1736: void
1737: assign_sigs( lb, ub ) int lb; int ub;
1738: {
1739: CurrAnsInfo.ans_sig_lb = lb;
1740: CurrAnsInfo.ans_sig_ub = ub;
1741: }
1742:
1743: void
1744: assign_id_list( s ) Symbol *s;
1745: {
1746: char warn_msg[WARN_MSG_LENGTH];
1747:
1748: switch( s->s_type ) {
1749: case IDENTIFIER:
1750: sprintf(warn_msg,"Eval = < ID @ Pts >, \"%s\" not defined before use.\n", s->s_name);
1751: capa_msg(MESSAGE_ERROR,warn_msg);
1752: break;
1753: case I_VAR: case I_CONSTANT:
1754: sprintf(warn_msg,"Eval = < %ld @ Pts >, ID cannot be a number.\n", s->s_int);
1755: capa_msg(MESSAGE_ERROR,warn_msg);
1756: break;
1757: case R_VAR: case R_CONSTANT:
1758: sprintf(warn_msg,"Eval = < %.16g @ Pts >, ID cannot be a number.\n", s->s_real);
1759: capa_msg(MESSAGE_ERROR,warn_msg);
1760: break;
1761: case S_VAR: case S_CONSTANT:
1762: CurrAnsInfo.ans_id_list = strsave(s->s_str);
1763: break;
1764: }
1765: }
1766:
1767: /* =========================================================================== */
1768:
1769:
1770:
1771:
1772:
1773:
1774: /* =========================================================================== */
1775:
1776: void start_question_over()
1777: {
1778: free_problems(LexiProblem_p);
1779: LexiProblem_p = (Problem_t *) capa_malloc(sizeof(Problem_t),1);
1780: problem_default(LexiProblem_p);
1781: begin_question();
1782: }
1783:
1784: void
1785: init_new_prob()
1786: {
1787: if (LastProblem_p) {
1788: LastProblem_p->next = LexiProblem_p;
1789: } else {
1790: FirstProblem_p = LexiProblem_p;
1791: }
1792: LastProblem_p = LexiProblem_p;
1793: Lexi_qnum++;
1794: LexiProblem_p = (Problem_t *) capa_malloc(sizeof(Problem_t),1); /* *** */
1795: problem_default(LexiProblem_p);
1796: }
1797: void
1798: add_answer_cnt(op)int op;
1799: {
1800: LexiProblem_p->ans_cnt++;
1801: if(LexiProblem_p->ans_op == 0) { /* the very first /AND or /OR */
1802: LexiProblem_p->ans_op = op;
1803: } else {
1804: if( LexiProblem_p->ans_op != op ) { char warn_msg[WARN_MSG_LENGTH];
1805: sprintf(warn_msg,"When specifying multiple answers, either /AND or /OR can be used, but not both.\n");
1806: capa_msg(MESSAGE_ERROR,warn_msg);
1807: }
1808: }
1809: }
1810:
1811: /* -- called when forming answer_expr */
1812: void
1813: finish_answer_info()
1814: {
1815: AnswerInfo_t *ai;
1816:
1817: if( LexiProblem_p->ans_cnt == 1 ) { /* Only one answer is defined */
1818: LexiProblem_p->answer = CurrAnsInfo.ans_str;
1819: LexiProblem_p->ans_type = CurrAnsInfo.ans_type;
1820: LexiProblem_p->calc = CurrAnsInfo.ans_calc;
1821: LexiProblem_p->tol_type = CurrAnsInfo.ans_tol_type;
1822: LexiProblem_p->tolerance = CurrAnsInfo.ans_tol;
1823: LexiProblem_p->sig_ubound= CurrAnsInfo.ans_sig_ub;
1824: LexiProblem_p->sig_lbound= CurrAnsInfo.ans_sig_lb;
1825: LexiProblem_p->id_list = CurrAnsInfo.ans_id_list;
1826: LexiProblem_p->pts_list = CurrAnsInfo.ans_pts_list;
1827: strcpy(LexiProblem_p->ans_fmt,CurrAnsInfo.ans_fmt);
1828: strcpy(LexiProblem_p->unit_str,CurrAnsInfo.ans_unit_str);
1829: LexiProblem_p->ans_unit = CurrAnsInfo.ans_unit;
1830: } else {
1831: ai = (AnswerInfo_t *)capa_malloc(sizeof(AnswerInfo_t),1);
1832: ai->ans_str = CurrAnsInfo.ans_str;
1833: ai->ans_type = CurrAnsInfo.ans_type;
1834: ai->ans_calc = CurrAnsInfo.ans_calc;
1835: ai->ans_tol_type = CurrAnsInfo.ans_tol_type;
1836: ai->ans_tol = CurrAnsInfo.ans_tol;
1837: ai->ans_sig_ub = CurrAnsInfo.ans_sig_ub;
1838: ai->ans_sig_lb = CurrAnsInfo.ans_sig_lb;
1839: ai->ans_id_list = CurrAnsInfo.ans_id_list;
1840: ai->ans_pts_list = CurrAnsInfo.ans_pts_list;
1841: strcpy(ai->ans_fmt,CurrAnsInfo.ans_fmt);
1842: strcpy(ai->ans_unit_str,CurrAnsInfo.ans_unit_str);
1843: ai->ans_unit = CurrAnsInfo.ans_unit;
1844: ai->ans_next = NULL;
1845: if(LexiProblem_p->ans_cnt == 2) {
1846: LexiProblem_p->ans_list = ai;
1847: } else {
1848: (LexiProblem_p->ans_list_last)->ans_next = ai;
1849: }
1850: LexiProblem_p->ans_list_last = ai;
1851: }
1852:
1853: }
1854:
1855: /* === End of capaGrammarDef.y ============================================= */
1856:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>