Difference between revisions of "The Flex Lexical Analyzer"

From Wiki**3

(See Also)
(Exercises)
Line 35: Line 35:
 
== Exercises ==
 
== Exercises ==
  
* [[The Flex Lexical Analyzer/Exercise 1|Exercise 1]]
+
* [[The Flex Lexical Analyzer/Exercise 1|Exercise 1]] - Printing the strings present in a C/C++ program.
* [[The Flex Lexical Analyzer/Exercise 2|Exercise 2]]
+
* [[The Flex Lexical Analyzer/Exercise 2|Exercise 2]] - Printing the number of times "." and "->" are used as operators.
* [[The Flex Lexical Analyzer/Exercise 3|Exercise 3]]
+
* [[The Flex Lexical Analyzer/Exercise 3|Exercise 3]] - Printing the comments present in a C/C++ program.
 +
* [[The Flex Lexical Analyzer/Exercise 4|Exercise 4]] - Removing the actions from a YACC specification.
 +
* [[The Flex Lexical Analyzer/Exercise 4|Exercise 4]] - Removing the actions from a Flex specification.
  
 
[[category:Compilers]]
 
[[category:Compilers]]
 
[[category:Teaching]]
 
[[category:Teaching]]

Revision as of 21:24, 11 April 2010

Basic Concepts

  • Definitions
  • Rules, actions
  • Code
  • States and sub-automata

Structure of a Flex Specification

How to Debug a Flex Specification

There are various flags and variables to activate the debug functionality in Flex (there is no need to insert useless code such as printfs or similar).

In the Flex specification file:

%option debug

This flag suffices when developing in C. When using C++ scanners (%option c++), even though the above flag still generates debug code, it's still necessary to tell the scanner to actually output debug information. This can be done by calling the set_debug method with a non-zero argument (this can be done conveniently at the start of the rules section in a Flex specification file -- note how the action is indented from the left and without any rule):

 %%
                        { set_debug(1); }

Also, the YYDEBUG environment variable will activate the debug messages both in Flex and YACC (allowing simultaneous debugging of token recognition by the scanner and corresponding use by the parser). In the command line (syntax may vary, depending on the actual environment definition circumstances):

 export YYDEBUG=1

See Also

Examples

Exercises

  • Exercise 1 - Printing the strings present in a C/C++ program.
  • Exercise 2 - Printing the number of times "." and "->" are used as operators.
  • Exercise 3 - Printing the comments present in a C/C++ program.
  • Exercise 4 - Removing the actions from a YACC specification.
  • Exercise 4 - Removing the actions from a Flex specification.