Saturday, 15 October 2016

Definitions section

The lex definitions section may contain any of several classes of items. The most important are external definitions, #include statements, and abbreviations.

External definitions

Recall that for legal lex source this section is optional, but in most cases some of these items are necessary. External definitions have the same form and function as in C. They declare that variables globally defined elsewhere (perhaps in another source file) can be accessed in your lexical analyzer.
You may want to define a variable for use only within the action associated with one rule, for example, a loop index. You can declare such a variable to be local to a block (in this case, the action sequence) as you normally would in C--by declaring it directly after the left curly brace.

#include statements

The purpose of the #include statement is the same as in C: to include files that are important to the lexical analyzer program. An example of this usage occurs when lex is used with yacc(CP). yacc is a program generator that generates parsers, programs that analyze input to ensure that it is syntactically correct. These parsers will call a lexical analyzer. When using yacc, the file y.tab.h, generated by yacc, should be included. This file can contain definitions for token names. #include statements and variable declarations must be placed between the delimiters `%{' and `%}', for example:
   %{
   #include "y.tab.h"
   extern int tokval;
   int lineno;
   %}

Abbreviations

The definitions section can also contain abbreviations for regular expressions to be used in the rules section. The purpose of abbreviations is to avoid needless repetition in writing your specifications and to provide clarity in reading them. A definition for an ``identifier'' would often be placed in this section.
Abbreviations must appear after the '%}' delimiter that ends your #include statements and variable declarations (if there are any).
The abbreviation appears on the left of the line and its definition appears on the right, separated by one or more spaces. When abbreviations are used in rules, they must be enclosed within curly braces. The following example illustrates how abbreviations are defined and used:

   dig     [0-9]
   ident   [a-zA-Z][a-zA-z0-9]*
   %%
   -{dig}+                printf("negative integer\n");
   "+"?{dig}+             printf("positive integer\n");
   -0\.{dig}+             printf("negative real number, no whole number part\n");
   {ident}                printf("%s",yytext);

No comments:

Post a Comment