本文章是基于一种为 small C语言的语法规则,通过lex和yacc编写归约规则,使得当输入一个合法程序的时候,输出基于归约规则和语法规则的语法树,该语法树的表现形式为缩进,同列的元素为兄弟节点,往右推进一列的元素为儿子节点。
假定small C的语法规则如下:
INT => /* integer 1*/
ID => /* identier2*/
SEMI => ;
COMMA => ,
DOT => .
BINARYOP => /* binary operators3 */
UNARYOP => /* unary operators4 */
ASSIGNOP => =
TYPE => int
LP => (
RP => )
LB => [
RB => ]
LC => {
RC => }
STRUCT => struct
RETURN ) return
IF => if
ELSE => else
BREAK => break
CONT => continue
FOR => for
1 A sequence of digits or digits followed by \0x(0X)" or \0" without spaces.
2 A character string consisting of alphabetic characters, digits and the underscore. In addition, digits can't be the rst character.
符号的优先级如下:
确