词法分析器

lex词法分析器用于消除二义性的两条规则

There are two important disambiguation rules used by Lex and other similar lexical-analyzer generators:

  • Longest match: The longest initial substring of the input that can
    match any regular expression is taken as the next token.
  • Rule priority: For a particular longest initial substring, the first
    regular expression that can match determines its token type. This
    means that the order of writing down the regular-expression rules has
    significance.
lex是基于DFA 实现的

DFA construction is a mechanical task easily performed by computer, so it
makes sense to have an automatic lexical analyzer generator to translate regular expressions into a DFA.

lex的输出是C程序

Lex is a lexical analyzer generator that produces a C program from a lexical specification. For each token type in the programming language to be lexically analyzed, the specification contains a regular expression and an action. The action communicates the token type (perhaps along with other information) to the next phase of the compiler.
The output of Lex is a program in C – a lexical analyzerexecutes the action
fragments on each match. The action fragments are just C statements that
return token values.

lex的前世今生

Lex was the first lexical-analyzer generator based on regular expressions
[Lesk 1975]; it is still widely used.

DFA transition tables can be very large and sparse. If represented as a simple two-dimensional matrix (states × symbols) they take far too much memory. In practice, tables are compressed; this reduces the amount of memory
required, but increases the time required to look up the next state [Aho et al.
1986].

flex 比lex快,case语句执行效率相当快。flex和bison已被证明比原来的Unix工具lex yacc更可靠、更强大、更快

Automatically generated lexical analyzers are often criticized for being
slow. In principle, the operation of a finite automaton is very simple and
should be efficient, but interpreting from transition tables adds overhead.
Gray [1988] shows that DFAs translated directly into executable code (implementing states as case statements) can run as fast as hand-coded lexers. The
Flex “fast lexical analyzer generator” [Paxson 1995] is significantly faster
than Lex.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值