0% found this document useful (0 votes)
20 views

Flex Assign

This document describes the format and components of a Flex source file used for lexical analysis. A Flex source file consists of three parts: definitions, rules, and user code. The definitions section contains C code and name definitions. The rules section contains regular expressions paired with actions. Flex takes this input file and generates a C file containing a scanner function that implements a DFA to recognize tokens based on the regular expressions.

Uploaded by

revolutionisme
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Flex Assign

This document describes the format and components of a Flex source file used for lexical analysis. A Flex source file consists of three parts: definitions, rules, and user code. The definitions section contains C code and name definitions. The rules section contains regular expressions paired with actions. Flex takes this input file and generates a C file containing a scanner function that implements a DFA to recognize tokens based on the regular expressions.

Uploaded by

revolutionisme
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Introduction What is Flex? Flex takes a set of descriptions of possible tokens and produces a scanner.

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.

You might also like