词法分析生成器 之 Lexertl 【1】简介

来源:互联网 发布:arduino和单片机区别 编辑:程序博客网 时间:2024/05/16 04:50
Technorati 标签: lexertl,boost lexer,lexical analyser

1. 简介

lexertl是一个现代的词法分析生成器(lexical analyser generator)。

传统意义上的词法分析生成器(如lex)都以生成源代码作为输出,有的甚至只支持生成一种编程语言的源代码。而lexertl使用正则表达式作为输入,输出为包含多个DFA的状态机。

与flex不同的是,lexertl默认情况下不产生源代码,而是所写词法分析器可直接编译并立即执行。(也即词法分析器描述文件即C++源代码。)这就使得开发者可以快速开发词法分析器。

Lexertl试图通过导出状态机来提供更多的灵活性。包括以下两种方式:

  • 1. 在运行时构建词法分析器并且立即可用。
  •  
    • 2. 从状态机生成你所选择的编程语言的源代码。

lexertl 可以使用 GCC 4.01和 Visual C++编译。

 

2. 设计目标

1. 在功能特性上和速度上赶上甚至超过flex。

2. 支持Unicode字符编码。

3. 正则表达式语法错误抛出异常。

4. 使用 “龙书”中介绍的 DFA算法。

5. 完善支持STL。

6. 异常安全,完善支持RAII。

7. 无递归代码。

8. 线程安全的TOKEN查找函数。

9. 易读懂的源代码,适合新入门的学生。

 

 

3. 什么是词法分析器

词法分析器(lexical analyser)是将输入的文本转换成能够识别的字串的程序。

每个字串称作一个token,并且有一个 ID 来唯一区分。 通过正则表达式描述的规则来定义一个token的匹配模式。

参考: http://en.wikipedia.org/wiki/Lexical_analysis

 

4. lexertl 当前支持的正则表达式语法

Character representations

Sequence

Meaning

/a

Alert (bell).

/b

Backspace.

/e

ESC character, x1B.

/n

Newline.

/r

Carriage return.

/f

Form feed, x0C.

/t

Horizontal tab, x09.

/v

Vertical tab, x0B.

/octal

Character specified by a three-digit octal code.

/xhex

Character specified by a hexadecimal code.

/cchar

Named control character.

"..."

All characters taken as literals between double quotes, except escape sequences.

Character classes and class-like constructs

Sequence

Meaning

[...]

A single character listed or contained within a listed range.

[^...]

A single character not listed and not contained within a listed range.

.

Any character.

/d

Digit character ([0-9]).

/D

Non-digit character ([^0-9]).

/s

Whitespace character ([ /t/n/r/f/v]).

/S

Non-whitespace character ([^ /t/n/r/f/v]).

/w

Word character ([a-zA-Z0-9_]).

/W

Non-word character ([^a-zA-Z0-9_]).

Alternation and Repetition

Sequence

Meaning

...|...

Try subpatterns in alternation.

*

Match 0 or more times (greedy).

+

Match 1 or more times (greedy).

?

Match 0 or 1 times (greedy).

{n}

Match exactly n times.

{n,}

Match at least n times (greedy).

{n,m}

Match at least n times but no more than m times (greedy).

*?

Match 0 or more times (abstemious).

+?

Match 1 or more times (abstemious).

??

Match 0 or 1 times (abstemious).

{n,}?

Match at least n times (abstemious).

{n,m}?

Match at least n times but no more than m times (abstemious).

{MACRO}

Include the regex MACRO in the current regex.

Anchors

Sequence

Meaning

^

Start of string or after a newline.

$

End of string or before a newline.

 

 

5. lexertl的开发现状

lexertl的作者为 Ben hanson。 目前lexertl 仍处于开发中,可以在 http://www.benhanson.net/lexertl.html 下载到最新的版本。

6. lexertl与boost

lexertl已经被提议到(proposed)到boost,目前正处于Reivew阶段,在boost的名称为 boost.Lexer。也许将会是boost第一个正式以词法器(Lexer)命名的库。

当然,lexertl 并非徒有虚名,C++领域的一些大牛们一致的给lexertl很高的评价:

以下是Eric Niebler在一个邮件讨论列表中提到lexertl

Just to remind you, there's a very good DFA implementation underneath
Spirit Lex. The interface is not perfect (Ben has requested for some
input in that area), but the implementation is very good. Spirit Lex
is proto on top of lexertl (Ben's work). We find it to be one of the
fastest lexer, close to performance to the fastest (RE2C) which is a
code-gen that generates a humongous switch.

Eric Niebler 是greta 和 Boost Xpressive 这两个大名鼎鼎的正则表达式库的作者。

同时boost.spirit 使用 lexertl 作为 Spirit.Lex 的词法分析组件。

总体来看,boost.lexer正式发布到boost前景是光明的。