User Tools

Site Tools


Lex example

lexer.l
%{
/* $Id: lexer.l,v 1.4 2016/08/14 22:43:12 root Exp root $ */
 
#include <stdio.h>
#include <stdarg.h>
#include <stdio.h>
#include <err.h>
 
%}
 
/*
%option 8bit noreentrant
%option nomain nounput noyy_top_state yywrap warn
 */
%option outfile="lexer.c"
%option nomain noyywrap reentrant
/* 
http://flex.sourceforge.net/manual/Patterns.html 
http://www.regular-expressions.info/posixbrackets.html
http://flex.sourceforge.net/manual/Start-Conditions.html
 */
 
%x COMMENT
 
%%
 
[0-9]+	{ printf("<number>%s</number>\n", yytext); }
[a-zA-Z0-9]+	{ printf("<name>%s</name>\n", yytext); }
 
[ ]+ 		{ printf("<space/>\n", yytext); }
[NL]		{ printf("</nl>\n", yytext); }
 
"\""		{ printf("<comment>", yytext); BEGIN(COMMENT); }
 
<COMMENT>[a-zA-Z0-9 ]+	{ printf("%s", yytext); }
<COMMENT>"\""	{ printf("</comment>\n", yytext); BEGIN(INITIAL); }
 
XXXXXXXXX.		printf("%s\n", yytext);
 
%%
 
char **argv;
 
int main ( int argc, char** argv )
{
 
//    ++argv, --argc;  /* skip over program name */
//    if ( argc > 0 ) {
//	    yyin = fopen( argv[0], "r" );
//    } else {
//            yyin = stdin;
//    }
 
    yyscan_t scanner;
    yylex_init(&scanner);
    yyset_in(fopen("sample.txt", "rb"), scanner);
    yylex(scanner);
    yylex_destroy(scanner);
 
    return 0;
}

Make c code

# make
flex -o lexer.c lexer.l
cc -O1  -c -o lexer.o lexer.c
cc  -o lexer lexer.o 

Input file

# cat sample.txt
some body 12345 qwerty "comment string 12345"

Output

# ./lexer
<name>some</name>
<space/>
<name>body</name>
<space/>
<number>12345</number>
<space/>
<name>qwerty</name>
<space/>
<comment>comment string 12345</comment>

First PagePrevious PageBack to overviewNext PageLast Page