--- loncom/homework/CAPA-converter/capaLexerDef.flex 2000/05/03 15:35:11 1.2 +++ 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) */ @@ -119,11 +146,15 @@ Symbol *FmlSymbLast_p; int FmlSymb_cnt; int Symb_count; -int IFstatus[MAX_FUNC_NEST]; /* <-- perhaps we can use linked list */ -int IFcurrent[MAX_FUNC_NEST]; /* <-- perhaps we can use linked list */ 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; @@ -140,7 +171,8 @@ extern Problem_t *LexiPro extern Problem_t *LastProblem_p; int first_run=1; int Stop_Parser; -int scriptopen=0; +static int dosend=1; +static int firstparam=1; #define FLEX #define YY_STACK_USED 1 /* for yy_push_state(), yy_pop_state() */ @@ -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 ) { \ @@ -337,6 +369,7 @@ void newyy_input (char *buf,int *result, #endif int capa_eof(); + %} Alpha [a-zA-Z_] @@ -359,8 +392,8 @@ EndLine ([\r][\n]|[\n]) %n 1000 %x S_COMMENT S_HINT S_HINTEXLAINX S_IMPORT S_EXPLAIN S_ENDX S_UNIT S_IGNORE %x S_SKIP S_VARIABLE S_LET S_DEFINE S_TEXT S_MAP S_FIGURE S_ANSWER -%x S_STRING S_ANSCONTINUE S_TRUE_FALSE_STMT S_IF_SKIP S_WHILE_SKIP -%x S_NEXT_LINE S_VERB +%x S_STRING S_ANSCONTINUE S_TRUE_FALSE_STMT S_WHILE_SKIP +%x S_NEXT_LINE S_VERB S_ECHO S_STRINGINANS %array @@ -379,33 +412,44 @@ EndLine ([\r][\n]|[\n]) } { -{EndLine}{Spaces}"//"[^\n]*$ {LLDBUG_PRL2("[COMMENT<%s>]\n",yytext); - remove_delayed(); - send("%s",yytext); - send_delayed("\n\n"); +{EndLine}{Spaces}"//"[^\n]*$ {LLDBUG_PRL2("[COMMENT<%s>]\n",yytext); + send("# %s\n",&yytext[2]); } -[^\n]*{EndLine} BEGIN S_TEXT; +[^\n]*{EndLine} { + send("\n"); BEGIN S_TEXT; + } } { ^{Spaces}"/LET" | - ^{Spaces}"/BEG" { LLDBUG_PRL1("[LET]"); Pcount = 0; BEGIN S_LET; if (!scriptopen){send(""); } +{ +[^\n]+$ { send(yytext); } +{EndLine} { send(yytext); BEGIN S_TEXT; } } -{ -[;,] { LLDBUG_PR2("[%c]",yytext[0]); return(yytext[0]); } -[\)] { LLDBUG_PR1("[) in MAP]"); Pcount--; - if(Pcount==0) { - BEGIN S_SKIP; - } - return(yytext[0]); +{ +[\\]{Space}*{EndLine} { LLDBUG_PR1("[\\EoL let ans map]"); /* continuation */ } +{EndLine} { LLDBUG_PR1("[EoL END let ans map]\n"); + if(Pcount == 0) BEGIN S_TEXT; + send(";%s",yytext); } } { -{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]"); RETURN(ANS_AND); } -"/OR" { LLDBUG_PR1("[OR]"); RETURN(ANS_OR); } -} - -{ -^{Spaces}"/IF"[^\n]*{EndLine} { IFcount++; LLDBUG_PRL2("[Skip IF ]\n",IFcount); - IFstatus[IFcount] = IF_DONT_CARE; - } -^{Spaces}"/ELSE"[^\n]*{EndLine} { LLDBUG_PRL2("[Skip ELSE ]",IFcount); - IFcurrent[IFcount]=RUN_ELSE_PORTION; - if( IFstatus[IFcount] == IF_FALSE ) { - LLDBUG_PRL1("[ELSE begin TEXT CR]\n"); - BEGIN S_TEXT; - } - if( IFstatus[IFcount] == IF_TRUE ) { - LLDBUG_PRL1("[ELSE THIS SHOULD NEVER HAPPEN.]\n"); - } - } -^{Spaces}"/ENDIF"[^\n]*{EndLine} { IFcount--; LLDBUG_PRL2("[Skip ENDIF ]\n",IFcount); - if( IFcount == 0 ) { - LLDBUG_PRL1("[ENDIF begin TEXT CR]\n"); - BEGIN S_TEXT; - } - if( (IFcurrent[IFcount] == RUN_IF_PORTION )&&(IFstatus[IFcount] == IF_TRUE)) { - LLDBUG_PRL1("[ENDIF begin TEXT CR]\n"); - BEGIN S_TEXT; - } - if( (IFcurrent[IFcount] == RUN_ELSE_PORTION )&&(IFstatus[IFcount] == IF_FALSE)) { - LLDBUG_PRL1("[ENDIF begin TEXT CR]\n"); - BEGIN S_TEXT; - } +{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; } -{EndLine} { LLDBUG_PRL1("[SkipIF a CR]\n"); } -[^\n]*$ { LLDBUG_PRL2("[SkipIF anything ]",IFcount); } +{Spaces} { /* Do nothing */ } } + { ([.]*){EndLine} { /* this ignores everything until it hits an EoL */ LLDBUG_PRL2("[ skip \'%s\' until EoL]\n",yytext); @@ -961,22 +1230,16 @@ send("$%s",yytext); { ^{Spaces}"/ENDVERB" { LLDBUG_PRL1("[END VERB]\n"); - yylval = (Symbol *) capa_malloc(1, sizeof(Symbol)); - yylval->s_str = strsave(Dynamic_buf); /* **** */ - yylval->s_type = S_CONSTANT; - capa_mfree(Dynamic_buf); - Dynamic_buf_cur=-1; - Dynamic_buf_max=0; - BEGIN S_TEXT; RETURN(VERBATIM); + BEGIN S_TEXT; + puts("\n\n"); + end_mode(); } -.*|{EndLine} { append_dynamic_buf(yytext); } +.*|{EndLine} { send(yytext); } } %% /* ========================================================================================== */ -extern void -begin_if_skip() { BEGIN S_IF_SKIP; } extern void begin_while_skip() { Wcount=0; While_idx--; /* while is FALSE, pop it out from stack */ BEGIN S_WHILE_SKIP; } @@ -1581,6 +1844,7 @@ int capa_eof() LexiProblem_p=NULL; /* printf("\nCAPA EOF\n"); fflush(stdout); */ } + end_mode(); return (0); #endif /*AVOIDYYINPUT*/ }