view gen/y.lex @ 144:105037679bf8

gen: Recognize `==` atom
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 03 Sep 2019 12:14:59 +0200
parents f2ba3e8e04f7
children fe5d01fa28ee
line wrap: on
line source


%pointer
%{ // bison-bridge results in `yllval` not being a global variable but being accepted by yylex().
%}
%option reentrant noyywrap bison-bridge

%top {
#include <gen/y.tab.h>

#include <assert.h>
#include <stdio.h>
#include <string.h>
}

%{
%}


SPACE   [[:blank:]]+
COMMENT --.*\n
LEFTP   "("
RIGHTP  ")"
NUMBER  -?[1-9][0-9]*|0
ATOM    [a-z+*/=<>-][a-z0-9=-]*
QUOTE   '
STRING_LIT \"[[:alnum:][:blank:][:punct:]]*\"

%%

{SPACE}   { }
{COMMENT} { }
{LEFTP}   { return TOK_LEFTP; }
{RIGHTP}  { return TOK_RIGHTP; }
{NUMBER}  { yylval->number = atoll(yytext); return TOK_NUMBER_LITERAL; }
{ATOM}    { yylval->atom = strdup(yytext); return TOK_ATOM; }
{QUOTE}   { return TOK_QUOTE; }
{STRING_LIT}    {
    yylval->string = strndup(yytext+1, strlen(yytext)-2);
    return TOK_STRING_LITERAL;
                }
%%

// Lex input from in. Closes after finishing.
// Only used by debug_lexer.
int ylex(FILE* in) {
    yyscan_t scanner;
    assert(0 == yylex_init(&scanner));

    #ifndef DEBUG
    FILE* out = fopen("/dev/null", "w");
    #else
    FILE* out = stdout;
    #endif

    yyset_in(in, scanner);
    yyset_out(out, scanner);
    int tok;
    YYSTYPE val;
    while (0 < (tok = yylex(&val, scanner)))
        printf("token: %d text: %s\n", tok, yyget_text(scanner));
    fclose(in);
    fclose(out);
    assert(0 == yylex_destroy(scanner));
    return 0;
}