Äó½Ð´ü¸Â¡§ | 2001ǯ8·î15Æü |
Äó½ÐÆü¡§ | 2001ǯ8·î15Æü |
¾¿Í¤Îºî¤Ã¤¿¥×¥í¥°¥é¥à¤òÆɤà¤Î¤Ï¡¢¤Ê¤«¤Ê¤«ÂçÊѤʤâ¤Î¤Ç¤¢¤ê¡¢²ÆµÙ¤ß¤ËÆþ¤ê¡¢¼Â²È¤Ëµ¢¾Ê¤·¤Æ¤ª¤ê¡¢¤·¤«¤â¥³¥³¤Ç¤Ï¥×¥ê¥ó¥¿¤¬¸Î¾ã¤·¤Æ¤¤¤Æ°õºþ¤µ¤¨½ÐÍè¤Ê¤¤¤È¤¤¤¦¾õ¶·¤Ç¤¢¤Ã¤¿¤Î¤Ç¡¢¤È¤Æ¤âÁ°ÅÄ»á¤ÎÅÅÂî¥×¥í¥°¥é¥à¤òÁ´¤ÆÍý²ò¤¹¤ëµ¤¤Ë¤Ï¤Ê¤ì¤Ê¤«¤Ã¤¿¡£
¤½¤³¤Ç¡¢Á°ÅÄ»á¤ÎÅÅÂî¤ò²þÎɤ¹¤ì¤Ð¡¢´Êñ¤Ë¹âµ¡Ç½¤ÊÅÅÂºî¤ì¤½¤¦¤À¤¬¡¢Îɤ¯Ê¬¤«¤é¤Ê¤¤¤Þ¤Þ²þÎɤò¤·¤Æµ¡Ç½Äɲ䷤Ƥ⡢;¤êÊÙ¶¯¤Ë¤Ê¤é¤Ê¤¤¤Î¤Ç¡¢Â礷¤¿¤â¤Î¤Ç¤Ï¤Ê¤¯¤Æ¤âÎɤ¤¤«¤é¡¢¼«Ê¬¤Çflex, bison¤ò»È¤Ã¤Æ£±¤«¤éºî¤Ã¤Æ¤ß¤è¤¦¤È»×¤Ã¤¿¡£
¤Þ¤º¡¢ÌäÂê¤Ï¤½¤Î³«È¯´Ä¶¤Ç¤¢¤ë¡£º£¤Ï¼Â²È¤Ê¤Î¤Ç¡¢Linux¥Þ¥·¥ó¤¬Ìµ¤¤¡£¥Î¡¼¥È¥Ñ¥½¥³¥ó¤ËLinux¤òÆþ¤ì¤ë¤È¤¤¤¦¼ê¤â¤¢¤ë¤Ë¤Ï¤¢¤ë¤¬¡¢£²£°£Ç£Â¤Î£È£Ä£Ä¤ò¥Ð¥Ã¥¯¥¢¥Ã¥×¤ò¼è¤ë¼êÃʤµ¤¨Ìµ¤¤¤¿¤á¡¢»ö¼Â¾åÉÔ²Äǽ¤Ç¤¢¤ë¡£¤½¤³¤Ç»ÅÊý¤Ê¤¯¡¢Cygwin¤ò¥À¥¦¥ó¥í¡¼¥É¤·¤Æ¤½¤ì¤ò»È¤¦¤³¤È¤Ë¤·¤¿¡£
¤µ¤é¤Ë°¤¤¤³¤È¤Ë¡¢ÂçÀÐÀèÀ¸¤ÎËܤò¤Ï¤¸¤á¡¢¤³¤ì¤ò²ò¤¯¤¿¤á¤Î»ñÎÁ¤ò»ý¤Ã¤Æ¤¯¤ë¤Î¤ò˺¤ì¤Æ¤·¤Þ¤Ã¤¿¤Î¤Ç¤¢¤ë¡£¤µ¤é¤Ë¡¢£Ã¸À¸ì¤ÎËܤ¹¤éÀιØÆþ¤·¤¿£±ºý¤·¤«¤Ê¤¯¡¢ÅļˤʤΤÇÇ㤤¤Ë¹Ô¤Ã¤Æ¤â¥í¥¯¤ÊËܤϤʤ¤¡£¤µ¤é¤Ë¡¢¥¤¥ó¥¿¡¼¥Í¥Ã¥È´Ä¶¤â¡¢¥Æ¥ì¥Û¡¼¥À¥¤¤Ë¤¹¤éÆþ¤Ã¤Æ¤Ê¤¯¡¢¥¢¥¯¥»¥¹¥Ý¥¤¥ó¥È¤¬¤¤¤Þ¤À¤Ë28,800bps¤È¤¤¤¦Îô°¤Ê´Ä¶¤Ç¤¢¤ë¡£¤½¤³¤Ø»ý¤Ã¤Æ¤¤Æ¡¢²ÆµÙ¤ß¤Ë¤è¤ë³«Êü´¶¤Ë¤è¤ê¡¢¤Þ¤ë¤Ç¤ä¤ëµ¤¤¬µ¯¤¤Ê¤¤¡Ê¤³¤ì¤¬°ìÈÖÌäÂ꤫¡©¡Ê¾Ð¡Ë¡Ë¡£
¤·¤«¤·¡¢°ì±þ¤Ï²¿¤«ºî¤é¤Í¤Ð¤Ê¤é¤Ê¤¤¤Î¤Ç¡¢Cygwin¤ò¥¤¥ó¥¹¥È¡¼¥ë¤·¡¢»î¤·¤Ë´Êñ¤Êbison¤ÎÎý½¬ÍÑ¥×¥í¥°¥é¥à¤òÂǤÁ¹þ¤ß¡¢¼Â¹Ô¤·¤Æ¤ß¤¿¡£¤½¤Î¸å¡¢»ú¶ç²òÀÏÉôʬ¤òflex¤Ç½ÐÍè¤ë¤è¤¦¤Ë¤·¤Æ¡¢flex, bison¤ò»È¤Ã¤¿¥×¥í¥°¥é¥à¤Î»ÅÊý¤ò°ì±þÂçÂÎÍý²ò¤·¤¿¡£
ºÇ½é¤ÏÎɤ¯Ê¬¤«¤ê¤Ë¤¯¤¤¤È»×¤Ã¤Æ¤¤¤¿bison¤â¡¢»È¤Ã¤Æ¤ß¤ë¤È·ë¹½´Êñ¤Ç¤¢¤ë¡£¤·¤«¤â¡¢¤³¤ó¤Ê¤â¤Î¤Ç¥¤¥ó¥¿¥×¥ê¥¿¤ä¥³¥ó¥Ñ¥¤¥é¤¬ºî¤ì¤Æ¤·¤Þ¤¦¤È¤Ï¡ª¤Ê¤«¤Ê¤«ÌÌÇò¤¤¤â¤Î¤À¡£
ºÇ½é¤Ï¡¢ÉáÄ̤μ¿ôƱ»Î¤Î»Í§±é»»¤¬½ÐÍè¡¢¥«¥Ã¥³¤ä¡ö¡¢¡Ü¤Ê¤É¤ÎÍ¥Àè½ç°Ì¤ò¹Íθ¤¹¤ëÅÅÂî¤òºî¤Ã¤Æ¤ß¤¿¡£¤³¤ì¤Ï´Êñ¤À¡£
¼¡¤Ë¡¢¤³¤ì¤òÊ£ÁÇ¿ô¤¬°·¤¨¤ë¤è¤¦¤Ë²þÎɤ·¤Æ¤ß¤¿¡£Ê£ÁÇ¿ô¤¬°·¤¨¤ë¤è¤¦¤Ë¤¹¤ë¤¿¤á¤Ë¡¢¿ô»ú¤òµ²±¤·¤Æ¤ª¤¯Îΰè¤Ï¡¢real¤Èimage¤È£²¤ÄÍÑ°Õ¤·¡¢¤½¤ì¤ÇÊ£ÁÇ¿ô¤òÊݸ¤Ç¤¤ë¤è¤¦¤Ë¤·¤¿¡£¤Þ¤¡Åö¤¿¤êÁ°¤Ç¤¢¤ë¤¬¡£
¸å¤Ï¡¢Ê£ÁÇ¿ôƱ»Î¤Î³Ý¤±»»¤ä³ä¤ê»»¤Ï¤É¤¦¤ä¤ì¤Ð¤è¤¤¤«¤ò»æ¤Ë½ñ¤¤¤Æ¹Í¤¨¡Ê¤È¤¤¤¦¤Û¤É¤Î¤â¤Î¤Ç¤â¤Ê¤¤¤¬¡Ë¡¢¤½¤ì¤ò¥×¥í¥°¥é¥à¤·¤¿¡£
¼¡¤Ï¡¢ÊÑ¿ô¤òÍÑ°Õ¤·¤è¤¦¡£ÊÑ¿ô¤ËÃͤòÂåÆþ¤Ç¤¤ë¤è¤¦¤Ë¤¹¤ë¤È¤«¤Ê¤êÊØÍø¤ÊÅÅÂî¤Ë¤Ê¤ë¤À¤í¤¦¡£ÊÑ¿ô¤ò¤É¤Î¤è¤¦¤Ë¤·¤ÆÊݸ¤·¤Æ¤ª¤¯¤«¤Ï»×°Æ¤Î¤·¤É¤³¤í¤Ç¤¢¤ë¡£»ä¤Î¾ì¹ç¡¢Àþ·Á¥ê¥¹¥È¤òÍѤ¤¤ë¤³¤È¤Ë¤·¤¿¡£
ÊÑ¿ô¤ÎÃͤòÊݸ¤¹¤ë¹½Â¤ÂΤòÍÑ°Õ¤¹¤ë¡£¤³¤Î¹½Â¤ÂΤˤϡ¢Ì¾Á°¡¢¼Â¿ô¡¢µõ¿ô¡¢¼¡¤Î¹½Â¤ÂΤؤΥݥ¤¥ó¥¿Åù¤Î¥á¥ó¥Ð¡¼¤¬¤¢¤ë¡£¤³¤ì¤Ë¤è¤ê¡¢ºÇ½é¤Î¹½Â¤ÂΤ«¤é̾Á°¤ò¥Á¥§¥Ã¥¯¤·¤Æ¤¤¤Ã¤Æ¡¢¤½¤ì¤Ç¤Ê¤±¤ì¤Ð¼¡¤Î¹½Â¤ÂΤء¦¡¦¡¦¤È¤¤¤¦¶ñ¹ç¤Ë¸¡º÷¤·¤Æ¤æ¤¡¢ºÇ¸å¤Î¹½Â¤ÂΤˤʤäƤ⤽¤Î̾Á°¤¬Ìµ¤«¤Ã¤¿¤é¡¢¿·¤·¤¤¹½Â¤ÂΤòºî¤ë¡Ê¤Ä¤Þ¤ê¡¢¿·¤·¤¤ÊÑ¿ô¤òºî¤ë¡Ë¤È¤¤¤¦¤è¤¦¤Ë¤·¤Æ¤¤¤ë¡£
¤½¤·¤Æ¡¢¼¡¤Ësin, cosÅù¤ÎÁȤ߹þ¤ß´Ø¿ô¤ò»È¤¨¤ë¤è¤¦¤Ë¤·¤¿¤¤¡£¤³¤ì¤Ï´Êñ¤Ç¤¢¤ë¡£flex¤Èbison¤Ë¤½¤ì¤¾¤ì¡¢¤Á¤ç¤Ã¤È¥×¥í¥°¥é¥à¤ò½ñ¤¤¤Æ¤ä¤ë¤À¤±¤Ç½ÐÍè¤Æ¤·¤Þ¤¦¡£·ë¶É¡¢»ä¤Î¥×¥í¥°¥é¥à¤Ç¤Ï¡¢
"sin, cos, tan, arccos, arcsin, arctan, cosh, sinh, tanh, exp, ln, log, sqrt"
¤È¤¤¤Ã¤¿´Ø¿ô¤ò»ÈÍѤǤ¤ë¤è¤¦¤Ë¤·¤¿¡£
¤½¤·¤ÆºÇ¸å¤Ï¡¢¹ÔÎó¤ò°·¤¨¤ë¤è¤¦¤Ë¤·¤¿¡£¹ÔÎó¤ò°·¤¨¤ë¤è¤¦¤Ë¤¹¤ë¤¿¤á¡¢exp¤Î¿ôÃͤò³ÊǼ¤¹¤ëÊÑ¿ô¡Ê»ä¤Î¾ì¹ç¤Ïnum* val)¤Ë¡¢¤½¤ÎÃͤμïÎà¡ÊÊ£ÁÇ¿ô¤«¹ÔÎ󤫡ˡ¢¼Â¿ôÃÍ¡¢µõ¿ôÃÍ¡¢¹ÔÎó¤ÎÍ×ÁǤؤΥݥ¤¥ó¥¿¡¢¹ÔÎó¤Î¹Ô¡¦Îó¿ô¤òÆþ¤ì¤é¤ì¤ë¤è¤¦¤Ë¤·¤¿¡£°Ê²¼¤Î¹½Â¤ÂΤ¬¤½¤ì¤Ç¤¢¤ë¡£
struct num { int kind; /* VAL or PROC */ double real; double image; double *proc; int x_width; int y_width; }; typedef struct num num;
¤Þ¤¿¡¢¤³¤ì¤Ë¹ç¤ï¤»¡¢ÊÑ¿ôÍÑÀþ·Á¥ê¥¹¥È¤Ë»ÈÍѤ¹¤ë¹½Â¤ÂΤâ°Ê²¼¤Î¤è¤¦¤ËÊѹ¹¤·¤¿¡£³Æ¥á¥ó¥Ð¡¼¤Î°ÕÌ£¤Ï¡¢¾å¤Î¤â¤Î¡ÜÊÑ¿ô¤Î̾Á°¡Ü¼¡¤Î¥ê¥¹¥È¤Ø¤Î¥Ý¥¤¥ó¥¿¤È¤¤¤Ã¤¿¤È¤³¤í¤Ç¤¢¤ë¡£
struct symboltable { char *name; int kind; double real; double image; double* proc; int x_width; int y_width; struct symboltable * next; }; typedef struct symboltable symboltable;
¹ÔÎó¤ÎÄêµÁ¤Ï¡¢
| (1) |
¤Ç¤¢¤ì¤Ð¡¢
[1 2;3 4]¤Î¤è¤¦¤Ë¤·¤Æɽ¤»¤ë¤è¤¦¤Ë´Êñ¤Ë¹½Ê¸µ¬Â§¤âºî¤Ã¤Æ¤ß¤¿¡£
¹ÔÎó¤¬É½¸½¤Ç¤¤ë¤è¤¦¤Ë¤Ê¤ë¤È¡¢¼¡¤Ï¹ÔÎó¤Î±é»»¤Ç¤¢¤ë¡£¤½¤³¤Ç¡¢BLAS¤Î´Ø¿ô¤ò»È¤¤¤¿¤¤¤È¤³¤í¤À¡£¤À¤¬¡¢¤³¤³¤Ç¤Þ¤¿ÌäÂêȯÀ¸¤Ç¤¢¤ë¡£¤Ê¤¼¤«Cygwin¤Ëlapack¤ò¥¤¥ó¥¹¥È¡¼¥ë¤·¤Æ¤â¡¢BLAS´Ø¿ô¤¬»ÈÍѤǤ¤Ê¤¤¤Î¤Ç¤¢¤ë¡£
¸·Ì©¤Ë¸À¤¦¤È¡¢lapack¤ÎMakefile¤Ë¤Ï¤¤¤¯¤Ä¤«¥Ð¥°¤¬¤¢¤ë¤Î¤Ç¡¢¤½¤ì¤ò½¤Àµ¤·¡¢¤¢¤ëÄøÅ٤ޤǤϥ¤¥ó¥¹¥È¡¼¥ë¤Ç¤¤ë¤Î¤À¤¬¡¢ºÇ¸å¤ÎÊý¤Î¥¿¥¤¥ß¥ó¥°´ØÏ¢¤Î¤È¤³¤í¤ÇCygwin¤Ç¤Ï»ß¤Þ¤Ã¤Æ¤·¤Þ¤¦¡£¸¶°ø¤ÏÉÔÌÀ¤À¤¬¡¢´°àú¤Ë¤Ï¥¤¥ó¥¹¥È¡¼¥ë¤Ç¤¤Ê¤¤¤Î¤Ç¤¢¤ë¡£¤¬¡¢BLAS¤ÎÉôʬ¤Ï½ª¤ï¤Ã¤Æ¤¤¤ë¥Ï¥º¤Ê¤Î¤Ç¡¢BLAS´Ø¿ô¤¯¤é¤¤¤Ï»È¤¨¤½¤¦¤Ê¤â¤Î¤Ê¤Î¤À¤¬¡¢¿ïʬ¿§¡¹¤È¤ä¤Ã¤Æ¤ß¤¿¤¬¡¢¤É¤¦¤ä¤Ã¤Æ¤âBLAS´Ø¿ô¡Êdgemm_¡Ë¤Ï»È¤¨¤ë¤è¤¦¤Ë¤Ê¤é¤Ê¤«¤Ã¤¿¡£¡Ê¤À¤¬¡¢lapack¤Î´Ø¿ô(dgesv)¤ÏÌäÂê¤Ê¤¯»È¤¨¤ë¡£¡Ë
¤Ê¤¼¤«BLAS´Ø¿ô¤¬¡¢°ìÈÌŪ¤Ê¤ä¤êÊý¤Ç¤Ï»È¤¨¤Ê¤«¤Ã¤¿¤Î¤Ç¡¢»ÅÊý¤Ê¤¯¡¢Á°²ó¤Î²ÝÂê¤Ç¼«Ê¬¤ÇºîÀ®¤·¤¿"dgemm_"´Ø¿ô¤ò»È¤¦¤³¤È¤Ë¤·¤¿¡£¤³¤ì¤Ç°ì±þ¥×¥í¥°¥é¥àÃæ¤Ç¤Ï¡¢dgemm_¤ò»ÈÍѤ·¤Æ¤¤¤ë¤Î¤Ç¡¢´Êñ¤ÊÊѹ¹¤Ç¡¢¸å¤ÇËÜʪ¤ÎBLAS¤ò»ÈÍѤ¹¤ë¤è¤¦¤Ë½ÐÍè¤ë¡£
¤³¤ì¤Ë¤è¤ê¡¢¹ÔÎóƱ»Î¤Î¡Ü¡¢¡Ý¡¢¡ö¡¢¤Þ¤¿¡¢¹ÔÎó¤È¥¹¥«¥é¡¼¤È¤Î¡Ü¡¢¡Ý¡¢¡ö¤¬½ÐÍè¤ë¤è¤¦¤Ë¥×¥í¥°¥é¥à¤·¤¿¡£¤½¤·¤Æ¡¢x=a\b¤Î¤è¤¦¤Ë¤·¤Æ¡¢Ax=b¤ò²ò¤±¤ë¤è¤¦¤Ë¤·¤¿¡£¤³¤ì¤Ë¤Ï¡¢dgesv¤ò»ÈÍѤ·¤¿¡£
º£²ó»ä¤ÎºîÀ®¤·¤¿ÅÅÂî¤Ï¡¢°Ê¾å¤Î¤è¤¦¤Ê¤â¤Î¤Ç¤¢¤ë¡£ ´Êñ¤Ë»ÅÍͤò¤Þ¤È¤á¤ë¤È¡¢
¤È¤¤¤Ã¤¿¤È¤³¤í¤Ç¤¢¤ë¡£¤Þ¤À¤Þ¤ÀÂ礷¤¿¤â¤Î¤Ç¤Ï¤Ê¤¤¤¬¡¢¤³¤ó¤Ê¤Ë´Êñ¤ËÅÅÂî¤Î¥¤¥ó¥¿¥×¥ê¥¿¤¬ºî¤ì¤ë¤È¤¤¤¦¤³¤È¤Ë´¶Æ°¤·¤¿¡£¤Þ¤À¡¢¥¨¥é¡¼½èÍý¤ä¥á¥â¥ê´ÉÍý¤Ê¤É¸·Ì©¤Ë¤ä¤é¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤Éôʬ¤¬Â¿¡¹¤¢¤ë¤¬¡¢¼«Ê¬¤ÇÁ´¤Æ¥×¥í¥°¥é¥à¤òÁȤà¤Î¤ÈÈæ¤Ù¤ì¤Ð¡¢ÍÚ¤«¤Ë³Ú¤Ç¤¢¤ë¡£flex, bison¤Î»È¤¤Êý¤Ïʬ¤«¤Ã¤¿¤Î¤Ç¡¢º£²ó¤Î²ÝÂê¤òÎý½¬¤È¤·¡¢¤¤¤º¤ì¡¢¤â¤Ã¤ÈËܳÊŪ¤Ê¤â¤Î¤òºî¤Ã¤Æ¤ß¤¿¤¤¤È»×¤Ã¤Æ¤¤¤ë¡£
# °ìÈÌŪ¤ÊÊ£ÁÇ¿ô¤Î±é»»¤ò¤·¤Æ¤ß¤ë 25+32*(7*3+4); ans = 825 5.2i*3.1+2.5*4+(2i*3-1.2); ans = 8.8+22.12i # ÊÑ¿ô¤ò»È¤Ã¤Æ¡¢·×»»¤·¤Æ¤ß¤ë oishi = 2.3+3.2i; ans = 2.3+3.2i shinichi = 5.3i-2.3; ans = -2.3+5.3i oishi+shinichi; ans = 8.5i oishi-shinichi; ans = 4.6-2.1i oishi*shinichi; ans = -22.25+4.83i oishi/shinichi; ans = 0.349611-0.58568i # ÁȤ߹þ¤ß´Ø¿ô¡ÊÊÑ¿ô¡Ë¤ò»È¤Ã¤Æ¤ß¤ë a=sin(0.6*pi); ans = -0.309017 b=cos(1.3*pi); ans = -0.809016 a*b; ans = 0.25 # ¹ÔÎó¤Î±é»»¤ò¹Ô¤Ã¤Æ¤ß¤ë [1 2; 3 4]; ans = 1 2 3 4 3 + [1 2;3 4]; ans = 4 5 6 7 [2 4;6 7] + 8; ans = 10 12 14 15 2-[1 2;3 4]; ans = 1 0 -1 -2 [1 2;3 4]-5; ans = -4 -3 -2 -1 A = [3 4; 5 6]; ans = 3 4 5 6 B = [2 5; 7 8]; ans = 2 5 7 8 A+B; ans = 5 9 12 14 A-B; ans = 1 -1 -2 -2 2*A-3*B; ans = 0 -7 -11 -12 A*B; ans = 34 47 52 73 # ϢΩÊýÄø¼°¤ò²ò¤¤¤Æ¤ß¤ë A = [2 6 4;3 7 2; 1 2 3]; ans = 2 6 4 3 7 2 1 2 3 b = [8;6;7]; ans = 8 6 7 x = A \ b; ans = 3 -1 2
/* * flex file for 'expr' * * Programmed by Takuya Saito * 2001-8-15 * */ %{ #include "cal.h" #include "y.tab.h" %} %option noyywrap %s COMMENT %s GRETU %% <INITIAL>0i? { num *ptr; ptr = (num *) malloc (sizeof(num)); ptr->kind = VAL; ptr->real = 0; ptr->image = 0; yylval.val = ptr; return NUMBER; } <INITIAL>[1-9][0-9]* { num *ptr; ptr = (num *) malloc (sizeof(num)); ptr->kind = VAL; ptr->real = atof(yytext); ptr->image = 0; yylval.val = ptr; return NUMBER; } <INITIAL>[1-9][0-9]*i { num *ptr; ptr = (num *) malloc (sizeof(num)); ptr->kind = VAL; ptr->image = atof(yytext); ptr->real = 0; yylval.val = ptr; return NUMBER; } <INITIAL>[0-9]*\.[0-9]* { num *ptr; ptr = (num *) malloc (sizeof(num)); ptr->kind = VAL; sscanf(yytext, "%lf", &(ptr->real)); ptr->image = 0; yylval.val = ptr; return NUMBER; } <INITIAL>[0-9]*\.[0-9]*i { num *ptr; ptr = (num *) malloc (sizeof(num)); ptr->kind = VAL; sscanf(yytext, "%lfi", &(ptr->image)); ptr->real = 0; yylval.val = ptr; return NUMBER; } <INITIAL>"pi" return PI; <INITIAL>"sin" return SIN; <INITIAL>"cos" return COS; <INITIAL>"tan" return TAN; <INITIAL>"arccos" return ACOS; <INITIAL>"arcsin" return ASIN; <INITIAL>"arctan" return ATAN; <INITIAL>"cosh" return COSH; <INITIAL>"sinh" return SINH; <INITIAL>"tanh" return TANH; <INITIAL>"exp" return EXP; <INITIAL>"ln" return LN; <INITIAL>"log" return LOG; <INITIAL>"sqrt" return SQRT; <INITIAL>[A-Za-z][A-Za-z0-9_]* { symboltable* s; s = getsym(yytext); if ( s == 0 ) { s = putsym(yytext); } yylval.tableptr = s; return VAR; } <INITIAL>"+" return PLUS; <INITIAL>"-" return MINUS; <INITIAL>"*" return MULT; <INITIAL>"/" return DIV; <INITIAL>"^" return EXPON; <INITIAL>"(" return LB; <INITIAL>")" return RB; <INITIAL>";" return SEMICOLON; <INITIAL>"=" return ASSIGN; <INITIAL>"[" return LK; <INITIAL>"]" return RK; <INITIAL>"\\" return YEN; <INITIAL>^# BEGIN COMMENT; <INITIAL>[ \n\t] ; <INITIAL>. { yyerror("Illegal character"); return(SEMICOLON); } <COMMENT>\n BEGIN INITIAL; <COMMENT>. ; %% extern symboltable *sym_table; /* allocate new variable */ symboltable* putsym(char* sym_name) { symboltable *ptr; ptr = (symboltable*) malloc (sizeof(symboltable)); ptr->kind = VAL; ptr->name = (char*) malloc (strlen (sym_name) + 1); strcpy (ptr->name, sym_name); ptr->real = 0; ptr->image = 0; ptr->proc = 0; ptr->x_width = 0; ptr->y_width = 0; ptr->next = (struct symboltable *) sym_table; sym_table = ptr; return ptr; } /* search for variable */ symboltable* getsym(char *sym_name) { symboltable *ptr; for (ptr = sym_table; ptr != (symboltable*) 0; ptr = (symboltable*)ptr->next) if (strcmp(ptr->name, sym_name) == 0) return ptr; return 0; }
/* * Calculator (bison file) * * Programmed by Takuya Saito * 2001-8-15 * */ %{ #include <stdio.h> #include <math.h> #include "cal.h" %} %union { num* val; symboltable* tableptr; } %token <val> NUMBER GNUM %token <tableptr> VAR %token PLUS MINUS MULT DIV EXPON SEMICOLON %token LB RB ASSIGN YEN %token SIN COS TAN SINH COSH TANH ACOS ASIN ATAN %token EXP LN LOG SQRT PI %token LK RK %left MINUS PLUS LB %left MULT DIV YEN %right EXPON RB %type <val> exp %% input: | input line ; line: SEMICOLON | exp SEMICOLON { disp($1); } ; glist: nums { ProcyPlus(); } | glist SEMICOLON nums { ProcyPlus(); } ; nums: exp { proc_num($1); } | nums exp { proc_num($2); } ; exp: NUMBER { $$ = $1; } | VAR { $$ = (num*) var_to_num($1); } | VAR ASSIGN exp { $$ = (num*) var_assign($1, $3); } | exp PLUS exp { $$ = (num*) enzan(PLUS, $1, $3); } | exp MINUS exp { $$ = (num*) enzan(MINUS, $1, $3); } | exp MULT exp { $$ = (num*) enzan(MULT, $1, $3); } | exp DIV exp { $$ = (num*) enzan(DIV, $1, $3); } | exp YEN exp { $$ = (num*) equation($1,$3); } | MINUS exp %prec MINUS { $$ = (num*)minus( $2 ); } | exp EXPON exp { $1->real = pow($1->real, $3->real); $$ = $1;} | LB exp RB { $$ = $2; } | COS LB exp RB { $$ = (num*) defined_func (COS, $3); } | SIN LB exp RB { $$ = (num*) defined_func (SIN, $3); } | TAN LB exp RB { $$ = (num*) defined_func (TAN, $3); } | ACOS LB exp RB { $$ = (num*) defined_func (ACOS, $3); } | ASIN LB exp RB { $$ = (num*) defined_func (ASIN, $3); } | ATAN LB exp RB { $$ = (num*) defined_func (ATAN, $3); } | COSH LB exp RB { $$ = (num*) defined_func (COSH, $3); } | SINH LB exp RB { $$ = (num*) defined_func (SINH, $3); } | TANH LB exp RB { $$ = (num*) defined_func (TANH, $3); } | EXP LB exp RB { $$ = (num*) defined_func (EXP, $3); } | LN LB exp RB { $$ = (num*) defined_func (LN, $3); } | LOG LB exp RB { $$ = (num*) defined_func (LOG, $3); } | SQRT LB exp RB { $$ = (num*) defined_func (SQRT, $3); } | PI { $$ = (num*) malloc (sizeof(num)); $$->real = 3.141592; $$->image = 0.0; $$->kind = VAL; } | LK glist RK { double *ptr; int x, y; $$ = (num*) malloc (sizeof(num)); ptr = (double *)proc_list(&x, &y); $$->kind = PROC; $$->proc = ptr; $$->x_width = x; $$->y_width = y; } ; %% yyerror(char *s) { printf("%s\n", s); } symboltable *sym_table = (symboltable*) 0; proclist *gProc1stPtr = (proclist*) 0; int gProc_x = 0; int gProc_y = 0; int main() { gProc1stPtr = 0; yyparse(); }
/* * Subroutine for expr.y * * Programmed by Takuya Saito * 2001-8-15 * */ #include <stdio.h> #include <math.h> #include "cal.h" #include "y.tab.h" #include "f2c.h" extern int dgemm_(char *transa, char *transb, int *m, int * n, int *k, double *alpha, double *a, int *lda, double *b, int *ldb, double *beta, double *c__, int *ldc); extern proclist *gProc1stPtr; extern int gProc_x; extern int gProc_y; /* display result */ void disp(num* val) { double r, i; printf("\nans =\n"); if (val->kind == VAL) { r = val->real; i = val->image; if (r != 0.0 && i != 0.0) { if (i >= 0.0) { printf(" %g+%gi\n", r, i); } else { printf(" %g%gi\n", r, i); } } else if (r != 0.0 && i == 0.0) { printf(" %g\n", r); } else if (r == 0.0 && i != 0.0) { printf(" %gi\n", i); } else { printf(" 0\n"); } } else { int x,y,xw,yw; xw = val->x_width; yw = val->y_width; for (y=0; y < yw; y++) { printf(" "); for (x=0; x < xw; x++) { printf("%g ", val->proc[x*yw+y]); } printf("\n"); } } printf("\n"); } num* enzan(int kind, num* val1, num* val3) { double r1, r3, i1, i3; double cd2, c2, d2; r1 = val1->real; i1 = val1->image; r3 = val3->real; i3 = val3->image; switch(kind) { case PLUS: if (val1->kind == VAL && val3->kind == VAL) { val1->real = r1 + r3; val1->image = i1 + i3; } else if (val1->kind == PROC && val3->kind == PROC) { int x, y, yw; double* ptr; yw = val1->y_width; ptr = (double*) malloc (sizeof(double) * val1->x_width * val1->y_width); for (x=0; x < val1->x_width; x++) for (y=0; y < yw; y++) ptr[x*yw+y] = val1->proc[x*yw+y] + val3->proc[x*yw+y]; val1->proc = ptr; } else if (val1->kind == VAL && val3->kind == PROC) { int x, y, yw; double* ptr; num* val; yw = val3->y_width; ptr = (double*) malloc (sizeof(double) * val3->x_width*val3->y_width); val = (num*) malloc (sizeof(num)); for (x=0; x < val3->x_width; x++) for (y=0; y < yw; y++) ptr[x*yw+y] = val1->real + val3->proc[x*yw+y]; val->kind = PROC; val->proc = ptr; val->x_width = val3->x_width; val->y_width = val3->y_width; return val; } else if (val1->kind == PROC && val3->kind == VAL) { int x, y, yw; double* ptr; num* val; yw = val1->y_width; ptr = (double*)malloc(sizeof(double) * val1->x_width * val1->y_width); val = (num*) malloc (sizeof(num)); for (x=0; x < val1->x_width; x++) for (y=0; y < yw; y++) ptr[x*yw+y] = val1->proc[x*yw+y] + val3->real; val->kind = PROC; val->proc = ptr; val->x_width = val1->x_width; val->y_width = val1->y_width; return val; } break; case MINUS: if (val1->kind == VAL && val3->kind == VAL) { val1->real = r1 - r3; val1->image = i1 - i3; } else if (val1->kind == PROC && val3->kind == PROC) { int x, y, yw; double* ptr; ptr = (double*) malloc (sizeof(double) * val1->x_width * val1->y_width); yw = val1->y_width; for (x=0; x < val1->x_width; x++) for (y=0; y < yw; y++) ptr[x*yw+y] = val1->proc[x*yw+y] - val3->proc[x*yw+y]; val1->proc = ptr; } else if (val1->kind == VAL && val3->kind == PROC) { int x, y, yw; double* ptr; num* val; ptr = (double*)malloc (sizeof(double) * val3->x_width*val3->y_width); val = (num*) malloc (sizeof(num)); yw = val3->y_width; for (x=0; x < val3->x_width; x++) for (y=0; y < yw; y++) ptr[x*yw+y] = val1->real - val3->proc[x*yw+y]; val->kind = PROC; val->proc = ptr; val->x_width = val3->x_width; val->y_width = val3->y_width; return val; } else if (val1->kind == PROC && val3->kind == VAL) { int x, y, yw; double* ptr; num* val; ptr = (double*) malloc (sizeof(double)*val1->x_width*val1->y_width); val = (num*) malloc (sizeof(num)); yw = val1->y_width; for(x=0; x < val1->x_width; x++) for(y=0; y < yw; y++) ptr[x*yw+y] = val1->proc[x*yw+y] - val3->real; val->kind = PROC; val->proc = ptr; val->x_width = val1->x_width; val->y_width = val1->y_width; return val; } break; case MULT: if (val1->kind == VAL && val3->kind == VAL) { val1->real = r1*r3 - i1*i3; val1->image = r1*i3 + i1*r3; } else if (val1->kind == PROC && val3->kind == PROC) { int m, n, k, lda, ldb, ldc; char transa, transb; double alpha, beta; double* C; m = val1->y_width; k = val1->x_width; n = val3->x_width; lda = m; ldb = k; ldc = m; alpha = 1.0; beta = 0.0; transa = 'n'; transb = 'n'; C = (double*) malloc (sizeof(double) * m * n); /* call BLAS function */ dgemm_(&transa, &transb, &m, &n, &k, &alpha, val1->proc, &lda, val3->proc, &ldb, &beta, C, &ldc); val1->kind = PROC; val1->real = 0; val1->image = 0; val1->proc = C; val1->x_width = n; val1->y_width = m; } else if (val1->kind == VAL && val3->kind == PROC) { int x, y, yw; double real; real = val1->real; yw = val3->y_width; for (x=0; x < val3->x_width; x++) { for (y=0; y < val3->y_width; y++) { val3->proc[x*yw + y] *= real; } } val1->kind = PROC; val1->real = 0; val1->image = 0; val1->proc = val3->proc; val1->x_width = val3->x_width; val1->y_width = val3->y_width; } else if (val1->kind == PROC && val3->kind == VAL) { int x, y, yw; double real; real = val3->real; yw = val1->y_width; for (x=0; x < val1->x_width; x++) { for (y=0; y < val1->y_width; y++) { val1->proc[x*yw + y] *= real; } } } break; case DIV: cd2 = r3*r3 + i3*i3; c2 = r3 / cd2; d2 = -i3 / cd2; val1->real = r1*c2 - i1*d2; val1->image = r1*d2 + i1*c2; break; } return val1; } /* minus value */ num* minus(num* val) { if (val->kind == VAL) { val->real = -val->real; val->image = -val->image; } else { int x, y, yw; double* ptr; yw = val->y_width; ptr = (double*) malloc (sizeof(double) * val->x_width * val->y_width); for (x=0; x < val->x_width; x++) for (y=0; y < yw; y++) ptr[x*yw+y] = - val->proc[x*yw+y]; val->proc = ptr; } return val; } /* defined functions */ num* defined_func(int kind, num* val) { switch(kind) { case COS: val->real = sin(val->real); break; case SIN: val->real = cos(val->real); break; case TAN: val->real = tan(val->real); break; case ACOS: val->real = acos(val->real); break; case ASIN: val->real = asin(val->real); break; case ATAN: val->real = atan(val->real); break; case COSH: val->real = cosh(val->real); break; case SINH: val->real = sinh(val->real); break; case TANH: val->real = tanh(val->real); break; case EXP: val->real = exp(val->real); break; case LN: val->real = log(val->real); break; case LOG: val->real = log10(val->real); break; case SQRT: val->real = sqrt(val->real); break; } return val; } /* x=A\b */ num* equation(num* A, num* b) { int x, y; int n, nrhs, lda, ldb, info; int* ipiv; double* X; double* A2; num* re; n = A->y_width; ipiv = (int*) malloc (sizeof(int) * A->y_width); nrhs = b->x_width; lda = A->y_width; ldb = A->y_width; X= (double*) malloc (sizeof(double) * b->x_width * b->y_width); for (x=0; x < b->x_width; x++) for (y=0; y < b->y_width; y++) X[x*b->y_width + y] = b->proc[x*b->y_width + y]; A2 = (double*) malloc (sizeof(double) * A->x_width * A->y_width); for (x=0; x < A->x_width; x++) for (y=0; y < A->y_width; y++) A2[x*A->y_width + y] = A->proc[x*A->y_width + y]; /* call lapack function */ dgesv_(&n, &nrhs, A2, &lda, ipiv, X, &ldb, &info); re = (num*) malloc (sizeof(num)); re->kind = PROC; re->real = 0; re->image = 0; re->proc = X; re->x_width = b->x_width; re->y_width = b->y_width; return re; } /* variable to num */ num* var_to_num(symboltable* val) { num* p; p = (num*) malloc (sizeof(num)); p->kind = val->kind; p->real = val->real; p->image = val->image; p->proc = val->proc; p->x_width = val->x_width; p->y_width = val->y_width; return p; } /* variable assign */ num* var_assign(symboltable* tp, num* val) { tp->kind = val->kind; tp->real = val->real; tp->image = val->image; tp->proc = val->proc; tp->x_width = val->x_width; tp->y_width = val->y_width; return val; } void proc_num(num* val) { proclist* ptr; ptr = (proclist*) malloc (sizeof(proclist)); if (gProc1stPtr == 0) { /* when first time */ gProc1stPtr = ptr; gProc_x = 1; gProc_y = 0; } else { /* when not first time */ proclist *p; /* searching for the last pointer */ for (p=gProc1stPtr; p->next != 0; p = p->next) ; p->next = ptr; gProc_x++; } ptr->real = val->real; ptr->next = 0; } double* proc_list(int* x_width, int* y_width) { proclist* p; double *ptr, *ptr2; int x, y, x2, y2, i; y = gProc_y; x = gProc_x / gProc_y; ptr = (double*) malloc (sizeof(double) * x * y); ptr2 = (double*) malloc (sizeof(double) * x * y); p = gProc1stPtr; for (i = 0; i < x*y; i++) { ptr[i] = p->real; p = p->next; } for (x2=0; x2 <= x; x2++) { for (y2=0; y2 <= y; y2++) { ptr2[x2*y + y2] = ptr[x2 + y2*x]; } } *x_width = x; *y_width = y; gProc_x = 0; gProc_y = 0; gProc1stPtr = 0; return ptr2; } void ProcyPlus() { gProc_y++; }
#define VAL 1 #define PROC 2 struct num { int kind; /* VAL or PROC */ double real; double image; double *proc; int x_width; int y_width; }; typedef struct num num; struct symboltable { char *name; int kind; double real; double image; double* proc; int x_width; int y_width; struct symboltable * next; }; typedef struct symboltable symboltable; struct proclist { double real; struct proclist *next; }; typedef struct proclist proclist; symboltable* putsym(char* sym_name); symboltable* getsym(char* sym_name);