Blame demos/calc/calclex.l

Packit 5c3484
/* Lexical analyzer for calc program.
Packit 5c3484
Packit 5c3484
Copyright 2000-2002 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
This program is free software; you can redistribute it and/or modify it under
Packit 5c3484
the terms of the GNU General Public License as published by the Free Software
Packit 5c3484
Foundation; either version 3 of the License, or (at your option) any later
Packit 5c3484
version.
Packit 5c3484
Packit 5c3484
This program is distributed in the hope that it will be useful, but WITHOUT ANY
Packit 5c3484
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
Packit 5c3484
PARTICULAR PURPOSE.  See the GNU General Public License for more details.
Packit 5c3484
Packit 5c3484
You should have received a copy of the GNU General Public License along with
Packit 5c3484
this program.  If not, see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
%{
Packit 5c3484
#include <string.h>
Packit 5c3484
#include "calc-common.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
#if WITH_READLINE
Packit 5c3484
/* Let GNU flex use readline.  See the calcread.c redefined input() for a
Packit 5c3484
   way that might work for a standard lex too.  */
Packit 5c3484
#define YY_INPUT(buf,result,max_size)   \
Packit 5c3484
  result = calc_input (buf, max_size);
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* Non-zero when reading the second or subsequent line of an expression,
Packit 5c3484
   used to give a different prompt when using readline.  */
Packit 5c3484
int  calc_more_input = 0;
Packit 5c3484
Packit 5c3484
Packit 5c3484
const struct calc_keywords_t  calc_keywords[] = {
Packit 5c3484
  { "abs",       ABS },
Packit 5c3484
  { "bin",       BIN },
Packit 5c3484
  { "decimal",   DECIMAL },
Packit 5c3484
  { "fib",       FIB },
Packit 5c3484
  { "hex",       HEX },
Packit 5c3484
  { "help",      HELP },
Packit 5c3484
  { "gcd",       GCD },
Packit 5c3484
  { "kron",      KRON },
Packit 5c3484
  { "lcm",       LCM },
Packit 5c3484
  { "lucnum",    LUCNUM },
Packit 5c3484
  { "nextprime", NEXTPRIME },
Packit 5c3484
  { "powm",      POWM },
Packit 5c3484
  { "quit",      QUIT },
Packit 5c3484
  { "root",      ROOT },
Packit 5c3484
  { "sqrt",      SQRT },
Packit 5c3484
  { NULL }
Packit 5c3484
};
Packit 5c3484
%}
Packit 5c3484
Packit 5c3484
%%
Packit 5c3484
Packit 5c3484
[ \t\f] { /* white space is skipped */ }
Packit 5c3484
Packit 5c3484
[;\n]   { /* semicolon or newline separates statements */
Packit 5c3484
          calc_more_input = 0;
Packit 5c3484
          return EOS; }
Packit 5c3484
\\\n    { /* escaped newlines are skipped */ }
Packit 5c3484
Packit 5c3484
Packit 5c3484
#(([^\\\n]*)\\)+\n {
Packit 5c3484
            /* comment through to escaped newline is skipped */ }
Packit 5c3484
#[^\n]*\n { /* comment through to newline is a separator */
Packit 5c3484
            calc_more_input = 0;
Packit 5c3484
            return EOS; }
Packit 5c3484
#[^\n]* {   /* comment through to EOF skipped */ }
Packit 5c3484
Packit 5c3484
Packit 5c3484
[-+*/%()<>^!=,] { return yytext[0]; }
Packit 5c3484
"<="    { return LE; }
Packit 5c3484
">="    { return GE; }
Packit 5c3484
"=="    { return EQ; }
Packit 5c3484
"!="    { return NE; }
Packit 5c3484
"<<"    { return LSHIFT; }
Packit 5c3484
">>"    { return RSHIFT; }
Packit 5c3484
"&&"    { return LAND; }
Packit 5c3484
"||"    { return LOR; }
Packit 5c3484
Packit 5c3484
(0[xX])?[0-9A-F]+ {
Packit 5c3484
        yylval.str = yytext;
Packit 5c3484
        return NUMBER; }
Packit 5c3484
Packit 5c3484
[a-zA-Z][a-zA-Z0-9]* {
Packit 5c3484
        int  i;
Packit 5c3484
Packit 5c3484
        for (i = 0; calc_keywords[i].name != NULL; i++)
Packit 5c3484
          if (strcmp (yytext, calc_keywords[i].name) == 0)
Packit 5c3484
            return calc_keywords[i].value;
Packit 5c3484
Packit 5c3484
        if (yytext[0] >= 'a' && yytext[0] <= 'z' && yytext[1] == '\0')
Packit 5c3484
          {
Packit 5c3484
            yylval.var = yytext[0] - 'a';
Packit 5c3484
            return VARIABLE;
Packit 5c3484
          }
Packit 5c3484
Packit 5c3484
        return BAD;
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
. { return BAD; }
Packit 5c3484
Packit 5c3484
%%
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
yywrap ()
Packit 5c3484
{
Packit 5c3484
  return 1;
Packit 5c3484
}