Index: /lang/c/misc/sandbox/calc/calc.y
===================================================================
--- /lang/c/misc/sandbox/calc/calc.y (revision 34092)
+++ /lang/c/misc/sandbox/calc/calc.y (revision 34092)
@@ -0,0 +1,84 @@
+%{
+#include <stdio.h>
+#include <stdlib.h>
+#define YYDEBUG 1
+%}
+%union {
+    int int_value;
+    double double_value;
+}
+%token <double_value> DOUBLE_LITERAL
+%token ADD SUB MUL DIV LP RP CR
+%type <double_value> expression term primary_expression
+
+%%
+line_list
+    : line
+    | line_list line
+    ;
+line
+    : expression CR
+    {
+        printf(">>%lf\n", $1);
+    }
+    | error CR
+    {
+        yyclearin;
+        yyerrok;
+    }
+    ;
+expression
+    : term
+    | expression ADD term
+    {
+        $$ = $1 + $3;
+    }
+    | expression SUB term
+    {
+        $$ = $1 - $3;
+    }
+    ;
+term
+    : primary_expression
+    | term MUL primary_expression
+    {
+        $$ = $1 * $3;
+    }
+    | term DIV primary_expression
+    {
+        $$ = $1 / $3;
+    }
+    ;
+primary_expression
+    : DOUBLE_LITERAL
+    | LP expression RP
+    {
+        $$ = $2;
+    }
+    | SUB primary_expression
+    {
+        $$ = -$2;
+    }
+    ;
+%%
+
+int
+yyerror(char const *str)
+{
+    extern char *yytext;
+    fprintf(stderr, "parser error near %s\n", yytext);
+    return 0;
+}
+
+int
+main(void)
+{
+    extern int yyparse(void);
+    extern FILE *yyin;
+    yyin = stdin;
+    if (yyparse()) {
+        fprintf(stderr, "Error! Error! Error!\n");
+        return 1;
+    }
+    return 0;
+}
Index: /lang/c/misc/sandbox/calc/calc.l
===================================================================
--- /lang/c/misc/sandbox/calc/calc.l (revision 34092)
+++ /lang/c/misc/sandbox/calc/calc.l (revision 34092)
@@ -0,0 +1,36 @@
+%{
+#include <stdio.h>
+#include "y.tab.h"
+
+int
+yywrap(void)
+{
+    return 1;
+}
+%}
+
+%%
+"+"        return ADD;
+"-"        return SUB;
+"*"        return MUL;
+"/"        return DIV;
+"("        return LP;
+")"        return RP;
+"\n"       return CR;
+
+([1-9][0-9]*)|0|([0-9]+\.[0-9]+) {
+    double tmp;
+    sscanf(yytext, "%lf", &tmp);
+    yylval.double_value = tmp;
+    return DOUBLE_LITERAL;
+}
+
+[ \t] {
+    ;
+}
+
+. {
+    fprintf(stderr, "lexical error.\n");
+    exit(1);
+}
+%%
Index: /lang/c/misc/sandbox/calc/Makefile
===================================================================
--- /lang/c/misc/sandbox/calc/Makefile (revision 34092)
+++ /lang/c/misc/sandbox/calc/Makefile (revision 34092)
@@ -0,0 +1,8 @@
+calc: y.tab.c lex.yy.c
+	gcc -o calc y.tab.c lex.yy.c
+y.tab.c: calc.y
+	bison --yacc -dv calc.y
+lex.yy.c: calc.l
+	flex calc.l
+clean:
+	rm -f calc y.tab.c y.tab.h y.output lex.yy.c
Index: /lang/c/misc/sandbox/calc/Readme
===================================================================
--- /lang/c/misc/sandbox/calc/Readme (revision 34092)
+++ /lang/c/misc/sandbox/calc/Readme (revision 34092)
@@ -0,0 +1,2 @@
+書籍「プログラミング言語を作る」から、コマンドライン電卓を作成した。
+MakefileはMac OS X Leopardでしか動作確認していない。
