--- loncom/xml/algebra/AlgParser.pm 2005/02/24 08:25:21 1.4 +++ loncom/xml/algebra/AlgParser.pm 2009/02/13 17:20:23 1.18 @@ -1,4 +1,4 @@ - +# $Id: AlgParser.pm,v 1.18 2009/02/13 17:20:23 bisitz Exp $ ## Last modification: 8/3/00 by akp ## Originally written by Daniel Martin, Dept of Math, John Hopkins @@ -39,14 +39,16 @@ $close{'('} = ')'; my $binoper3 = '(?:\\^|\\*\\*)'; my $binoper2 = '[/*_,]'; -my $binoper1 = '[-+=><%!#]'; +my $binoper1 = '(?:[-+%!])'; +my $binoper0 = '(?:<>|<=|>=|[=><])'; my $openparen = '[{(\\[]'; my $closeparen = '[})\\]]'; my $varname = '[A-Za-z](?:_[0-9]+)?'; -my $greek='alpha|bita|gamma|zita|thita|iota|kappa|lambda|mu|nu|xi|rho|sigma|tau|phi|chi|psi|omega|zepslon|zdelta|xeta|zupslon'; +my $greek='alpha|(?:(?:(?:var)?(?:[tT]h))|(?:[bz])?)eta|[gG]amma|iota|kappa|[lL]ambda|mu|nu|[xX]i|(?:var)?rho|(?:var)?[sS]igma|tau|(?:var)?(?:[pP])hi|chi|[oO]mega|(?:(?:var)?(?:[eE])|(?:[uU]))psilon|[dD]elta|[pP]si|(?:var)?[pP]i'; my $delete='zeroplace'; my $escape='infty'; -my $specialvalue = '(?:e|pi|d[a-z]|'.$escape.'|'.$greek.'|'.$delete.')'; +my $specialvalue = '(?:'.$escape.'|'.$greek.'|'.$delete.'|d[a-z]|e)'; + my $numberplain = '(?:\d+(?:\.\d*)?|\.\d+)'; my $numberE = '(?:' . $numberplain . 'E[-+]?\d+)'; my $number = '(?:' . $numberE . '|' . $numberplain . ')'; @@ -63,7 +65,7 @@ my $trigfname = '(?:(?:a(?:rc)?)?(?:sin| my $otherfunc = '(?:exp|abs|logten|log|ln|sqrt|sgn|step|fact|int|lim|fun[a-zA-Z])'; my $funcname = '(?:' . $otherfunc . '|' . $trigfname . ')'; -my $tokenregexp = "(?:($binoper3)|($binoper2)|($binoper1)|($openparen)|" . +my $tokenregexp = "(?:($binoper3)|($binoper2)|($binoper1)|($binoper0)|($openparen)|" . "($closeparen)|($funcname)|($specialvalue)|($varname)|" . "($numberE)|($number))"; @@ -76,13 +78,14 @@ sub nexttoken { if (defined($1)) {return ['binop3', $1];} if (defined($2)) {return ['binop2', $2];} if (defined($3)) {return ['binop1', $3];} - if (defined($4)) {return ['openp', $4];} - if (defined($5)) {return ['closep', $5];} - if (defined($6)) {return ['func1', $6];} - if (defined($7)) {return ['special', $7];} - if (defined($8)) {return ['varname', $8];} - if (defined($9)) {return ['numberE', $9];} - if (defined($10)) {return ['number', $10];} + if (defined($4)) {return ['binop0', $4];} + if (defined($5)) {return ['openp', $5];} + if (defined($6)) {return ['closep', $6];} + if (defined($7)) {return ['func1', $7];} + if (defined($8)) {return ['special', $8];} + if (defined($9)) {return ['varname', $9];} + if (defined($10)) {return ['numberE',$10];} + if (defined($11)) {return ['number', $11];} } else { push @{$self->{posarray}}, [$p1, undef]; @@ -117,7 +120,7 @@ sub parse { local $_; while ($currenttok) { $_ = $currenttok->[0]; - /binop1/ && do { + /binop[01]/ && do { # check if we have a binary or unary operation here. if (defined(${$currentref})) { # binary - walk up the tree until we hit an open paren or the top @@ -125,16 +128,21 @@ sub parse { $currentref = pop @backtrace; } my $index = ((${$currentref}->[0] eq 'top')?1:3); - ${$currentref}->[$index] = ['binop1', $currenttok->[1], + ${$currentref}->[$index] = [$currenttok->[0], $currenttok->[1], ${$currentref}->[$index], undef]; push @backtrace, $currentref; push @backtrace, \${$currentref}->[$index]; $currentref = \${$currentref}->[$index]->[3]; - } else { + } elsif (/binop1/) { # unary ${$currentref} = ['unop1', $currenttok->[1], undef]; push @backtrace, $currentref; $currentref = \${$currentref}->[2]; + } else { + my ($mark) = pop(@{$self->{posarray}}); + my $position = 1+$mark->[0]; + return $self->error("Didn't expect " . $currenttok->[1] . + " at position $position" , $mark); } }; /binop2/ && do { @@ -481,9 +489,10 @@ sub tostring { # print STDERR Data::Dumper->Dump([@_]); my($self) = shift; my($type, @args) = @$self; + local $_; $_ = $type; - /binop1/ && do { + /binop[01]/ && do { my ($p1, $p2) = ('',''); if ($args[2]->[0] eq 'binop1') {($p1,$p2)=qw{ ( ) };} return ($args[1]->tostring() . $args[0] . $p1 . @@ -517,9 +526,6 @@ sub tostring { }; /special|varname|numberE?/ && return $args[0]; /closep/ && do { - my(%close) = %AlgParser::close; - - return ($args[0] . $args[1]->tostring() . $close{$args[0]}); }; @@ -528,12 +534,17 @@ sub tostring { sub tolatex { my($self) = shift; my($type, @args) = @$self; + local $_; $_ = $type; - /binop1/ && do { + /binop[01]/ && do { my ($p1, $p2) = ('',''); if ($args[2]->[0] eq 'binop1') {($p1,$p2)=qw{ \left( \right) };} - return ($args[1]->tolatex() . $args[0] . $p1 . + my $cmd=$args[0]; + if ($args[0] eq '<>') { $cmd='\\not= '; } + elsif ($args[0] eq '<=') { $cmd='\\leq '; } + elsif ($args[0] eq '>=') { $cmd='\\geq '; } + return ($args[1]->tolatex() . $cmd . $p1 . $args[2]->tolatex() . $p2); }; /unop1/ && do { @@ -542,37 +553,21 @@ sub tolatex { return ($args[0] . $p1 . $args[1]->tolatex() . $p2); }; /binop2/ && do { - my ($p1, $p2, $p3, $p4) = ('','','',''); - if ($args[0] =~ /implicit/) { - if ( (($args[1]->head eq qq(number)) && - ($args[2]->head eq qq(number))) || - (($args[1]->head eq qq(binop2)) && - ($args[1]->[2]->head eq qq(number))) ) { - $args[0] = '\\,'; - } else { - $args[0] = ' '; - } - } - if ($args[1]->[0] =~ /binop1|numberE/) - {($p1,$p2)=qw{ \left( \right) };} - # if ($args[2]->[0] =~ /binop[12]|numberE/) - if ($args[2]->[0] =~ /binop[12]|numberE|unop1/) - {($p3,$p4)=qw{ \left( \right) };} + my ($lop,$rop) = ($args[1]->tolatex,$args[2]->tolatex); if ($args[0] eq '/'){ -# return('\frac{' . $p1 . $args[1]->tolatex() . $p2 . '}'. -# '{' . $p3 . $args[2]->tolatex() . $p4 . '}' ); - return('\frac{' . $args[1]->tolatex() . '}'. - '{' . $args[2]->tolatex() . '}' ); + return('\frac{'.$lop.'}{'.$rop.'}'); } - elsif ($args[0] eq '*'){ -# return('\frac{' . $p1 . $args[1]->tolatex() . $p2 . '}'. -# '{' . $p3 . $args[2]->tolatex() . $p4 . '}' ); - return($args[1]->tolatex() . '\cdot ' . $args[2]->tolatex() ); + my $op = $args[0]; + if ($args[0] eq '*'){ + $op = '\cdot '; } - else{ - return ($p1 . $args[1]->tolatex() . $p2 . $args[0] . $p3 . - $args[2]->tolatex() . $p4); + $lop = '\left('.$lop.'\right)' if ($args[1]->[0] =~ /binop1|numberE/); + $rop = '\left('.$rop.'\right)' if ($args[2]->[0] =~ /binop[12]|numberE|unop1/); + if ($args[0] =~ /implicit/) { + $op = ($lop =~ m/[.0-9]$/ && $rop =~ m/^[-+.0-9]/) ? '\cdot ' : ' '; } + return ($lop.$op.$rop); + }; /binop3/ && do { my ($p1, $p2, $p3, $p4)=('','','',''); @@ -585,7 +580,7 @@ sub tolatex { }; /func1/ && do { my($p1,$p2); - if($args[0] eq "sqrt"){($p1,$p2)=qw{ \left{ \right} };} + if($args[0] eq "sqrt"){($p1,$p2)=('{','}');} else {($p1,$p2)=qw{ \left( \right) };} # @@ -593,20 +588,41 @@ sub tolatex { # added missing trig functions # #$specialfunc = '(?:abs|logten|asin|acos|atan|sech|sgn|step|fact)'; - my $specialfunc = '(?:abs|logten|a(?:sin|cos|tan|sec|csc|cot)h?|sgn|step|fact)'; + my $specialfunc = '(?:(logten)|a(sin|cos|tan|sec|csc|cot)(h)?|sgn|step|fact|(abs))'; # # End DPVC # if ($args[0] =~ /$specialfunc/) { - return ('\mbox{' . $args[0] .'}'. $p1 . $args[1]->tolatex() . $p2); + if (defined($1)) { + return ('\log_{10}'. $p1 . $args[1]->tolatex() . $p2); + } + elsif (defined($2)) { + if (defined($3) && ($2 eq 'sec' || $2 eq 'csc' || $2 eq 'cot')) { + return ('\mathrm{' . $2.$3 .'}^{-1}'. $p1 . $args[1]->tolatex() . $p2); + } else { + return ('\\' . $2.$3 .'^{-1}'. $p1 . $args[1]->tolatex() . $p2); + } + } + elsif (defined($4)) { + return ('|' . $args[1]->tolatex() . '|'); + } + else { + return ('\mbox{' . $args[0] .'}'. $p1 . $args[1]->tolatex() . $p2); + } } else { - return ('\\' . $args[0] . $p1 . $args[1]->tolatex() . $p2); + if ($args[0] =~/(sec|csc|cot)h/) { + return ('\mathrm{' . $args[0] . '}' . $p1 . $args[1]->tolatex() . $p2); + } else { + return ('\\' . $args[0] . $p1 . $args[1]->tolatex() . $p2); + } } }; /special/ && do { - if ($args[0] eq 'pi') {return '\pi';} else {return $args[0];} + if ($args[0] =~/($greek|$escape)/) {return '\\'.$1;} + elsif ($args[0] =~/$delete/) {return '';} + else { return $args[0]; } }; /varname|(:?number$)/ && return $args[0]; /numberE/ && do { @@ -615,9 +631,8 @@ sub tolatex { }; /closep/ && do { my($backslash) = ''; - my(%close) = %AlgParser::close; if ($args[0] eq '{') {$backslash = '\\';} -#This is for editors to match: } +#This is for editors to match: } return ('\left' . $backslash . $args[0] . $args[1]->tolatex() . '\right' . $backslash . $close{$args[0]}); };