Help answer threads with 0 replies.
Go Back > Blogs > astrogeek
User Name


Rate this Entry

Lexer: Simple Flex Template

Posted 08-19-2017 at 04:16 AM by astrogeek

Flex / Bison Project Template, Flex lexer source file.

Save this source to a file named fbproject.l in the same directory as other project template files.

This file defines a Flex based lexer which along with the Bison parser source and Makefile will allow you to quickly build a working project template with the essential features required by many projects, as described in the project README file.

/* Flex/Bison tutorial skeleton lexer definitions
        Robert Allen April 16, 2017
/* Include Bison generated header to import tokens */
#include ""

/* Using Bison locations so we define yycolumn and YY_USER_ACTION
to keep column count in sync on each pattern match */
int yycolumn=1;
#define YY_USER_ACTION yylloc.filename=filename; \
yylloc.first_line = yylloc.last_line = yylineno; \
yylloc.first_column = yycolumn; yylloc.last_column = yycolumn+yyleng-1; \
yycolumn += yyleng;

%option header-file="fbproject.lex.h"
%option yylineno
        /*%option nodefault*/

 /* If not defining our own yywrap() compile with -lfl
    for default or enable option noyywrap, negate -lfl
%option noyywrap

 /* We will define two types of comment:
 C-type multi-line
 Shell type where # is first character on line
 We handle C-type with an exclusive start state, COMMENT

 /* C-type multi-line comment */
"*/" {BEGIN(0);}
\n { yycolumn=1; }
<<EOF>> { lyyerror("%s: %i.%i: missing */: unclosed comment at end of file",yylloc.filename, yylineno, yylloc.first_column);
        return EOF; }
 /* Shell type comment */
^[ \t]*#.* {/* Do nothing*/ }

 /* Single line, double-quoted strings, strip quotes */
\"(\\.|\"\"|[^"\n])*\" { yylval.sval = strndup(yytext+1,yyleng-2); return QSTRING; }
\"(\\.|\"\"|[^"\n])*$  { lyyerror("%s: %i.%i: missing \": unclosed quote at end of line",yylloc.filename, yylineno, yylloc.first_column);
        yylval.sval = strndup(yytext+1,yyleng-1); return QSTRING; }

 /* Commands, keywords */
(?i:"q"|"qu"|"qui"|"quit") { return QUIT; }
(?i:"d"|"db"|"debug") { return DBG; }
(?i:"h"|"?"|"help") { return HELP; }

[ \t]+ {/*Eat up whitespace*/}
. { /* Anything else not recognized is an error
        This parser always recovers to EOL so we grab trailing non-sapce
        chars for user visual context */
        char buf[32] ={0};
        int i=0;
                if(buf[i]<'!' || buf[i] >'z'){
        lyyerror("%s: %i.%i-%i.%i: unrecognized input: %s", yylloc.filename, yylineno, yylloc.first_column, \
        yylineno, yylloc.last_column, buf);
        return ERRTOK; }

\n { yycolumn=1; return EOL; }

« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 07:14 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration