Created
June 23, 2011 16:23
-
-
Save baccigalupi/1042901 to your computer and use it in GitHub Desktop.
Flex File 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%option noyywrap | |
%{ | |
/* Lexer example using a overdone c++ class for managing both tokens and logic */ | |
#include "1_simple_calculator.cpp" | |
using namespace simple_calculator; | |
Queue token_queue; | |
Token token; | |
%} | |
/* regular definitions */ | |
digit [0-9] | |
number {digit}+(\.{digit}+)?(E[+-]+{digit}+)? | |
/* token rules */ | |
%% | |
{number} { printf("%s", yytext); token = Token(SC_NUMBER, yytext); token_queue.append(token); } | |
"+" { printf("+"); token = Token(SC_PLUS); token_queue.append(token); } | |
"-" { printf("-"); token = Token(SC_MINUS); token_queue.append(token); } | |
"*" { printf("*"); token = Token(SC_TIMES); token_queue.append(token);} | |
"/" { printf("/"); token = Token(SC_DIVIDE); token_queue.append(token);} | |
[\n] { printf("=\n"); printf("%f\n\n", token_queue.calculate()); } | |
[ \t] {} | |
. { printf("unknow input"); } | |
%% | |
/* helper functions */ | |
int main(void) { | |
yylex(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment