Flex Assign
Flex Assign
What does Flex do? It takes as its input a text le containing regular expressions, together with the action to be taken when each expression is matched. It produces an output le that contains C source code dening a function yylexthat is a table-driven implementation of a DFA corresponding to the regular expressions of the input le. The Flex output le is then compiled with a C compiler to get an executable. 2. The format of a Flex source le As shown below, a lexical specication le for Flex consists of three parts divided by a single line starting with %%: Definitions %% Rules %% User Code In all parts of the specication comments of the form /* comment text */ are permitted. Denitions The denition section occurs before the rst %%. It contains two things. First, any C code that must be inserted external to any function should appear in this section between the delimiters %{ and %}. Secondly, the denitions section contains declarations of simple name denitions to simplify the scanner specication, and declarations of start conditions. (For a discussion of start conditions, see the Flex Manual ([1]), pages 13 - 18. ) Name denitions have the form: name definition The name is a word beginning with a letter or an underscore ( ) followed by zero or more letters, digits, , or - (dash). The denition is taken to begin at the rst non-white-space character following the name and continuing to the end of the line. The denition can subsequently be referred to by using name, which will expand to (denition). For example, DIGIT [0-9] ID [a-z][a-z0-9]* denes DIGIT to be a regular expression which matches a single digit, and ID to be a regular expression which matches a letter followed by zero-ormore letters-or-digits. Rules
The lexical rules section of a Flex specication contains a set of regular expressions and actions (C code) that are executed when the scanner matches the associated regular expression. It is of the form: pattern action where the pattern must be unindented and the action must begin on the same line.