--- loncom/homework/CAPA-converter/capaLexerDef.flex 2001/11/18 09:49:43 1.10 +++ loncom/homework/CAPA-converter/capaLexerDef.flex 2002/09/12 15:54:06 1.21 @@ -1,3 +1,30 @@ +/* The LearningOnline Network with CAPA + * CAPA lexer dfinition, heavily modified to become a LON-CAPA convertor + * $Id: capaLexerDef.flex,v 1.21 2002/09/12 15:54:06 albertel Exp $ + * + * Copyright Michigan State University Board of Trustees + * + * This file is part of the LearningOnline Network with CAPA (LON-CAPA). + * + * LON-CAPA 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. + * + * LON-CAPA 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 LON-CAPA; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * /home/httpd/html/adm/gpl.txt + * + * http://www.lon-capa.org/ + */ + /*------------------------------------------------------------------------*/ /* capaLexerDef.flex created by Isaac Tsai Jul 15 1996 */ /* added /END(variable) */ @@ -123,6 +150,11 @@ int IFcount; WhileLoop_t WhileStack[MAX_FUNC_NEST]; /* <-- perhaps we can use linked list */ int While_idx, Wcount; int sccount; /* Semi-colon count for MAP and RMAP */ +int HINTflag=0; +int EXPflag=0; +int EVALflag=0; +int compound_answer=0; +int essay_answer=0; #ifdef USE_DYNAMIC_SYMBOLS Symbol *SymbList_p; @@ -215,7 +247,7 @@ void newyy_input (char *buf,int *result, } \ Input_idx = 0; \ first_run=0; \ - yyin = Input_stream[Input_idx]; LIDBUG_PR1("<>\n"); \ +yyin = Input_stream[Input_idx]; LIDBUG_PR1("<>\n"); \ } \ out_of_char = 0; \ if ( Lexi_buf[Input_idx] == NULL ) { \ @@ -400,10 +432,22 @@ EndLine ([\r][\n]|[\n]) send("
\n");
                                  }
 ^{Spaces}"/HIN"{Alpha}*{Spaces}  { LLDBUG_PRL1("[HIN]"); 
-                                   start_mode(MODE_HINT, "");
+                               /*    start_mode(MODE_HINT, "");*/
+                                   if (!HINTflag) {
+                                     start_streams(HINT_DEST,1);
+                                     HINTflag=-1;
+                                   }
+                                   change_destination(HINT_DEST);
+                                   send("\t",NULL);
 				   BEGIN S_HINT; 
                                  }
-^{Spaces}"/EXP"{Alpha}*{Spaces}  { start_mode(MODE_BLOCK,"condition=&explanation");
+^{Spaces}"/EXP"{Alpha}*{Spaces}  { 
+                                   if (!EXPflag) {
+                                     start_streams(EXP_DEST,1);
+                                     EXPflag=-1;
+                                   }
+                                   change_destination(EXP_DEST);
+                                   send("\t",NULL);
                                    LLDBUG_PRL1("[EXP]"); Current_char_p = String_buf;  BEGIN S_EXPLAIN; }
 ^{Spaces}"/IMP"{Alpha}*{Space}+  { LLDBUG_PRL1("[IMP]"); BEGIN S_IMPORT; end_mode(); }
 ^{Spaces}"/END"                  { LLDBUG_PRL1("[END]");  
@@ -437,63 +481,71 @@ EndLine    ([\r][\n]|[\n])
                                 send("]\n",While_idx);
-                                
-                                top_item = While_idx - 1;
-                                if( top_item < 0 ) { /* strange things must have happened here! */
-                                  
-                                } else {
-                                  input_idx = WhileStack[top_item].input_idx;
-                                  file_pos = WhileStack[top_item].pos_idx;
-                                  Current_line[input_idx] = WhileStack[top_item].line_idx;
-                                  Lexi_pos[input_idx] = 0;
-                                  fseek(Input_stream[input_idx],file_pos,SEEK_SET);
-                                  fgets(Lexi_buf[input_idx],LEX_BUFLEN-1,Input_stream[input_idx]);
-                                  While_idx--;
-                                }
+^{Spaces}"/ENDWHILE"([^\n])*          { 
+                                int i;
+                                LLDBUG_PRL2("[ENDWHILE While_idx=<%d>]\n",IFcount);
+                                IFcount--;
+				end_mode_stream(DEFAULT_DEST,0);
+				for(i=0;i");
+				send_stream(1,"}");
+				if (IFcount == 0) {
+				  if (watch_mode[current_dest][1]) {
+				    end_streams(DEFAULT_DEST,1);
+ 				  } else {
+				    end_streams(DEFAULT_DEST,0);
+				  }
+                                  change_destination(DEFAULT_DEST);
+				}
+				delete_cache();
                                 BEGIN S_TEXT;
                               }
 "/WHILE"                      |
-^{Spaces}"/WHILE"             { long int file_pos;
-                                int      leng;
-                                LLDBUG_PRL2("[WHILE While_idx=<%d>]\n",While_idx);
-                                leng = strlen(Lexi_buf[Input_idx]);  /* length of current line */
-                                /* <-- because we use fgets() to read input, 
-                                       thus ftell() will give the starting position of next line */
-                                WhileStack[While_idx].input_idx = Input_idx;
-                                file_pos = ftell(Input_stream[Input_idx]);
-                                file_pos -= leng;  /* calibrate the current line length */
-                                WhileStack[While_idx].pos_idx = file_pos;  /* begin of current line */
-                                WhileStack[While_idx].line_idx = Current_line[Input_idx];
-                                While_idx++;  /* advance the stack pointer */
-                                
-                                BEGIN S_TRUE_FALSE_STMT; RETURN(CAPA_WHILE); 
-                                
+^{Spaces}"/WHILE"             { 
+                                int      i;
+                                LLDBUG_PRL2("[WHILE While_idx=<%d>]\n",IFcount);
+                                IFcount++;  /* advance the stack pointer */
+                                BEGIN S_TRUE_FALSE_STMT;
+				if ( IFcount == 1) {
+ 				  start_streams(DEFAULT_DEST, 2);
+                                  change_destination(DEFAULT_DEST);
+				  watch_mode[current_dest][1]=1;
+				}
+				end_mode_stream(DEFAULT_DEST, 0);
+				start_mode_stream(DEFAULT_DEST,1,MODE_SCRIPT,NULL);
+				for(i=1;i]",IFcount);
 				BEGIN S_TRUE_FALSE_STMT;
 				if ( IFcount == 1) {
- 				  start_streams(2);
-				  watch_mode[1]=1;
+ 				  start_streams(DEFAULT_DEST, 2);
+                                  change_destination(DEFAULT_DEST);
+				  watch_mode[current_dest][1]=1;
 				}
-				end_mode_stream(0);
-				start_mode_stream(1,MODE_SCRIPT,NULL);
+				end_mode_stream(DEFAULT_DEST, 0);
+				start_mode_stream(DEFAULT_DEST,1,MODE_SCRIPT,NULL);
 				for(i=1;i]\n",IFcount);
-				end_mode_stream(0);
+				end_mode_stream(DEFAULT_DEST,0);
 				for(i=1;i\n",
@@ -511,29 +563,30 @@ EndLine    ([\r][\n]|[\n])
                               }
 ^{Spaces}"/ENDIF"([^\n])*     { int i;
 				IFcount--;
-				end_mode_stream(0);
+				end_mode_stream(DEFAULT_DEST,0);
 				for(i=0;i");
 				send_stream(1,"}");
 				if (IFcount == 0) {
-				  if (watch_mode[1]) {
-				    end_streams(1);
+				  if (watch_mode[current_dest][1]) {
+				    end_streams(DEFAULT_DEST,1);
  				  } else {
-				    end_streams(0);
+				    end_streams(DEFAULT_DEST,0);
 				  }
+                                  change_destination(DEFAULT_DEST);
 				}
 				delete_cache();
 				LLDBUG_PRL2("[ENDIF ]\n",IFcount); 
                               }
-"/AND"                        { LLDBUG_PRL1("[AND]"); /*implict in LON-CAPA*/}
 "/DIS"                        { /* since S_VARIABLE treat {Space} as null, so here we do not match ( */
                                 /* so that between /DIS and ( can have as many {Space} as we want */
+				start_mode(MODE_OUTTEXT,NULL);
                                 LLDBUG_PR1("[DIS<]");
                                 init_funcstack();
                                 Pcount = 0; BEGIN S_VARIABLE; 
+				send("");
 				start_delayed();
                               }
-"/OR"                         { LLDBUG_PRL1("[OR]"); BEGIN S_ANSCONTINUE;  RETURN(ANS_OR); }
 {EndLine}                     { LLDBUG_PR1("[EoL within S_TEXT]\n"); /* end of the whole text line */ 
                                 send("\n"); }
 [\\]{Space}*{EndLine}         { LLDBUG_PR2("[\\EoL continue](%s)",yytext); /* continuation on next line */ }                       
@@ -663,7 +716,7 @@ EndLine    ([\r][\n]|[\n])
                                 }
 [/]                            { send("/"); }
 [\\]                           { send("\\"); }
-[\\]{Space}*[\n]               { LLDBUG_PR1("[\\CR hint explain continue]"); /* Hint and explain continuation */ }
+[\\]{Space}*[\n]               { LLDBUG_PR1("[\\CR hint explain continue]"); /* Hint and explain continuation */ send("\n\t");}
 [^/\n\\]+$                     {char  *aptr = yytext;
                                 send(aptr);
                                }
@@ -671,12 +724,14 @@ EndLine    ([\r][\n]|[\n])
 {
 {EndLine}                     {  LLDBUG_PR1("[CR hint]");
                                  send("\n"); 
+                                 change_destination(DEFAULT_DEST);
                                  BEGIN S_TEXT;
                               }
 }
 {
 {EndLine}                     {  LLDBUG_PR1("[CR explain]");
                                  send("\n"); 
+                                 change_destination(DEFAULT_DEST);
                                  BEGIN S_TEXT;
                               }
 }
@@ -689,17 +744,25 @@ EndLine    ([\r][\n]|[\n])
 
 {
 {FileName}{Space}*             {
+                                 char *endquote;
                                  end_mode();
 				 start_mode(MODE_IMPORT,NULL);
-                                 send("%s/%s",import_prefix,yytext);
-			         end_mode();
-                                 BEGIN S_SKIP; 
+				 /* Get rid of leading and trailing quotes */
+				 endquote = strrchr(yytext,'\"');
+                                 *endquote = '\0';
+                                 if (yytext[1] == '/') {
+                                    send("%s%s",import_prefix,&yytext[1]);
+                                 } else {
+                                    send("%s",&yytext[1]);
+                                 }
+				 end_mode();
+				 BEGIN S_SKIP;
                                }
 {Identifier}{Space}*           { end_mode();
 				 start_mode(MODE_IMPORT,NULL);
-				 send("$%s",yytext);
+				 send("${%s}",yytext);
 				 end_mode();
-				 BEGIN S_SKIP;
+                                 BEGIN S_SKIP;
 			       }
 }
 
@@ -729,12 +792,18 @@ EndLine    ([\r][\n]|[\n])
                           if (dosend==2) add_delayed("%s",yytext);
                         }
 "<"                          { LLDBUG_PR2("[symbol(%s)]",yytext);
+			       if (!EVALflag) {
                                if (dosend==1) send("%s",yytext);
                                if (dosend==2) add_delayed("%s",yytext);
                              }
+                             }
 ">"                          { LLDBUG_PR2("[symbol(%s)]",yytext);
+			       if (!EVALflag) {
                                if (dosend==1) send("%s",yytext);
                                if (dosend==2) add_delayed("%s",yytext);
+                               } else {
+				 EVALflag = 0;
+			       }
                              }
 
 [Pp][Cc][Rr]                 |
@@ -762,15 +831,15 @@ EndLine    ([\r][\n]|[\n])
 			       } else {
 				 add_delayed("\" />\n\t");
 			       }
-			       add_delayed("\n\t");
+                               } else {
                                send("\">\n\t");
+                               }
 			       dosend=1;
 			       flush_delayed();
 			       if (firstparam!=1) send("\" />\n");
-			       send("\t\n\n");
+                               if (essay_answer) {
+			         send("  Enter your answer here.\n");
+                               } else {
+			       send("\t\n");
+                               }
+                         /* Fill in Hints */ 
+			       if ( !is_dest_empty(HINT_DEST) ) {
+			         send("\t\n\t\n\t");
+                                 end_streams(HINT_DEST,0);
+                                 HINTflag=0;
+                                 send("\t\n\t\n\t\n");
+			       }
+                               if (essay_answer) {
+			         send("\n");
+                               } else {
+			         send("\n");
+			       }
+
+                             }
+}
+
+{
+{Alpha}{AlphaNum}*             { LLDBUG_PR2("[ID<%s>]",yytext);
+                                 LLDBUG_PR2("[SYMB CNT=<%d>]", Symb_count); 
+                                 if(Pcount <= 1) {
+                                   if (dosend==1) send("${%s}",yytext); 
+                                   if (dosend==2) add_delayed("${%s}",yytext); 
+                                 } else {
+                                   if (dosend==1) send("$%s",yytext); 
+                                   if (dosend==2) add_delayed("$%s",yytext); 
+                               }
                              }
 }
 
@@ -808,22 +911,11 @@ EndLine    ([\r][\n]|[\n])
                                  if (dosend==2) add_delayed("&%s",yytext);
 				 Pcount++;
                                }
-{Alpha}{AlphaNum}*{Space}*[\[]  {  char aline[MAX_FUNC_NAME];
-                                   int  i;   
-                                   for(i=0;i < (yyleng-1); i++) {
-                                     if( yytext[i] == ' ' || yytext[i] == '\t' || 
-                                         yytext[i] == 0   || yytext[i] == '[' )    break;
-                                     aline[i] = yytext[i];
-                                   }
-                                   aline[i] = 0;
-                                   LLDBUG_PR2("[ARRAY<%s>]",aline);
-                                   
-                                   yylval = (Symbol *) capa_malloc(1, sizeof(Symbol)); /* *** */
-                                   yylval->s_name = strsave(aline); /* free it in parser() */
-                                   yylval->s_type = ARRAY_ID;
-                                   
-                                   yyless(yyleng-1); /* <-- push back char '[' */
-                                   RETURN(ARRAY_ID);
+{Alpha}{AlphaNum}*{Space}*[\[]  {
+                                   LLDBUG_PR2("[ARRAY<%s>]",yytext);
+                                   yyless(yyleng-1); /*<-- push back char '[' */
+                                   if (dosend==1) send("$%s",yytext); 
+                                   if (dosend==2) add_delayed("$%s",yytext); 
                                }
 {Number}*"\."{Number}*[Ee]"+"{Number}+ |
 {Number}*"\."{Number}*[Ee]{Number}+    |
@@ -841,8 +933,14 @@ EndLine    ([\r][\n]|[\n])
                              if (dosend==1) send("%s",yytext);
                              if (dosend==2) add_delayed("%s",yytext);
 			  }
-[\[]                      { LLDBUG_PR1("[dis let ans map '[']");         return(yytext[0]); }
-[\]]                      { LLDBUG_PR1("[dis let ans map ']']");         return(yytext[0]); }
+[\[]                      { LLDBUG_PR1("[dis let ans map '[']");
+			    if(dosend==1) send("%s",yytext);
+			    if(dosend==2) add_delayed("%s",yytext);
+			  }
+[\]]                      { LLDBUG_PR1("[dis let ans map ']']");
+			    if(dosend==1) send("%s",yytext);
+			    if(dosend==2) add_delayed("%s",yytext);
+			  }
 {Space}+                  { /* LLDBUG_PR1("[SP ignored]");  Ignore Spaces */ }
 }
 
@@ -916,9 +1014,13 @@ EndLine    ([\r][\n]|[\n])
       }
 }
 
+{
+"+"		    { send(".");}
+}
+
 {
-"=="                { LLDBUG_PR1("[==]"); send(yytext);  }
-"!="                { LLDBUG_PR1("[!=]"); send(yytext);  }
+"=="                { LLDBUG_PR1("[==]"); send(" eq ");  }
+"!="                { LLDBUG_PR1("[!=]"); send(" ne ");  }
 ">"                 { LLDBUG_PR1("[>]");  send(yytext);  }
 ">="                { LLDBUG_PR1("[>=]"); send(yytext);  }
 "<"                 { LLDBUG_PR1("[<]");  send(yytext);  }
@@ -930,6 +1032,7 @@ EndLine    ([\r][\n]|[\n])
 			 BEGIN S_ECHO;
                       }
                     }
+[%]                 {send("%%");}
 {Operator}          { LLDBUG_PR2("[Op(%c) in VAR,TF_STMT,LET]",yytext[0]); send(yytext); }
 }
 
@@ -939,6 +1042,7 @@ EndLine    ([\r][\n]|[\n])
 [\)]                     { LLDBUG_PR1("[)]"); 
                            Pcount--; 
                            if(Pcount == 0) {
+			      send("");
                               BEGIN S_TEXT; 
                               flush_delayed();
                            } else {
@@ -962,6 +1066,9 @@ EndLine    ([\r][\n]|[\n])
 				send_stream(0,"\">\n");
 				send_stream(1,") {\n");
 				BEGIN S_NEXT_LINE;
+			   } else {
+                                send_stream(0,")");
+                                send_stream(1,")");
 			   }
 			 }
 [\\]{Space}*{EndLine}    { 
@@ -988,6 +1095,12 @@ EndLine    ([\r][\n]|[\n])
                       send("'");
 		      yy_pop_state();
 		    }
+[%]                 { /*Escape percent signs so that vasprintf doesn't choke */
+                      send("%%");
+                    }
+[\']                { /* Escape single quotes so that perl doesn't choke */
+                      send("\\\'");
+                    }
 {EndLine}           { /* check for termination of string constant */
                       char warn_msg[WARN_MSG_LENGTH];
                       
@@ -1050,12 +1163,45 @@ EndLine    ([\r][\n]|[\n])
 }
 
 {
-{Space}+                 { /* ignore white spaces */ }
-[\\]{Space}*{EndLine}    { /* continuation */ }
-{EndLine}                { /* end of answer and/or other answers */ LLDBUG_PR1("[complete an answer]"); 
-                           BEGIN S_TEXT; }
-"/AND"                   { LLDBUG_PR1("[AND]"); /* implicit in LON-CAPA */ }
-"/OR"                    { LLDBUG_PR1("[OR]");  RETURN(ANS_OR);  }
+{Spaces}{EndLine}{Spaces}"/AND"  { LLDBUG_PR1("[AND]");
+                                   compound_answer=-1;
+                                   /* implicit in LON-CAPA */ }
+{Spaces}{EndLine}{Spaces}"/OR"   { LLDBUG_PR1("[OR]"); 
+                                   compound_answer=-1;
+				   /*RETURN(ANS_OR); */ }
+{Spaces}{EndLine}{Spaces}"/ANS" { LLDBUG_PRL2("[ANS(%s)]",yytext); 
+                                Pcount = 0; 
+                                BEGIN S_ANSWER; 
+                                end_mode();
+                                if (!compound_answer) {
+			          if ( !is_dest_empty(EXP_DEST) ) {
+			            send("\n\t\n");
+                                    end_streams(EXP_DEST,0);
+                                    EXPflag=0;
+                                    send("\t\n\n");
+                                    }
+                                  send("\n\n"); 
+                                }
+				start_mode(MODE_ANSWER,NULL);
+                                send("]"); 
+			   if ( !is_dest_empty(EXP_DEST) ) {
+			     send("\n\t\n");
+                             end_streams(EXP_DEST,0);
+                             EXPflag=0;
+                             send("\t\n\n");
+                           }
+                           send("\n\n"); 
+                           BEGIN S_TEXT; 
+                         }
+{Spaces}		{ /* Do nothing */ }
 }
 
 {