编译原理 第二章 Scanning(lexical analysis)

本书以 编译原理及实践 为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();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值