summaryrefslogtreecommitdiff
path: root/examples/hello_calc2
diff options
context:
space:
mode:
authorAndrew Shadura <andrew@shadura.me>2015-07-25 14:44:21 +0200
committerAndrew Shadura <andrew@shadura.me>2015-07-25 14:44:21 +0200
commit1d3d0bb1e461a81bc1a9cd4520958bd2b1db4c7f (patch)
tree1aa5b654ab4ab52e936550463aac7e4da0aad722 /examples/hello_calc2
Imported Upstream version 0.24.0
Diffstat (limited to 'examples/hello_calc2')
-rw-r--r--examples/hello_calc2/Makefile30
-rw-r--r--examples/hello_calc2/expect.out59
-rw-r--r--examples/hello_calc2/expressions.txt8
-rw-r--r--examples/hello_calc2/lex.l24
-rw-r--r--examples/hello_calc2/parser.y53
-rw-r--r--examples/hello_calc2/test.mk32
6 files changed, 206 insertions, 0 deletions
diff --git a/examples/hello_calc2/Makefile b/examples/hello_calc2/Makefile
new file mode 100644
index 0000000..098b5af
--- /dev/null
+++ b/examples/hello_calc2/Makefile
@@ -0,0 +1,30 @@
+PROG = calc
+
+SRCS = lex.l parser.y
+
+YHEADER = 1
+YPREFIX = calc_
+LPREFIX = calc_
+
+MKC_CHECK_BUILTINS = prog_bison prog_flex
+
+YACC = ${BUILTIN.prog_bison} -y
+LEX = ${BUILTIN.prog_flex}
+LEXLIB = -lfl
+
+lex.o: parser.h
+
+.include <mkc.configure.mk>
+
+.if empty(BUILTIN.prog_flex)
+MKC_ERR_MSG += "Cannot find flex, good bye!"
+.endif
+
+.if empty(BUILTIN.prog_bison)
+MKC_ERR_MSG += "Cannot find bison, good bye!"
+.endif
+
+LDADD += -lm
+
+.include "test.mk"
+.include <mkc.prog.mk>
diff --git a/examples/hello_calc2/expect.out b/examples/hello_calc2/expect.out
new file mode 100644
index 0000000..a50aab6
--- /dev/null
+++ b/examples/hello_calc2/expect.out
@@ -0,0 +1,59 @@
+4
+45
+-1
+45
+460
+484
+27
+73
+=========== all ============
+/objdir/Makefile
+/objdir/_mkc_compiler_type.err
+/objdir/_mkc_compiler_type.res
+/objdir/_mkc_custom_prog_bison.err
+/objdir/_mkc_custom_prog_bison.res
+/objdir/_mkc_custom_prog_flex.err
+/objdir/_mkc_custom_prog_flex.res
+/objdir/_mkc_prog_cc.err
+/objdir/_mkc_prog_cc.res
+/objdir/calc
+/objdir/expect.out
+/objdir/expressions.txt
+/objdir/hello_calc2.test.out.tmp
+/objdir/lex.c
+/objdir/lex.l
+/objdir/lex.o
+/objdir/parser.c
+/objdir/parser.h
+/objdir/parser.o
+/objdir/parser.y
+/objdir/test.mk
+========= install ==========
+/objdir/prefix
+/objdir/prefix/bin
+/objdir/prefix/bin/calc
+======== uninstall =========
+========== clean ===========
+/objdir/Makefile
+/objdir/_mkc_compiler_type.err
+/objdir/_mkc_compiler_type.res
+/objdir/_mkc_custom_prog_bison.err
+/objdir/_mkc_custom_prog_bison.res
+/objdir/_mkc_custom_prog_flex.err
+/objdir/_mkc_custom_prog_flex.res
+/objdir/_mkc_prog_cc.err
+/objdir/_mkc_prog_cc.res
+/objdir/expect.out
+/objdir/expressions.txt
+/objdir/hello_calc2.test.out.tmp
+/objdir/lex.l
+/objdir/parser.y
+/objdir/test.mk
+======= distclean ==========
+/objdir/Makefile
+/objdir/expect.out
+/objdir/expressions.txt
+/objdir/hello_calc2.test.out.tmp
+/objdir/lex.l
+/objdir/parser.y
+/objdir/test.mk
diff --git a/examples/hello_calc2/expressions.txt b/examples/hello_calc2/expressions.txt
new file mode 100644
index 0000000..357aa2f
--- /dev/null
+++ b/examples/hello_calc2/expressions.txt
@@ -0,0 +1,8 @@
+1+3
+5*9
+5-6
+(3+2)*(2+7)
+(12+34)*10
+(12-34)^2
+10+34/2
+3^2+4^3
diff --git a/examples/hello_calc2/lex.l b/examples/hello_calc2/lex.l
new file mode 100644
index 0000000..eb58f23
--- /dev/null
+++ b/examples/hello_calc2/lex.l
@@ -0,0 +1,24 @@
+
+%{
+#include <stdio.h>
+
+#define yylval calc_lval
+
+#include "parser.h"
+%}
+
+%option noyywrap
+
+digit [0-9]
+number {digit}+\.?|{digit}*\.{digit}+
+id [a-zA-Z]+
+
+%%
+
+[ ] { /* Skip spaces. */ }
+{number} { sscanf (yytext, "%lf", &yylval.value); return NUMBER; }
+\n|[-+\/*^()] { return yytext[0]; }
+
+%%
+
+YYSTYPE calc_lval;
diff --git a/examples/hello_calc2/parser.y b/examples/hello_calc2/parser.y
new file mode 100644
index 0000000..67bef50
--- /dev/null
+++ b/examples/hello_calc2/parser.y
@@ -0,0 +1,53 @@
+
+%{
+#include <stdio.h>
+#include <math.h>
+%}
+
+%union {
+ double value;
+ char* name;
+}
+
+%token <value> NUMBER
+
+%type <value> expr
+
+%left '+' '-'
+%right SQRT
+%left '*' '/'
+%right '^'
+%right UMINUS
+
+%%
+lines: lines expr '\n' { printf("%.10g\n", $2); }
+ | lines '\n'
+ | error '\n' { printf("Please re-enter last line: ");
+ yyerrok; }
+ |
+ ;
+
+expr: expr '+' expr { $$ = $1 + $3; }
+ | expr '-' expr { $$ = $1 - $3; }
+ | expr '*' expr { $$ = $1 * $3; }
+ | expr '/' expr { $$ = $1 / $3; }
+ | expr '^' expr { $$ = pow($1, $3); }
+ | '(' expr ')' { $$ = $2; }
+ | '-' expr %prec UMINUS { $$ = -$2; }
+ | NUMBER
+ ;
+
+%%
+#include <ctype.h>
+#include <stdio.h>
+
+int main (int argc, char **argv)
+{
+ return yyparse ();
+}
+
+int yyerror (char* errstr)
+{
+ printf ("Error: %s\n", errstr);
+ return 1;
+}
diff --git a/examples/hello_calc2/test.mk b/examples/hello_calc2/test.mk
new file mode 100644
index 0000000..cf8cf2f
--- /dev/null
+++ b/examples/hello_calc2/test.mk
@@ -0,0 +1,32 @@
+.PHONY : test_output
+test_output:
+ @set -e; \
+ ${.OBJDIR}/calc < ${.CURDIR}/expressions.txt; \
+ rm -rf ${.OBJDIR}${PREFIX}; \
+ \
+ echo =========== all ============; \
+ find ${.OBJDIR} -type f | \
+ mkc_test_helper "${PREFIX}" "${.OBJDIR}"; \
+ \
+ echo ========= install ==========; \
+ ${MAKE} ${MAKEFLAGS} install DESTDIR=${.OBJDIR} \
+ > /dev/null; \
+ find ${.OBJDIR}${PREFIX} -type f -o -type d | \
+ mkc_test_helper "${PREFIX}" "${.OBJDIR}"; \
+ \
+ echo ======== uninstall =========; \
+ ${MAKE} ${MAKEFLAGS} uninstall DESTDIR=${.OBJDIR} > /dev/null; \
+ find ${.OBJDIR}${PREFIX} -type f | \
+ mkc_test_helper "${PREFIX}" "${.OBJDIR}";\
+ \
+ echo ========== clean ===========; \
+ ${MAKE} ${MAKEFLAGS} clean DESTDIR=${.OBJDIR} > /dev/null; \
+ find ${.OBJDIR} -type f | \
+ mkc_test_helper "${PREFIX}" "${.OBJDIR}";\
+ \
+ echo ======= distclean ==========; \
+ ${MAKE} ${MAKEFLAGS} distclean DESTDIR=${.OBJDIR} > /dev/null; \
+ find ${.OBJDIR} -type f | \
+ mkc_test_helper "${PREFIX}" "${.OBJDIR}"
+
+.include <mkc.minitest.mk>