aboutsummaryrefslogtreecommitdiff
path: root/store/works/life/compile-principle-experiment/1/hoc.y
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-02-28 23:13:39 +0800
committerYuqian Yang <crupest@crupest.life>2025-02-28 23:13:39 +0800
commitdc1f0c4c0096013799416664894c5194dc7e1f52 (patch)
tree2f5d235f778cd720f4c39ec3e56b77ba6d99f375 /store/works/life/compile-principle-experiment/1/hoc.y
parent7299d424d90b1effb6db69e3476ddd5af72eeba4 (diff)
downloadcrupest-dc1f0c4c0096013799416664894c5194dc7e1f52.tar.gz
crupest-dc1f0c4c0096013799416664894c5194dc7e1f52.tar.bz2
crupest-dc1f0c4c0096013799416664894c5194dc7e1f52.zip
chore(store): move everything to store.
Diffstat (limited to 'store/works/life/compile-principle-experiment/1/hoc.y')
-rw-r--r--store/works/life/compile-principle-experiment/1/hoc.y65
1 files changed, 65 insertions, 0 deletions
diff --git a/store/works/life/compile-principle-experiment/1/hoc.y b/store/works/life/compile-principle-experiment/1/hoc.y
new file mode 100644
index 0000000..c4452ad
--- /dev/null
+++ b/store/works/life/compile-principle-experiment/1/hoc.y
@@ -0,0 +1,65 @@
+%{
+#define YYSTYPE double /* data type of yacc stack */
+#include <stdio.h>
+#include <ctype.h>
+#include <math.h>
+
+int yylex();
+int yyerror(const char * s);
+%}
+%token NUMBER
+%left '+' '-' /* left associative, same precedence */
+%left '*' '/' '%' /* left assoc., higher precedence */
+%left UNARYMINUS
+%%
+list: /* nothing */
+ | list '\n'
+ | list expr '\n' { printf("%lf\n", $2); }
+ ;
+expr: NUMBER { $$ = $1; }
+ | expr '+' expr { $$ = $1 + $3; }
+ | expr '-' expr { $$ = $1 - $3; }
+ | expr '*' expr { $$ = $1 * $3; }
+ | expr '/' expr { $$ = $1 / $3; }
+ | expr '%' expr { $$ = fmod($1, $3); }
+ | '-' expr %prec UNARYMINUS { $$ = -$2; }
+ | '(' expr ')' { $$ = $2; }
+ ;
+%%
+
+int lineno = 1;
+
+int main(int argc, char** argv)
+{
+ yyparse();
+ return 0;
+}
+
+int yylex()
+{
+ int c;
+
+ do {
+ c = getchar();
+ } while (c == ' ' || c == '\t');
+
+ if (c == EOF)
+ return 0;
+
+ if (c == '.' || isdigit(c)) {
+ ungetc(c, stdin);
+ scanf("%lf", &yylval);
+ return NUMBER;
+ }
+
+ if (c == '\n')
+ lineno++;
+
+ return c;
+}
+
+int yyerror(const char* s)
+{
+ fprintf(stderr, "Error occured near line %d\n", lineno);
+ return 0;
+}