flex-bison-in-action/analyzers/cpl/cpl.l

51 lines
1.1 KiB
Plaintext

%{
#include "cpl.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int yylineno;
void yyerror(const char *s);
%}
%option noyywrap
DIGIT [0-9]
ID [a-zA-Z][a-zA-Z0-9_]*
WS [ \t]+
%%
"if" { return IF; }
"else" { return ELSE; }
"while" { return WHILE; }
"return" { return RETURN; }
"print" { return PRINT; }
"&&" { return AND; }
"||" { return OR; }
"!" { return NOT; }
">=" { return GE; }
"<=" { return LE; }
"==" { return EQ; }
"!=" { return NE; }
">" { return '>'; }
"<" { return '<'; }
"+" { return '+'; }
"-" { return '-'; }
"*" { return '*'; }
"/" { return '/'; }
"%" { return '%'; }
"(" { return '('; }
")" { return ')'; }
";" { return ';'; }
"=" { return '='; }
"{" { return '{'; }
"}" { return '}'; }
{DIGIT}+ { yylval.num = atoi(yytext); return NUMBER; }
{ID} { yylval.str = strdup(yytext); return IDENTIFIER; }
{WS} /* skip whitespace */
\n { yylineno++; }
. { yyerror("Invalid character"); }
%%