Skip to content

Instantly share code, notes, and snippets.

@JALsnipe
Last active August 29, 2015 13:56
Show Gist options
  • Save JALsnipe/d984be6e903cc9d2131d to your computer and use it in GitHub Desktop.
Save JALsnipe/d984be6e903cc9d2131d to your computer and use it in GitHub Desktop.
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int longest = 0;
char *word = NULL;
%}
%option noyywrap
match ^([abctuv])+$
%%
{match} {
if(yyleng > longest) {
if(word == NULL) {
free(word);
}
longest = yyleng;
word = (char *) malloc((yyleng + 1) * sizeof(char));
strcpy(word, yytext);
}
}
\n { }
. { }
%%
int main(int argc, char *argv[])
{
yylex();
printf("Last four digits: 0282\n");
printf("Word: %s\n", word);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment