Skip to content

Instantly share code, notes, and snippets.

@baccigalupi
Created June 23, 2011 15:42
Show Gist options
  • Save baccigalupi/1042801 to your computer and use it in GitHub Desktop.
Save baccigalupi/1042801 to your computer and use it in GitHub Desktop.
Flex File format
/*
You can add include and setup C code
that will go directly into the resulting source code.
The format for getting it in their via the Flex DSL is like so:
*/
%{
/* this goes straight into the source code! */
#include 'my_file.c'
double total = 0.0;
%}
/* regular expression helpers: they go before the first divider, %% */
digit [0-9]
number {digit}+(\.{digit}+)?(E[+-]+{digit}+)?
/* token rules - section is separated on either side by %% */
%%
// token rules take the form: regular expression {block of executable C code}
{number} { printf("%s", yytext); }
"+" { printf("+"); }
"-" { printf("-"); }
"*" { printf("*"); }
"/" { printf("/"); }
[\n] { printf("=?\n"); }
[ \t] {}
. { printf("unknow input"); }
%%
/* helper functions */
/* The helpers include anything that you would want to use in the attached code blocks for tokens.
For example, our code could get dryer if we just made one helper for all out output: */
void print_token(void) {
printf("%s", yytext);
};
// That would work for all our tokens that print something out.
// PS: yytext is something provided by Flex that holds the matched string. We can access it anywhere in our lexing code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment