Lex Example on Ubuntu 16.04

Install Lex on Ubuntu 16.04. Lex is in flex and Yacc in bison.

sudo apt install flex bison

Create an empty file and edit it.

digit  [0-9] 
letter [A-Za-z] 
%{     
	int count; 
%} 
%%     
	/* match identifier */ 
{letter}({letter}|{digit})*      {count++; printf("number of identifiers = %d
", count);}
%% 

Save it as lol.l

lex lol.l
gcc lex.yy.c -lfl

And we can run it by typing

./a.out

An example

原文地址:https://www.cnblogs.com/cxxszz/p/8553608.html