本书以 编译原理及实践 为textbook
Chapter Two. Scanning(lexical analysis)
regular expression
Finite Automata
NFA
DFA
miniDFA
对miniDFA中的结果,可以知道miniDFA中的states in the set,其对某一个character的transition的结果(指向同一个set)相同。
algorithm:先分类为set of accepting states,set of non-accepting states。对the set of states,如果对一个character的transition结果(包括error,即无transition)不同,则将其分类,直至对每一个set of states,其每一个character的transition的结果都相同。
flex语法
{definitions}
%%
{rules}
%%
{auxiliary routines}
{definitions}包括相应语言的程序(C code(变量和include))需要由%{、%}包括和正则表达式的命名
{rules}包括正则表达式语法和相应的程序(C code)
{auxiliary routines}包括程序(C code)
例一:计算输入文本的字符数、单词数、行数
%option noyywrap
%{
#include<stdio.h>
#include<stdlib.h>
int NumberOfChar=0;
int NumberOfWord=0;
int NumberOfLine=0;
%}
Word [a-zA-z]+
Line \n
Char .
%%
{Word} {NumberOfWord++;NumberOfChar+=strlen(yytext);}
{Line} {NumberOfLine++;NumberOfChar++;}
{Char} {NumberOfChar++;}
%%
int main()
{
char filename[50];
printf("Input the name of the file:");
scanf("%s",filename);
yyin=fopen(filename, "r");
yyout=fopen("output.txt","w");
if (yyin==NULL)
{
fprintf(yyout,"Fail to open file!\n");
exit(0);
}
yylex();
fprintf(yyout,"NumberOfChar=%d\nNumberOfWord=%d\nNumberOfLine=%d\n",NumberOfChar,NumberOfWord,NumberOfLine);
}
例二:使文本的关键词大写
%option noyywrap
%{
#include<stdio.h>
#include<stdlib.h>
int pos=0;
%}
Comment [/][*]([^\*]|([*])*[^\*/])*([*])*[*][/]
KeyWords atuo|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while
%%
{Comment} {fprintf(yyout,"%s",yytext);}
(" "|"\n"){KeyWords}(" "|"\n") {int i;fprintf(yyout,"%c",yytext[0]);for (i=1;i<strlen(yytext)-1;i++) fprintf(yyout,"%c",yytext[i]+65-97);unput(yytext[strlen(yytext)-1]);}
{KeyWords}(" "|"\n") {if (pos!=0) fprintf(yyout,yytext);else {int i;for (i=0;i<strlen(yytext)-1;i++) fprintf(yyout,"%c",yytext[i]+65-97);unput(yytext[strlen(yytext)-1]);}}
[^ \n]* {fprintf(yyout,yytext);}
(" "|"\n") {fprintf(yyout,yytext);}
%%
int main()
{
char filename[50];
printf("Input the name of the file:");
scanf("%s",filename);
yyin=fopen(filename, "r");
yyout=fopen("output.txt","w");
if (yyin==NULL)
{
fprintf(yyout,"Fail to open file!\n");
exit(0);
}
yylex();
}