diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-04-17 14:26:10 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-04-17 14:26:10 -0400 |
commit | 028f700584306cd465ddb18231ecc41c17418082 (patch) | |
tree | 60208e5409cdafe1af9b720d7ab7322b21b7253a /expr.l |
initial commit recognizing tokens
Diffstat (limited to 'expr.l')
-rw-r--r-- | expr.l | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -0,0 +1,68 @@ +%{ +#define _XOPEN_SOURCE 2 +#include <locale.h> +#include <string.h> +#include <stdio.h> +#include <unistd.h> +#include "expr.h" +%} + +DIGIT [0-9] + +%% + +"|" puts("or"); +"&" puts("and"); +"=" puts("equal"); +">" puts("greater"); +">=" puts("greater or equal"); +"<" puts("less"); +"<=" puts("less or equal"); +"!=" puts("not equal"); +"+" puts("plus"); +"-" puts("minus"); +"*" puts("times"); +"/" puts("divide"); +"%" puts("remainder"); +"(" puts("lparen"); +")" puts("rparen"); +{DIGIT}+ printf("number %s\n", yytext); +-{DIGIT}+ printf("negative %s\n", yytext); +.+ printf("string '%s'\n", yytext); + +%% + +int main(int argc, char *argv[]) +{ + setlocale(LC_ALL, ""); + + int c; + while ((c = getopt(argc, argv, "")) != -1) { + switch (c) { + default: + return -1; + } + } + + if (optind >= argc) { + fprintf(stderr, "expr: missing operands\n"); + return 1; + } + + size_t n = 1; + for (int i = optind; i < argc; i++) { + n += strlen(argv[i]) + 1; + } + + FILE *mem = fmemopen(NULL, n, "w+"); + for (int i = optind; i < argc; i++) { + fprintf(mem, "%s\n", argv[i]); + } + fprintf(mem, "\n"); + rewind(mem); + + yyin = mem; + + + return yylex(); +} |