Lex PDF
Lex PDF
Lex PDF
state machines.
*.c is generated after running
source.l
%{
< C global variables, prototypes, comments > This part will be embedded
into *.c
%}
substitutions, code and start
[DEFINITION SECTION]
states; will be copied into *.c
%%
define how to scan and what
[RULES SECTION]
action to take for each token
%%
any user code. For example,
< C auxiliary subroutines> a main function to call the
scanning function yylex().
Input specification file is divided in three parts:
Definitions: Declarations
Rules: Token Descriptions and actions
Subroutines: User-Written code
cc lex.yy.c o executable_filename
./executable_filename
%{
C declarations and includes
%}
<name> <regexp>
<name> <regexp>
%%
<regexp> { <action to take when matched> }
<regexp> { <action to take when matched> }
%%
User subroutines (C Code)
%{
%}
letter [A-Za-z]
%%
/* match letters */
{letter}+ { printf("Letter Read");}
%%
int yywrap(void) {
return 1;
}
int main(void) {
yylex();
printf("Program ends\n");
return 0;
}
Meta-characters (do not match themselves)
()[]{}<>+/,^*|.\"$?-%