Bison的学习笔记

1. 见文件bison.simple
....................
YYPARSE_PARAM
   Macro for specifying the name of a parameter that yyparse should accept.
  
 /* The user can define YYPARSE_PARAM as the name of an argument to be passed
    into yyparse.  The argument has type YYPARSE_PARAM_TYPE, which defaults
    to void* if you don't define it.
    It should actually point to an object.
    Grammar actions can access the variable by casting it
    to the proper pointer type, or by defining YYPARSE_PARAM_TYPE.  */
 
 #ifndef YYPARSE_PARAM_TYPE
 #define YYPARSE_PARAM_TYPE void*
 #endif

如也可
   #define YYPARSE_PARAM param
   #define YYPARSE_PARAM_TYPE void **
  
..............
YYERROR_VERBOSE
   Macro that you define with #define in the Bison declarations section
   to request verbose, specific error message strings when yyerror is called.  
  
%start symbol
Bison assumes by default that the start symbol for the grammar is the
first nonterminal specified in the grammar specification section.
The programmer may override this restriction with the %start declaration as follows:
  
...............
When you use %union to specify multiple value types, you must
declare the value type of each nonterminal symbol for which
values are used. This is done with a %type declaration, like this:
%type <type> nonterminal...

.............
Each nonterminal symbol must have grammatical rules showing how
it is made out of simpler constructs.  
By convention, it should be in lower case ( 一般小写)

token type terminal symbol :
By convention, these identifiers should be upper case to
distinguish them from nonterminals (一般大写)

Semantic Actions:动作
Locations :
When building a new location for a given grouping, the default
behavior of the output parser is to take the beginning of the
first symbol, and the end of the last symbol.

lexical analyzer :词法分析
The Bison parser calls the lexical analyzer each time it
wants a new token. It doesn't know what is "inside" the
tokens (though their semantic values may reflect this).
Typically the lexical analyzer makes the tokens by parsing
characters of text, but Bison does not depend on this.

yylex, recognizes tokens from the input stream and returns
them to the parser. Bison does not create this function
automatically; you must write it so that yyparse can call it.
The function is sometimes referred to as a lexical scanner.
yylex()函数必须自己写,bison不会自动产生( 词法函数 )
yyparse                              ( 语法函数 )
其中需要额外提供的函数:yylex()词法函数,yyerror()错误处理函数
如果当作独立程序:还需提供main();

如调试时,需定义yydebug()
 编译时: `-DYYDEBUG=1'
 或      `#define YYDEBUG 1'
 
 如果调试时没定义YYFPRINTF,YYPRINTF is defined to fprintf
 否则,调试时YYFPRINTF (stderr, format, args) --->printf
 -v 参数选项:可看具体 移进/规约

In simple programs, yylex is often defined at the end of the
Bison grammar file. If yylex is defined in a separate source
file, you need to arrange for the token-type macro definitions
to be available there. To do this, use the `-d' option when
you run Bison, so that it will write these macro definitions
into a separate header file `name.tab.h' which you can include
in the other source files that need it.

bison的文法构成.y构成
%{
C declarations // 可以包含#include ,包含库函数
%}

Bison declarations //包含terminal,nonterminal,优先级描述.....

%%
Grammar rules  // 对每个nonterminal语法规则
%%
Additional C code //yylex,额外的函数


#define YYSTYPE double
The Bison parser will use whatever type YYSTYPE is defined as;
if you don't define it, int is the default. Because we specify double,
each token and each expression has an associated value, which is a floating point number.

例子:
%{
#include <math.h>  /* For math functions, cos(), sin(), etc. */
#include "calc.h"  /* Contains definition of `symrec'        */
%}
%union {
double     val;  /* For returning numbers.                   */
symrec  *tptr;   /* For returning symbol-table pointers      */
}

%token <val>  NUM        /* Simple double precision number   */
%token <tptr> VAR FNCT   /* Variable and Function            */
%type  <val>  exp
.............
必须先用%union 第一所有的类型,再对每个类型定义
%type is used for declaring nonterminal symbols非终端字符
%token is used for declaring token types.   终端字符
多于一个数据类型的必须要%union来定义,再用%type,%token

...............
语法规则
result: components...
        ;

grammar specification section中第一个非终端字符作为起始字符
也可用下规则定义
%start symbol

栈溢出 YYMAXDEPTH(默认10000) YYINITDEPTH(默认200)