User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

c:lex-sample [2018-08-01 15:35] (current)
Line 1: Line 1:
 +=====Lex Flex parser example======
 +
 +<code c 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;
 +}
 +</​code>​
 +
 +===Make c code===
 +
 +<​code>​
 +# make
 +flex -o lexer.c lexer.l
 +cc -O1  -c -o lexer.o lexer.c
 +cc  -o lexer lexer.o ​
 +</​code>​
 +
 +
 +===Input file===
 +<​code>​
 +# cat sample.txt
 +some body 12345 qwerty "​comment string 12345"
 +</​code>​
 +
 +===Output===
 +<​code>​
 +# ./lexer
 +<​name>​some</​name>​
 +<​space/>​
 +<​name>​body</​name>​
 +<​space/>​
 +<​number>​12345</​number>​
 +<​space/>​
 +<​name>​qwerty</​name>​
 +<​space/>​
 +<​comment>​comment string 12345</​comment>​
 +</​code>​
 +
 +
 +----
 +[<>]