next_inactive up previous


YaYacc: Programmers Guide
DocumentId:GradSof-yayacc-e-PG-27.03.2001-1

$Id: ProgrammingGuide_eng.tex,v 1.6 2001/05/21 10:10:24 vaa Exp $

Introduction

YaYacc - abridgment for Yet Another Yacc.

The program was created as a syntactical analyzer, which is compatible with original yacc| citeyacc ([1] - good tutorial) on algorithm analysis and perceived language LALR(1), and it generates the code by C++ language but not by C language as original yacc.

General description of work with yayacc

  1. Default of grammar's description.
  2. The generation of code
  3. Using the code: The code of class's method is situated in generated file Parser<MyGrammarTag> parse.

Thus, general situation can be expresses the following scheme:

\begin{displaymath}
\begin{array}{c c c c c}\\
\fbox{ MyGrammarTag.h, MyGramma...
...\
\fbox{ MyGrammar.y }& & & & \fbox{y.tab.cpp}\\
\end{array}\end{displaymath}

Detail examination

Let's go to detail examination of process working yayacc. So, at beginning - parameters of grammar.

grammar properties: on which are they influence

subsectionYYType

Terminal grammar type. It has semantics the same as YYTYPE in standard yacc has: i.e. MyGrammarTag::YYType is used in that place, where traditionally YYTYPE situates; YYType must obtain the following properties:

If in your comprehension the terminal symbol is the more difficult structure, for which using deep copy is not effective (for example, knot of the syntactical tree), then better to use pointers on this structure in capacity of YYType. In this case, don't forget to put the deleting elements cycle in your code renewal from mistakes.

subsubsection%union statement

If you use %union statement in file Grammar.y, then corresponding definition of type is generated automatically as concatenation of lines MyGrammarTag and YYType. Thus, for example, if you run yyac with option -n Calculator, and there is the definition of union in your Calculator.y file.

%union {
  int i;
  char ch;
};
then in work result the following lines will appear in y.tab.h and in y.h.cpp :
typedef union {
    int i;
    char ch;  
}  CalculatorYYTag;

In Calculator.h you may either duplicate this definition or include y.tab.h and write something like that:

struct Calculator
{
 typedef CalculatorYYType YYType;
 ...
};

subsectionYYInfoType

This data type, which is used for information passing between syntactical analyzer and program environment from enters analyzer.

More detail: At the time of creation the reference is passed into the class Parser on the object as type YYInfoType. You may use it for passing additional information from/into syntactical analyzer.

subsectionmaxStackSize

It must be integer constant, which define maximal size of stack of syntactical analyzer. It may be considered as top limit for maximal possible quantity of no reducible symbols. The value on default in yacc - 10000.

By the way, it is question: for what is it necessary to limit from the top the size of stack - for when use the syntactical analyzer in network services it isn't any possibility for attack as type DOS.

subsectioninitStackSize

This must be the integer constant, which define the initial size of the analyzer stack. It may be considered the top limit for quantity of no reducible symbols in current program. For grammar calculator it will be near five. Constant value in yacc on preterition - 100.

subsectionThe example: describing of the grammar tag

struct MyGrammarTag
{
 typedef int YYType;
 typedef bool YYInfoType;
 static  const int maxTableSize;
 static  const int initTableSize;
}

The interface with lexical analyzer

A lexical analyzer is passed 1 to the object YYParser when its creation.

Template for lexical analyzer is described in interface file GradSoft/YaYacc.h and has a appearance:

template<class YYTag>
class YYLexer
{
public:
  int      readToken();
  typename YYTag::YYType  getLastValue();

  YYLexer() {};
  virtual ~YYLexer() {};

private:

  YYLexer(const YYLexer&);
  YYLexer& operator=(const YYLexer&);

};

You must assign a specialization this template and /or/ inherit your lexical analyzer from it.

Two following functions are important:

The example of lexical analyzer is situated in files CalculatorLexer.h (corresponding CalculatorLexer.cpp) in subfolder demo/calculator of distribute YaYacc

The interface with the main program

The example: simple calculator

It is situated in directory demo/calculator/ in yayacc distributive.

Command line options

The distribution of YaYacc runtime in your pack

Simple transport the contents of subcatalogue interfaces in some subcatalogue of your project and set it as one more source of included files in options of your C++ compiler.

If your project installs included files, which use yayacc interfaces, then during installation create in <prefix>/include 2 subdirectory GradSoft and install there the file YaYacc.h

The list of changes

  1. 07.05.2001 - added description of getErrorMessage method.
  2. 05.05.2001 - Bibliography added.
  3. 28.04.2001 - English version was created.

Bibliography

1
John R. Levine Tony Mason and Doug Brown.
Lex & Yacc.
O'Reilly and Associates, 1992.
ISBN 1-56592-000-7.



Footnotes

... passed1
on reference
... <prefix>/include2
here prefix - installation place of your pack

next_inactive up previous
GradSoft