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.
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.
Code:
%{ /* Flex/Bison tutorial skeleton lexer definitions Robert Allen April 16, 2017 */ /* Include Bison generated header to import tokens */ #include "fbproject.tab.h" /* 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 */ %x COMMENT %% /* C-type multi-line comment */ "/*" {BEGIN(COMMENT);} <COMMENT>{ "*/" {BEGIN(0);} [^*\n]+ "*"[^/] \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; buf[i]=yytext[i]; for(i++;i<30;i++){ buf[i]=input(); if(buf[i]<'!' || buf[i] >'z'){ unput(buf[i]); break;} yycolumn++; yylloc.last_column++; } 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; } %%
Total Comments 0