functions used with MKS lex 

Miscellaneous Information


typedef unsigned-integral yy_state_t;
typedef struct save-state YY_SAVED;

#define	YYLMAX	100

/* Variables */
#ifdef STRICT
	extern HINSTANCE hInst;
	extern HANDLE hInst;

char yytext [YYLMAX];
int yyleng;
int yylineno;
FILE *yyin = stdin;
FILE *yyout = stdout;

/* Macros */
#define output(c) putc((c), yyout)
#define yygetc()  getc(yyin)

/* Functions */
int yylex(void);

int input(void);
int unput(int c);

void yycomment(const char *delim);
void yyerror(const char *format, ...);
void yyless(int n);
void yymore();
int yymapch(int delim, int esc);
void yy_reset(void);
int yywrap(void);

YY_SAVED yySaveScan(FILE *);
void yyRestoreScan(YY_SAVED *);




is defined by LEX to be the appropriate unsigned-integral for indexing state tables. It is either unsigned char or unsigned int, depending on the size of your scanner.


is an internal data structure used to save the current state of the scanner. See the description of yySaveScan() in the Functions subsection.



defines the maximum length of tokens the LEX scanner can recognize. Its default value is 100 characters, and can be changed with the C preprocessor #undef and #define directives in the input declarations section.

External Variables


an externally declared variable that holds the window handle of the application. It is used to access the resources which contain the scanner tables when an application is developed for Microsoft Windows.



is the current input token recognized by the LEX scanner, and is accessible both within a LEX action and on return of the yylex() function. It is terminated with a null (zero) byte. If %pointer is specified in the definitions section, yytext is defined as a pointer to a pre-allocated array of char. If %array is specified in the definitions section, yytext is an array of char. This is also the default when neither %pointer nor %array are specified.


is the length of the input token in yytext.


is the current input line number, maintained by input() and yycomment().


determines the input stream for the yylex() and input() functions.


determines the output stream for the output() macro, which processes input that does not match any rules. The values of yyin and yyout can be changed by assignment.



in an action causes lex to enter a new start condition.


copies the matched input token yytext to the LEX output stream yyout.


resets yylexx() as though a newline had been seen on the input.


causes yylex() to discard the current match and examine the next possible match, if any.


can be called with a string message upon an error. The message is printed to stderr and yylex() exits with an error code of 1.


re-initializes yylex() from an unknown state. This macro can only be used in a LEX action; otherwise, use the function yy_reset().


is normally defined in the code as being equal to 1. If defined as 1, yylex() attempts to satisfy its input requirements without looking ahead past newlines, which is useful for interactive input. If YY_INTERACTIVE is defined as 0, yylex() does look past newlines; it is also slightly faster.


is normally not defined. If defined, when an expression is matched, LEX will save any pushback in yytext before calling any user action and restore this pushback after the action. This may be needed for older LEX programs that change yytext. It's not recommended, because the state saves are fairly expensive.


is called by yylex() to obtain characters. Currently, this is defined as

#define yygetc() getc(yyin)

A new version can be defined for special purposes, by first using #undef to remove the current macro definition.



the scanner that lex produces. Returns a token if it has located in the input. A negative or zero value indicates error or end of input.


returns the next character from the LEX input stream. (This means that LEX does not see it.) This function properly accounts for any look-ahead that LEX may require.

unput(int c

may be called to make the argument character c the next character to be read by input(), and hence by yylex(). The characters are placed in a push-back buffer.

yycomment(const char *delim

may be called by a translation when LEX recognizes the sequence of characters which mark the start of a comment in the given syntax. yycomment() takes a sequence of characters which mark the end of a comment, and skips over characters in the input stream until this sequence is found. Newlines found while skipping characters increment the external variable yylineno. An unexpected end-of-file produces a suitable diagnostic (using yyerror().) The following lex rules match C and shell-style comments:

"/*"	   yycomment("*/");
#.*\n   ;

Note that a LEX pattern is more efficient at recognizing a newline-terminated comment, while the yycomment() function can handle comments longer than YYLMAX.


is used by routines that generate diagnostics. A version of yyerror() is provided in the library which simply passes its arguments to vfprintf() with output to the error stream stderr. A newline is written following the message. The user may provide a replacement.

yyless(int n

shrinks the matched input in yytext to n characters. The remaining characters are rescanned by yylex().

yymapch(int delim, int esc

may be used to process C-style character constants or strings. It returns the next string character from the input, or -1 when the character delim, is reached. The usual C escapes are recognized; esc is the escape character to use: for C it would be backslash.


is a function that causes the next token to be concatenated to the current token in yytext. The current token is not rescanned.


can be called from outside a LEX action to reset the LEX scanner. This is useful when starting a scan of new input.


can be called to save the current state of yylex() and initialize the scanner to read from the given file pointer. The scanner state is saved in a newly-allocated YY_SAVED record; this record is then returned. The contents of the save block are not of interest to the caller. Instead, the save block is intended to be passed to yyRestoreScan() to reset the scanner.


restores the state of scanner after a yySaveScan() call, and frees the allocated save-block. The yySaveScan() and yyRestoreScan() functions allow an include facility to be safely defined for LEX. Here is how the save functions can be used:

include(FILE * newfp)
	void * saved;
	saved = (void *) yySaveScan(newfp);
	 * scan new file
	 * using yylex() or yyparse()

is called by yylex() when it gets EOF from yygetc(). The default version of yywrap() returns 1, which indicates no more input is available. yylex() then returns 0 indicating end of file. If the user wishes to supply more input, a yywrap() should be provided that sets up the new input (possibly by assigning a new file stream to yyin), then returns 0 to indicate that more input is available.


PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability


mks_lex, mks_yacc


LEX Programming Guide

PTC MKS Toolkit 10.3 Documentation Build 39.