Sponsored Links
-->

Wednesday, July 4, 2018

1.3.1 تصميم مترجم لغة برمجة (compiler) بلغة C : عن Lexical ...
src: i.ytimg.com

Flex (fast lexical analyzer generator) is a free and open-source software alternative to lex. It is a computer program that generates lexical analyzers (also known as "scanners" or "lexers"). It is frequently used as the lex implementation together with Berkeley Yacc parser generator on BSD-derived operating systems (as both lex and yacc are part of POSIX), or together with GNU bison (a version of yacc) in *BSD ports and in Linux distributions. Unlike Bison, flex is not part of the GNU Project and is not released under the GNU General Public License.


Video Flex (lexical analyser generator)



History

Flex was written in C by Vern Paxson around 1987. He was translating a Ratfor generator, which had been led by Jef Poskanzer.


Maps Flex (lexical analyser generator)



Example lexical analyzer

This is an example of a Flex scanner for the instructional programming language PL/0.

The tokens recognized are: '+', '-', '*', '/', '=', '(', ')', ',', ';', '.', ':=', '<', '<=', '<>', '>', '>='; numbers: 0-9 {0-9}; identifiers: a-zA-Z {a-zA-Z0-9} and keywords: begin, call, const, do, end, if, odd, procedure, then, var, while.


Setup and Run Lex and YACC code in Windows (Flex, Bison) - YouTube
src: i.ytimg.com


Internals

These programs perform character parsing and tokenizing via the use of a deterministic finite automaton (DFA). A DFA is a theoretical machine accepting regular languages. These machines are a subset of the collection of Turing machines. DFAs are equivalent to read-only right moving Turing machines. The syntax is based on the use of regular expressions. See also nondeterministic finite automaton.


Chapter 3 Lexical Analysis. - ppt download
src: slideplayer.com

Issues

Time complexity

A Flex lexical analyzer usually has time complexity O ( n ) {\displaystyle O(n)} in the length of the input. That is, it performs a constant number of operations for each input symbol. This constant is quite low: GCC generates 12 instructions for the DFA match loop. Note that the constant is independent of the length of the token, the length of the regular expression and the size of the DFA.

However, using the REJECT macro in a scanner with the potential to match extremely long tokens can cause Flex to generate a scanner with non-linear performance. This feature is optional. In this case, the programmer has explicitly told Flex to "go back and try again" after it has already matched some input. This will cause the DFA to backtrack to find other accept states. The REJECT feature is not enabled by default, and because of its performance implications its use is discouraged in the Flex manual.

Reentrancy

By default the scanner generated by Flex is not reentrant. This can cause serious problems for programs that use the generated scanner from different threads. To overcome this issue there are options that Flex provides in order to achieve reentrancy. A detailed description of these options can be found in the Flex manual.

Usage under non-Unix environments

Normally the generated scanner contains references to unistd.h header file which is Unix specific. To avoid generating code that includes unistd.h, %option nounistd should be used. Another issue is the call to isatty (a Unix library function), which can be found in the generated code. The %option never-interactive forces flex to generate code that doesn't use isatty.

Using flex from other languages

Flex can only generate code for C and C++. To use the scanner code generated by flex from other languages a language binding tool such as SWIG can be used.


Introduction to Lex / Flex Programming - YouTube
src: i.ytimg.com


Flex++

flex++ is a similar lexical scanner for C++ which is included as part of the flex package. The generated code does not depend on any runtime or external library except for a memory allocator (malloc or a user-supplied alternative) unless the input also depends on it. This can be useful in embedded and similar situations where traditional operating system or C runtime facilities may not be available.

The flex++ generated C++ scanner includes the header file FlexLexer.h, which defines the interfaces of the two C++ generated classes.


Chapter 3 Lexical Analysis. - ppt download
src: slideplayer.com


See also

  • Comparison of parser generators
  • Lex
  • yacc
  • GNU Bison
  • Berkeley Yacc

Part 02: Tutorial on lex/yacc. - YouTube
src: i.ytimg.com


References


Chapter 3 Lexical Analysis. - ppt download
src: slideplayer.com


Further reading

  • Levine, John (August 2009). flex & bison. O'Reilly Media. ISBN 978-0-596-15597-1. 
  • M. E. Lesk and E. Schmidt, LEX - Lexical Analyzer Generator
  • Alfred Aho, Ravi Sethi and Jeffrey Ullman, Compilers: Principles, Techniques and Tools, Addison-Wesley (1986). Describes the pattern-matching techniques used by flex (deterministic finite automata)

Simple Lexing in C using Flex - YouTube
src: i.ytimg.com


External links

  • Official website
  • ANSI-C Lex Specification
  • JFlex: Fast Scanner Generator for Java
  • Brief description of Lex, Flex, YACC, and Bison

Source of the article : Wikipedia

Comments
0 Comments