distr/tokens.lex

81 lines
1.2 KiB
Plaintext
Raw Permalink Normal View History

2024-08-16 10:22:44 -04:00
%{
#include "rust.h"
#define YYSTYPE BST
#include "grammar.tab.h" // PLUS MINUS etc.
extern YYSTYPE yylval;
#include <readline/readline.h>
#include <readline/history.h>
#define YY_INPUT(buf,result,max_size) result = mygetinput(buf, max_size);
static int mygetinput(char *buf, int size) {
char *line;
if (feof(yyin))
return YY_NULL;
line = readline("> ");
if(line == NULL)
return YY_NULL;
size_t len = strnlen(line, size);
if (len > size-2)
return YY_NULL;
snprintf(buf, size, "%s\n", line);
add_history(line);
free(line);
return strlen(buf);
}
%}
%option noyywrap
white [ \t]+
sign (\+?|-)
digits [0-9]+
number {digits}(\.{digits}|e{sign}{digits})?
ident [a-zA-Z][a-zA-Z0-9_]*
%%
{white} { ; }
{number} {
yylval = number(yytext, yyleng);
return NUMBER;
}
{ident} {
yylval = variable(yytext, yyleng);
return IDENT;
}
"+" return PLUS;
"-" return MINUS;
"*" return MUL;
"/" return DIV;
"=" return ASSIGN;
"(" return LEFT;
")" return RIGHT;
"{" return LCURL;
"}" return RCURL;
"[" return LSQUARE;
"]" return RSQUARE;
"," return COMMA;
":" return COLON;
"<" return LESSER;
">" return GREATER;
"~" return SQRT;
"\n" return END;
. return SYNTAXERROR;
%%