uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

commit 079549ee79b6f72028fe6ff8fb11c5f0df1ad9d3
parent cff7409e939aa093278339abe4fdef094d85e8fb
Author: Christos Margiolis <christos@margiolis.net>
Date:   Wed, 27 Apr 2022 01:03:25 +0300

part2

Diffstat:
Alex_bison_compilers/part2/Makefile | 7+++++++
Alex_bison_compilers/part2/input.txt | 31+++++++++++++++++++++++++++++++
Alex_bison_compilers/part2/lex.l | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alex_bison_compilers/part2/token.h | 8++++++++
4 files changed, 117 insertions(+), 0 deletions(-)

diff --git a/lex_bison_compilers/part2/Makefile b/lex_bison_compilers/part2/Makefile @@ -0,0 +1,7 @@ +all: + lex -o lex.c lex.l + cc lex.c -o lex + ./lex input.txt + +clean: + rm -f lex.c lex diff --git a/lex_bison_compilers/part2/input.txt b/lex_bison_compilers/part2/input.txt @@ -0,0 +1,31 @@ ++1234 +50 +-115 +3.14 +-10.0 ++0.0001 +3.14e-10 +0e0 +static-facts +MoveUp +CUBES +sum-1 +table +pacman +A-21-b +?x +?X +?3 +?ad +?X1b23 +?32AbC +?ABcd1234de +"" +"Test" +"Hello world" +"Mark said, \"Boo!\"" +; this is a comment +ignore whitespace +#unknown ?2 ? ?hello ?world +deffacts defrule test +2 + 2 diff --git a/lex_bison_compilers/part2/lex.l b/lex_bison_compilers/part2/lex.l @@ -0,0 +1,71 @@ +%option noyywrap + +%{ +#include <err.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#include "token.h" + +int lineno = 1; +%} + +DELIM [ \t]+ +INT 0|[+-]?[1-9]+[0-9]* +FLOAT [+-]?[0-9]+((\.[0-9]+)([eE][+-]?[0-9]*)?|([eE][+-]?[0-9]*)?) +STR \"[^\"\\]*(?:\\.[^\"\\]*)*\" +DEFIN [A-Za-z]+[A-Za-z0-9_-]* +VAR \?[A-Za-z0-9]+ +COMMENT ;.* +KEYWORD deffacts|defrule|test|bind|read|printout +OPERATOR =|\+|-|\*|\/ +UNKNOWN [^ {DELIM}\n]+ +%% + +{DELIM} { /* ignore whitespace */ } +{INT} { return TOK_INT; } +{FLOAT} { return TOK_FLOAT; } +{STR} { return TOK_STR; } +{DEFIN} { return TOK_DEFIN; } +{VAR} { return TOK_VAR; } +{COMMENT} { /* skip comments */ } +{KEYWORD} { return TOK_KEYWORD; } +{OPERATOR} { return TOK_OPERATOR; } +{UNKNOWN} { return TOK_UNKNOWN; } +\n { lineno++; } +<<EOF>> { printf("EOF\n"); exit(0); } +%% + +static char *tokens[] = { + "INT", /* integers */ + "FLOAT", /* floats */ + "STR", /* strings */ + "DEFIN", /* definitions */ + "VAR", /* variables */ + "KEYWORD", /* keywords */ + "OPERATOR", /* operators */ + "UNKNOWN", /* unknown token */ +}; + +int +main(int argc, char *argv[]) +{ + int tok; + + if (argc < 2) { + fprintf(stderr, "usage: %s input [output]\n", *argv); + return (-1); + } + if ((yyin = fopen(argv[1], "r")) == NULL) + err(1, "fopen(%s)", argv[1]); + if (argc == 3 && (yyout = fopen(argv[2], "w")) == NULL) + err(1, "fopen(%s)", argv[2]); + + while ((tok = yylex()) >= 0) { + fprintf(yyout, "line: %d\ttoken=%s\tval='%s'\n", + lineno, tokens[tok-1], yytext); + } + + return (0); +} diff --git a/lex_bison_compilers/part2/token.h b/lex_bison_compilers/part2/token.h @@ -0,0 +1,8 @@ +#define TOK_INT 1 +#define TOK_FLOAT 2 +#define TOK_STR 3 +#define TOK_DEFIN 4 +#define TOK_VAR 5 +#define TOK_KEYWORD 6 +#define TOK_OPERATOR 7 +#define TOK_UNKNOWN 8