From 226bfe84c5f690462adbf386dc66316cc21fdb28 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 22 Nov 2021 20:53:01 +0800 Subject: import(life): Add compile principle codes. --- works/life/compile-principle-experiment/2/hoc.y | 106 +++++++++++++++++++++ works/life/compile-principle-experiment/2/makefile | 5 + 2 files changed, 111 insertions(+) create mode 100644 works/life/compile-principle-experiment/2/hoc.y create mode 100644 works/life/compile-principle-experiment/2/makefile (limited to 'works/life/compile-principle-experiment/2') diff --git a/works/life/compile-principle-experiment/2/hoc.y b/works/life/compile-principle-experiment/2/hoc.y new file mode 100644 index 0000000..14500bb --- /dev/null +++ b/works/life/compile-principle-experiment/2/hoc.y @@ -0,0 +1,106 @@ +%{ +#include +#include +#include + +int yylex(); +int yyerror(const char * s); +void execerror(const char* s, const char* t); +void warning(const char* s, const char* t); + +double mem[26]; /* memory for variables 'a'..'z' */ +%} + +%union { /* stack type */ + double val; /* actual value */ + int index; /* index into mem[] */ +} +%token NUMBER +%token VAR +%type expr +%right '=' +%left '+' '-' +%left '*' '/' +%left UNARYMINUS +%% +list: /* nothing */ + | list '\n' + | list expr '\n' { printf("\t%.8g\n", $2); } + | list error '\n' { yyerrok; } + ; +expr: NUMBER + | VAR { $$ = mem[$1]; } + | VAR '=' expr { $$ = mem[$1] = $3; } + | expr '+' expr { $$ = $1 + $3; } + | expr '-' expr { $$ = $1 - $3; } + | expr '*' expr { $$ = $1 * $3; } + | expr '/' expr { + if ($3 == 0.0) + execerror("division by zero", ""); + $$ = $1 / $3; } + | '(' expr ')' { $$ = $2; } + | '-' expr %prec UNARYMINUS { $$ = -$2; } + ; +%% + /* end of grammar */ + +int lineno = 1; +#include +#include +jmp_buf begin; + +void fpecatch(int s); + +int main(int argc, char** argv) +{ + (void)setjmp(begin); + signal(SIGFPE, fpecatch); + yyparse(); +} + +int yylex() +{ + int c; + + while ((c=getchar()) == ' ' || c == '\t') + ; + if (c == EOF) + return 0; + if (c == '.' || isdigit(c)) { /* number */ + ungetc(c, stdin); + scanf("%lf", &yylval.val); + return NUMBER; + } + if (islower(c)) { + yylval.index = c - 'a'; /* ASCII only */ + return VAR; + } + if (c == '\n') + lineno++; + return c; +} + +int yyerror(const char *s) +{ + warning(s, (char *)0); + return 0; +} + +void execerror(const char* s, const char* t) +{ + warning(s, t); + longjmp(begin, 0); +} + +void fpecatch(int s) +{ + execerror("floating point exception", (char *) 0); +} + +void warning(const char* s, const char* t) +{ + fprintf(stderr, "%s", s); + if (t && *t) + fprintf(stderr, " %s", t); + fprintf(stderr, " near line %d\n", lineno); +} diff --git a/works/life/compile-principle-experiment/2/makefile b/works/life/compile-principle-experiment/2/makefile new file mode 100644 index 0000000..65bcca2 --- /dev/null +++ b/works/life/compile-principle-experiment/2/makefile @@ -0,0 +1,5 @@ +hoc: hoc.c + cc hoc.c -o hoc + +hoc.c: hoc.y + bison hoc.y -o hoc.c -- cgit v1.2.3