yacc introduction
- Section 1 describes the preparation of grammar rules,
 - Section 2 the preparation of the user supplied actions associated with these rules, and
 - Section 3 the preparation of lexical analyzers.
 - Section 4 describes the operation of the parser
 
参考yacc
section1: Basic Specifications
every specification file consists of three sections: the declarations, (grammar) rules, and programs. The sections are separated by double percent %% marks. (The percent %' is generally used in Yacc specifications as an escape character.)
Names representing tokens must be declared; this is most simply done by writing
1  | %token name1 name2  | 
The parser is designed to recognize the start symbol;
1  | %start symbol  | 
The rules section is made up of one or more grammar rules. A grammar rule has the form:
1  | A : BODY ;  | 
A represents a nonterminal name, and BODY represents a sequence of zero or more names and literals. The colon and the semicolon are Yacc punctuation.
1  | A : '(' B ')'  | 
‘(‘就是一个literal
section2: Actions
With each grammar rule, the user may associate actions to be performed each time the rule is recognized in the input process.
1  | A : '(' B ')'  | 
1  | XXX : YYY ZZZ  | 
yacc采用$$$$符号来表示返回值,用$1, $2, $3...等符号来引用被同一个产生式中前面的action返回的结果。
1  | exp: '(' expr ')' { $$ = $2 ; }  |