C++ functions used with MKS yacc 

Miscellaneous Information


typedef union or int YYSTYPE;


typedef struct {
	char	* name;
	short	type, type;
} yyNamedType;
typedef struct {
	char	* name;
	short	type;
} yyTypedRules;

char * yysvar[];
yyNamedType yyTokenTypes[];
yyTypedRules yyRules[];
long yyStates[];

class yy_parse {
	short	yystate;
	short	* stateStack, * yyps;
	YYSTYPE * valueStack, * yypv;
	YYSTYPE	yylval;
	int	yychar;
	int win_yyparse(yy_scan *);
	tracing functions, see Tracing Functions
	yy_scan	* scan;
	int	yydebug;
	int	yyntoken, yynvar, yynstate, yynrule;

	yy_parse(int sz= 150);
	yy_parse(int sz, short *states, YYSTYPE *vals);

	int yyparse(yy_scan *);
	void yyreset(void);
	void setdebug(int);
	void yyerrok(void);
	void yyclearin(void);
	int	YYRECOVERING(void);
} ;

extern int yy_scan::yylex(void);
extern int yy_scan::yyerror(char *fmt, ...);
extern YYSTYPE yylval;
extern HANDLE hInst;




is either a typedef or an int, depending on whether your grammar defines a %union or not. YACC writes the appropriate definition into a header definition file (by default, ytab.hpp).



is a preprocessor symbol that can be defined when compiling the yacc generated code with user-supplied tracing code. If defined, the symbol YYDEBUG is also defined, causing YACC debugging tables and the yydebug variable to be declared. As well, unlike YYDEBUG, when this symbol is defined no trace functions are defined; these must be provided by the user.


is a preprocessor symbol that can be defined when compiling the yacc generated code. If defined, additional YACC debugging tables are declared, and a number of virtual functions for printing YACC traces are defined.


is a preprocessing symbol that can be defined to cause yyparse() to fetch any lookahead before any user action is executed. It is usually not defined.


is a macro that can be coded in a YACC action to raise a parse error.


is a macro that can be coded in an action to cause yyparse() to return as though it had succeeded parsing.


is a macro that can be coded in an action to cause yyparse() to return as though the parse had failed.


is a macro that can be coded in an action to cause yyparse() to return the indicated value.


is defined automatically whe creating Windows resources to enable the automatic loading and unloading of the resource based parser tables. This flag should not be enabled manually.

Class yy_parse

The following objects are part of the class yy_parse, defined in the header file produced by yacc.


There are additional protected members not documented here.


has the current internal state number of the YACC generated parser. If YYDEBUG is defined, the external state number is available in yysmap[yystate].


is a pointer to an array of YYSTYPE used to save values. valueStack is a protected member of yy_parse, and is initialized in the constructor for yy_parse; see Constructors.


is a pointer to an array of shorts used to save states by yyparse(). stateStack is a protected member of yy_parse; like valueStack, it is initialized in the yy_parse constructor.

yyps, yypv 

are protected pointers to the tops of the state and value stacks, respectively.


is declared as a member variable of type YYSTYPE. It is used to hold the current value of the global ::yylval, which is filled in by yylex().


is an int variable holding the latest token read from yylex(), or the value -1 to indicate that lookahead has been cleared.


is a pointer to an object of type yy_scan , which should contain, at a minimum, member functions yylex() and yyerror(). See the mks_lexplus reference page for the definitions required. The parser generated by YACC uses a LEX-generated scanner to read input tokens. If you are not using LEX, you can define a dummy yy_scan as follows:

class yy_scan {
	int	yylex();
	void	yyerror(char *, ...);
} ;
int yy_scan::yylex() { /* token scanner */ }
void yy_scan::yyerror(char * msg, ...)
{ /* print error message */ }

is an int variable, initially set to 0. If the YACC generated code is compiled with YYDEBUG defined, then setting this variable to 1 causes yyparse() to print a trace of the parsing activity. This is useful for debugging a new grammar.

yyntoken, yynvar, yynstate, yynrule 

are constants containing the sizes of various debugging tables.


When a member of class yy_parse is defined, one of two C++ constructors can be used. The default constructor takes an optional integer argument, which defaults to 150, and allocates tables of that length for valueStack and stateStack, using the C++ new operator. This constructor also sets a flag so that the destructor for yy_scan can delete that space.

The other constructor is defined as:

yy_parse(int sz, short* states, YYSTYPE* vals);

This constructor can be used if you want to allocate the stateStack and valueStack tables yourself, for example:

short states[50];
YYSTYPE values[50];
yy_parse parse(50, states, values);	// use local tables

Your tables should all be of the length specified by sz.


The destructor for yy_parse does not attempt to delete your tables if you use this constructor.

Member Functions

int win_yyparse(yy_scan *

When generating a parser compatible in a Windows environment, yacc renames the standard parser function win_yyparse() as a protected member function, and creates another member function yyparse() as a wrapper, responsible for the loading and unloading of resource based tables. This function calls win_yyparse() if tables were loaded without error. Thus, the Windows support is in effect transparent to the user of yyparse(). See the description of yyparse() for more details. Ensure that the global variable hInst is set with the window handle of the application prior to calling yyparse() when developing a Windows application using lex and yacc.

int yyparse(yy_scan *

the parser that yacc generates. It takes a single argument, which is a pointer to an object of type yy_scan. If a user action does a return from yyparse(), it can be recalled to continue parsing from its current position. To restart a parse, call yyreset() first.

void yyreset(void

is a function that can be called to reset yy_parse(), so that the next call to yyparse() restarts a parse.

void setdebug(int

is a function taking a value that is assigned to yydebug. You can call setdebug() to start or stop debug tracing, assuming you compiled the YACC generated code with YYDEBUG defined.

void yyerrok(void

is a function that can be coded in an action to clear the yacc error condition.

void yyclearin(void

is a function that can be coded in an action to clear any lookahead tokens.


is a function that returns 1 if yyparse() is recovering from a parse error, and 0 otherwise.


The following are external to YACC, and must be defined elsewhere.

int yy_scan::yylex(void

is the lexical scanner that yyparse() calls when a new token is needed. It should return a token value, and possibly assign a value to yylval.

void yy_scan::yyerrror(char *,...) 

is used by YACC to print diagnostics. If you use LEX, the default definition of yyerror() simply prints its arguments, using vfprintf(), with output to the error stream stderr.


is the external variable that contains the token value, assigned in yylex(). It is of the type YYSTYPE, which is an int if your grammar does not have a %union, and a union typedef otherwise.


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

YACC Tracing

If YYDEBUG is defined as a non-zero value, a number of tracing variables and functions are provided. You can define YYDEBUG by using the -t option to yacc, or by compiling with YYDEBUG or YYTRACE set to a non-zero value.

Tracing Variables

char * yysvar[] 

is a table of the YACC variables (non-terminals) in the grammar.

yyNamedType yyTokenTypes[] 

is a table containing the YACC token names, and their types and token values.

yyTypedRules yyRules[] 

is a table of all the rules in the grammar, and their declared type. The rules are encoded to save space; see yyExpandName().

long yyStates[] 

is a table of offsets into the statesfile produced by YACC with the -s or -S states options. This file contains encoded state strings for the grammar; see yyExpandName().

short yyrmap[] 

contains the mapping from internal to external reduction numbers. YACC encodes numbers to save table space.

short yysmap[] 

is the counterpart to yyrmap for state numbers.

Information Functions

int yyGetType(int token

returns the type of a token, using the yyTokenTypes table.

char * yyptok(int token

returns a printable representation of a given token, using the yyTokenTypes table.

int yyExpandName(int n, int isrule, char* s, int len

looks up a state or rule number, and fills in the given string s with a printable representation of this state or rule. If isrule is set, the table yyRules is used to obtain the rule description. Otherwise, the table yyStates is used to determine the index fo the state description file states.out, which YACC generates with the -s option. This file contains encoded state information.

In either case, yyExpandName() decodes the description and fills in the given string. If the string is too short, 0 is returned and the characters "..." are appended to the string; otherwise, 1 is returned.

Tracing Functions

void yyShowState(void

prints the current state, lookahead, and number of pushed states. The current state is in yystate, and the lookahead is in yychar. If done is set when YACC resumes, the parse is terminated.

void yyShowShift(void

prints the transition via a Shift to a new state. The new state is in yystate. If done is set when YACC resumes, the parse is terminated.

void yyShowRedcue(void

prints the rule number that caused a reduction, available in rule, and the number of states to be popped, from npop. If done is set when YACC resumes, the parse is terminated.

void yyShowGoto(void

prints the transition via Goto to a new state, available in yystate. If done is set when YACC resumes, the parse is terminated.

void yyShowErrRecovery(void

prints the state YACC discards on error recovery, available in *yyps, and the new uncovered state, in *(yyps-1). If done is set when YACC resumes, the parse is terminated.

void yyShowErrDiscard(void

prints the value of the token, in yyvar, that is being discarded by the error recovery mechanism. If done is set when YACC resumes, the parse is terminated.

void yyShowRead(void

prints the token value in yyvar returned from yylex().



prototype YACC parser for C++


source files for YACC libraries


Microsoft Visual C++ library files


PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability


mks_lex, mks_yacc


YACC Programming Guide

LEX & YACC Tutorial

PTC MKS Toolkit 10.3 Documentation Build 39.