Giter Site home page Giter Site logo

flex's Introduction

## lex
#用来定义所有的模式匹配规则
#x.l x.lex, x.y x.yacc
# 注释为C注释
Lex 文件格式
%{
     第一部分 定义
%}
%%
 第二部分 规则
%%

的三部分 子程序


定义部分:
 可以用来声明函数, 变量,定义代替
digit [0-9]
letter [A-Za-z]

 在规则中可以直接使用 {digit} 来

正则元字符
" \ [] ^ - ? . * + | () $ / {} % <>


所有对元字符的引用都要使用转义字符
e.g. \* 匹配 * 本身

使用引号包含的字符串就代表他自己
\t tab
\b backspace
\n newline
\\ \

e.g. xyz"++"
   equal "xyz++"
   equal xyz/+/+

[] 中有3个元字符 \ - ^
如果要在[] 中包含字符 - 他必须是第一个字符或最后一个
 e.g. [-+0-9] matches all the gigits and the two signs

(ab|cd) 选择一个


Action
对于任何匹配的token 都会执行一个对于的action 就是执行代码,默认的是
把输入输出,一个空的action 是 ;

输出匹配到的token Lex 提供了一个yytext 的变量存放匹配到的token
[a-z]+ printf("%s", yytext);
也可使用 ECHO 来代替
[a-z]+ ECHO;

yyleng 来存放匹配token的长度
yylval 
yylineno 提供当前行数

如果action 有多个句子要使用 {} 扩起来


##Lex 内置函数
yywrap 
  当lex 读完输入文件后就会调用 yywrap 
  如果返回1 表示输入已经结束
  int yywrap(void) {return 1;}

yyerror 
  错误处理函数, void yyerror(const char*)
  通常用来输出错误,或者纠错

yyparse
  yacc 提供的函数,可以直接使用, yyparse 会自动
   调用 yylex 来取得陪个token yylex 是yylex 是Lex生成的
 
flex
 生产词法分析源码
 flex xx.lex 
 会生成一个 lex.yy.c 的文件

## yacc bison
  bison -d xx.y
  bison 的输入文件后缀名必须是y
  会生成 xx.tab.c, xx.tab.h

Yacc 的文法使用BNF的变量描述

又 Lex返回的标记只能是终结符 terminal
        
gcc xx.tab.c lex.yy.c
 生成的可执行文件 读取标准输出,然后输出的标准输出
  
自底向上 reduce 从一个表倒是简化为一个非终结符 noterminal
reduce confilict 当语法有有二意性时,及对一个表达是有多个匹配
  规则。  需要重写语法为二意性, 或者提供操作符优先级给yacc

%left 指定左结合
%right 指定右结合
最后列出的优先级高于先前列出的

yacc 的格式和lex相同,第二部分为文法定义
yacc 维护两个堆栈,1个分析站,一个内容站
分析站中保存着中介符合和非中介符合
内容站保存这这些符合对应的值
$1 代表右是的地1个成员
$$ 代表缩小后的堆栈的顶部


Flex
flex的选项影响最终生成的词法分析器的属性和行为。
这些选项可以在运行flex命令时在终端输入,也可以在.l文件中使用%option指定。

 %option noyywrap 来不生产yywarap
          yywrap 函数已经废弃了,Flex有新的方法提供多文件输入
     %option nounistd 强制flex生成的文件不包含 unistd.h 
     %option reentrant 强制生成可重入的代码
     %option yylineno 开启行号支持 
     %option nounput 指示不要使用 input 和 unput 函数
     %option bison-bridge 适配bison,生成的API能被bison 调用
     %option header-fil="xxx.h" 指定生成的头文件的名字
     %option outifle="lex.yy.c" 指定生成的文件的名字

放在 %{ %} 前面

提交提供一个 main函数,主动调用 yylex(), 和设置不使用 yywarp 可以
不用链接flex 的库

%option yylineno
 让flex 生成一个 yylineno 的变量,指定当前的行数

flex's People

Contributors

lilijreey avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.