zoukankan      html  css  js  c++  java
  • Flex简介

    1.Introduction

    Flex是Fast Lexical Analyzer的简称,是词法分析器生成器。以下是摘自sourceforge的英文介绍:

    Flex is a tool for generating scanners. A scanner is a program which recognizes lexical patterns in text.

    The flex program reads the given input files, or its standard input if no input file names are given, for a 

    description of  a scanner to generate. The description is in the form of pairs of regular expressions and 

    C code, called rules. Flex generates as output a C source file, lex.yy.c by default, which defines a routine 

    yylex(). The file can be conpiled and linked with the flex runtime library to produce an executable. When 

    the executable is run, it analyses its input for occurrences of the regular expressions. Whenever it finds one,

    it executes the corresponding C code.

    2.Flex输入文件的格式

    Flex的输入文件由三个部分(section)组成,各个部分之间以%%隔开。

    definitions

    %%

    rules

    %%

    user code

    2.1 Format of the Definitions Section

    The definitions section contains declarations of simple name definitions to simplify the scanner specification, 

    and declarations of start conditions.

    Name definitions have the form: 

    name definition

    name 是以字母或者下划线开始,后面跟字母、数字、下划线或者破折号("-"),而definition部分是以紧接在name后

    的非空格字符开始,直到本行结束。definition部分可以用{name}的形式来引用,它将被扩展为(definition)。例如:

    DIGIT   [0-9]

    ID    [a-z][a-z0-9]*

    这里的DIGIT被定义为匹配单个数字的正则表达式,而ID被定义为匹配以字母开始后跟0个或者多个字符、数字的正则

    表达式。如果我们这样写:{DIGIT}+"."{DIGIT}*,那么随后它将被扩展为([0-9])+"."([0-9])*。

    不带缩进的注释部分(形如/*...*/)将被原封不动的拷贝到输出文件中(lex.yy.c)。

    任何缩进的文本或者以‘%{’和‘%}’包括起来的文本将被原封不动的拷贝到输出文件,但是符号‘%{’和‘%}’将被删除。在lex

    文件中%{和%}不能缩进。

    (未完待续)

  • 相关阅读:
    OSG学习笔记0——解决OSG读obj模型问题[转]
    Shell脚本——make命令和Makefile文件【转】
    Makefile教程(绝对经典,所有问题看这一篇足够了)【转】
    cmake 手册详解【转】
    Grafana密码恢复
    nsenter的用法
    ssh “permissions are too open” error
    CoreDns配置以及外部dns使用
    Prometheus监控系统-Alertmanager Silences静默配置
    linux下解决bash: syntax error near unexpected token `(' 的错误
  • 原文地址:https://www.cnblogs.com/miaoyong/p/3535342.html
Copyright © 2011-2022 走看看