Skip to content

Instantly share code, notes, and snippets.

@LeslieZhu
Last active January 1, 2016 15:09
Show Gist options
  • Save LeslieZhu/8162183 to your computer and use it in GitHub Desktop.
Save LeslieZhu/8162183 to your computer and use it in GitHub Desktop.
一个Flex例子,用于生成显示行号的词法分析程序。flex linno.lex; gcc lex.yy.c -o lineno; cat file.txt | ./lineno;
%option noyywrap
%{
/* a Lex program that adds line numbers
to lines of text, printing the new text
to the standard output
*/
#include <stdio.h>
int lineno = 1;
%}
line .*\n
%%
{line} {printf("%5d %s", lineno++,yytext);}
%%
int main()
{
yylex();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment