Open System Services Shell and Utilities Reference Manual (G06.28+, H06.05+)
User Commands (k - l) lex(1)
YY_USER_ACTION
Can be redefined to provide an action that is always executed prior
to the matched pattern’s action.
YY _USER_INIT
Can be redefined to provide an action that is always executed before
the first scan.
YY_BREAK
Is used in the scanner to separate different actions. By default, it is
simply a break, but it can be redefined if necessary.
The user_functions section consists of complete C functions, which are passed directly into the
lex.y.cc output file (the effect is similar to defining the functions in separate .c files and linking
them with lex.y.cc). This section is separated from the rules section by the %% delimiter.
Comments, in C syntax, can appear anywhere in the user_functions or definitions sections. In the
rules section, comments can be embedded within actions. Empty lines or lines consisting of
white space are ignored.
The following macros are not normally called explicitly within an action, but they are used inter-
nally by the lex command to handle the input and output streams.
input( ) Reads the next character from the input stream. You cannot redefine input( ).
output() Writes the next character to the output stream.
unput(c) Puts the character c back into the input stream. It will be the next character
scanned. You cannot redefine unput().
libl.a contains default functions to support testing or quick use of a lex program without the yacc
command; these functions can be linked in through -ll. They can also be provided by the user.
main() A simple wrapper that simply calls setlocale() and then calls the yylex() func-
tion.
yywrap() The function called when the scanner reaches the end of an input stream. The
default definition simply returns 1, which causes the scanner in turn to return 0
(zero).
EXAMPLES
1. The following command processes the file lexcommands to produce the scanner file
lex.yy.c:
lex lexcommands
This is then compiled and linked by the command:
cc -oscanner lex.yy.c -ll
to produce a program scanner.
2. The scanner program converts uppercase to lowercase letters, removes spaces at the end
of a line, and replaces multiple spaces with single spaces. The lexcommands file con-
tains:
%%
[A-Z] putchar(tolower(yytext[0]));
[ ]+$
[ ]+ putchar(’ ’);
527188-007 Hewlett-Packard Company 5−57