(1) 从网站:http://www.lysator.liu.se/c/ 下载C语言的语法文件:
The ANSI C grammar ( Yacc and Lex)
(2) 编译词法文件:
(3) 编译语法文件:
说明:-d:产生头文件y.yab.h,-v:产生分析表y.output。针对else产生的移进规约冲突,采用了yacc的默认动作“移进”解决。
(4) 编译语法分析器:
(5) 测试:
编写测试程序test.c
运行:
结果如下:
发现c.y文件中没有语义动作。只是进行了词法扫描一下,所以得到结果就是上面的样子了。
(5) 贴一下y.out里的规则:
The ANSI C grammar ( Yacc and Lex)
(2) 编译词法文件:
>
lex c.l
>
yacc -dv c
.
y
(4) 编译语法分析器:
>
cc lex
.
yy
.
c y
.
tab
.
c -ll
编写测试程序test.c
#include
"
stdio.h
"
int main() {
int a = 0;
for(; a < 10; a++){
printf("hello from sun! ");
}
}
int main() {
int a = 0;
for(; a < 10; a++){
printf("hello from sun! ");
}
}
>
.
/
a.
out
<
test.c
结果如下:
"
stdio.h
"
int () {
int = ;
for(; < ; ++){
int("hello from sun hong hao! ");
}
}
int () {
int = ;
for(; < ; ++){
int("hello from sun hong hao! ");
}
}
发现c.y文件中没有语义动作。只是进行了词法扫描一下,所以得到结果就是上面的样子了。
(5) 贴一下y.out里的规则:
0
$
accept : translation_unit
$
end
1 primary_expression : IDENTIFIER
2 | CONSTANT
3 | STRING_LITERAL
4 | ' ( ' expression ' ) '
5 postfix_expression : primary_expression
6 | postfix_expression '[' expression ']'
7 | postfix_expression ' ( ' ' ) '
8 | postfix_expression ' ( ' argument_expression_list ' ) '
9 | postfix_expression ' . ' IDENTIFIER
10 | postfix_expression PTR_OP IDENTIFIER
11 | postfix_expression INC_OP
12 | postfix_expression DEC_OP
13 argument_expression_list : assignment_expression
14 | argument_expression_list ' , ' assignment_expression
15 unary_expression : postfix_expression
16 | INC_OP unary_expression
17 | DEC_OP unary_expression
18 | unary_operator cast_expression
19 | SIZEOF unary_expression
20 | SIZEOF ' ( ' type_name ' ) '
21 unary_operator : '&'
22 | '*'
23 | ' + '
24 | '-'
25 | '~'
26 | '!'
27 cast_expression : unary_expression
28 | ' ( ' type_name ' ) ' cast_expression
29 multiplicative_expression : cast_expression
30 | multiplicative_expression '*' cast_expression
31 | multiplicative_expression ' / ' cast_expression
32 | multiplicative_expression '%' cast_expression
33 additive_expression : multiplicative_expression
34 | additive_expression ' + ' multiplicative_expression
35 | additive_expression '-' multiplicative_expression
36 shift_expression : additive_expression
37 | shift_expression LEFT_OP additive_expression
38 | shift_expression RIGHT_OP additive_expression
39 relational_expression : shift_expression
40 | relational_expression ' < ' shift_expression
41 | relational_expression ' > ' shift_expression
42 | relational_expression LE_OP shift_expression
43 | relational_expression GE_OP shift_expression
44 equality_expression : relational_expression
45 | equality_expression EQ_OP relational_expression
46 | equality_expression NE_OP relational_expression
47 and_expression : equality_expression
48 | and_expression '&' equality_expression
49 exclusive_or_expression : and_expression
50 | exclusive_or_expression '^' and_expression
51 inclusive_or_expression : exclusive_or_expression
52 | inclusive_or_expression '|' exclusive_or_expression
53 logical_and_expression : inclusive_or_expression
54 | logical_and_expression AND_OP inclusive_or_expression
55 logical_or_expression : logical_and_expression
56 | logical_or_expression OR_OP logical_and_expression
57 conditional_expression : logical_or_expression
58 | logical_or_expression '?' expression ':' conditional_expression
59 assignment_expression : conditional_expression
60 | unary_expression assignment_operator assignment_expression
61 assignment_operator : ' = '
62 | MUL_ASSIGN
63 | DIV_ASSIGN
64 | MOD_ASSIGN
65 | ADD_ASSIGN
66 | SUB_ASSIGN
67 | LEFT_ASSIGN
68 | RIGHT_ASSIGN
69 | AND_ASSIGN
70 | XOR_ASSIGN
71 | OR_ASSIGN
72 expression : assignment_expression
73 | expression ' , ' assignment_expression
74 constant_expression : conditional_expression
75 declaration : declaration_specifiers ' ; '
76 | declaration_specifiers init_declarator_list ' ; '
77 declaration_specifiers : storage_class_specifier
78 | storage_class_specifier declaration_specifiers
79 | type_specifier
80 | type_specifier declaration_specifiers
81 | type_qualifier
82 | type_qualifier declaration_specifiers
83 init_declarator_list : init_declarator
84 | init_declarator_list ' , ' init_declarator
85 init_declarator : declarator
86 | declarator ' = ' initializer
87 storage_class_specifier : TYPEDEF
88 | EXTERN
89 | STATIC
90 | AUTO
91 | REGISTER
92 type_specifier : VOID
93 | CHAR
94 | SHORT
95 | INT
96 | LONG
97 | FLOAT
98 | DOUBLE
99 | SIGNED
100 | UNSIGNED
101 | struct_or_union_specifier
102 | enum_specifier
103 | TYPE_NAME
104 struct_or_union_specifier : struct_or_union IDENTIFIER '{' struct_declaration_list '}'
105 | struct_or_union '{' struct_declaration_list '}'
106 | struct_or_union IDENTIFIER
107 struct_or_union : STRUCT
108 | UNION
109 struct_declaration_list : struct_declaration
110 | struct_declaration_list struct_declaration
111 struct_declaration : specifier_qualifier_list struct_declarator_list ' ; '
112 specifier_qualifier_list : type_specifier specifier_qualifier_list
113 | type_specifier
114 | type_qualifier specifier_qualifier_list
115 | type_qualifier
116 struct_declarator_list : struct_declarator
117 | struct_declarator_list ' , ' struct_declarator
118 struct_declarator : declarator
119 | ':' constant_expression
120 | declarator ':' constant_expression
121 enum_specifier : ENUM '{' enumerator_list '}'
122 | ENUM IDENTIFIER '{' enumerator_list '}'
123 | ENUM IDENTIFIER
124 enumerator_list : enumerator
125 | enumerator_list ' , ' enumerator
126 enumerator : IDENTIFIER
127 | IDENTIFIER ' = ' constant_expression
128 type_qualifier : CONST
129 | VOLATILE
130 declarator : pointer direct_declarator
131 | direct_declarator
132 direct_declarator : IDENTIFIER
133 | ' ( ' declarator ' ) '
134 | direct_declarator '[' constant_expression ']'
135 | direct_declarator '[' ']'
136 | direct_declarator ' ( ' parameter_type_list ' ) '
137 | direct_declarator ' ( ' identifier_list ' ) '
138 | direct_declarator ' ( ' ' ) '
139 pointer : '*'
140 | '*' type_qualifier_list
141 | '*' pointer
142 | '*' type_qualifier_list pointer
143 type_qualifier_list : type_qualifier
144 | type_qualifier_list type_qualifier
145 parameter_type_list : parameter_list
146 | parameter_list ' , ' ELLIPSIS
147 parameter_list : parameter_declaration
148 | parameter_list ' , ' parameter_declaration
149 parameter_declaration : declaration_specifiers declarator
150 | declaration_specifiers abstract_declarator
151 | declaration_specifiers
152 identifier_list : IDENTIFIER
153 | identifier_list ' , ' IDENTIFIER
154 type_name : specifier_qualifier_list
155 | specifier_qualifier_list abstract_declarator
156 abstract_declarator : pointer
157 | direct_abstract_declarator
158 | pointer direct_abstract_declarator
159 direct_abstract_declarator : ' ( ' abstract_declarator ' ) '
160 | '[' ']'
161 | '[' constant_expression ']'
162 | direct_abstract_declarator '[' ']'
163 | direct_abstract_declarator '[' constant_expression ']'
164 | ' ( ' ' ) '
165 | ' ( ' parameter_type_list ' ) '
166 | direct_abstract_declarator ' ( ' ' ) '
167 | direct_abstract_declarator ' ( ' parameter_type_list ' ) '
168 initializer : assignment_expression
169 | '{' initializer_list '}'
170 | '{' initializer_list ' , ' '}'
171 initializer_list : initializer
172 | initializer_list ' , ' initializer
173 statement : labeled_statement
174 | compound_statement
175 | expression_statement
176 | selection_statement
177 | iteration_statement
178 | jump_statement
179 labeled_statement : IDENTIFIER ':' statement
180 | CASE constant_expression ':' statement
181 | DEFAULT ':' statement
182 compound_statement : '{' '}'
183 | '{' statement_list '}'
184 | '{' declaration_list '}'
185 | '{' declaration_list statement_list '}'
186 declaration_list : declaration
187 | declaration_list declaration
188 statement_list : statement
189 | statement_list statement
190 expression_statement : ' ; '
191 | expression ' ; '
192 selection_statement : IF ' ( ' expression ' ) ' statement
193 | IF ' ( ' expression ' ) ' statement ELSE statement
194 | SWITCH ' ( ' expression ' ) ' statement
195 iteration_statement : WHILE ' ( ' expression ' ) ' statement
196 | DO statement WHILE ' ( ' expression ' ) ' ' ; '
197 | FOR ' ( ' expression_statement expression_statement ' ) ' statement
198 | FOR ' ( ' expression_statement expression_statement expression ' ) ' statement
199 jump_statement : GOTO IDENTIFIER ' ; '
200 | CONTINUE ' ; '
201 | BREAK ' ; '
202 | RETURN ' ; '
203 | RETURN expression ' ; '
204 translation_unit : external_declaration
205 | translation_unit external_declaration
206 external_declaration : function_definition
207 | declaration
208 function_definition : declaration_specifiers declarator declaration_list compound_statement
209 | declaration_specifiers declarator compound_statement
210 | declarator declaration_list compound_statement
211 | declarator compound_statement
1 primary_expression : IDENTIFIER
2 | CONSTANT
3 | STRING_LITERAL
4 | ' ( ' expression ' ) '
5 postfix_expression : primary_expression
6 | postfix_expression '[' expression ']'
7 | postfix_expression ' ( ' ' ) '
8 | postfix_expression ' ( ' argument_expression_list ' ) '
9 | postfix_expression ' . ' IDENTIFIER
10 | postfix_expression PTR_OP IDENTIFIER
11 | postfix_expression INC_OP
12 | postfix_expression DEC_OP
13 argument_expression_list : assignment_expression
14 | argument_expression_list ' , ' assignment_expression
15 unary_expression : postfix_expression
16 | INC_OP unary_expression
17 | DEC_OP unary_expression
18 | unary_operator cast_expression
19 | SIZEOF unary_expression
20 | SIZEOF ' ( ' type_name ' ) '
21 unary_operator : '&'
22 | '*'
23 | ' + '
24 | '-'
25 | '~'
26 | '!'
27 cast_expression : unary_expression
28 | ' ( ' type_name ' ) ' cast_expression
29 multiplicative_expression : cast_expression
30 | multiplicative_expression '*' cast_expression
31 | multiplicative_expression ' / ' cast_expression
32 | multiplicative_expression '%' cast_expression
33 additive_expression : multiplicative_expression
34 | additive_expression ' + ' multiplicative_expression
35 | additive_expression '-' multiplicative_expression
36 shift_expression : additive_expression
37 | shift_expression LEFT_OP additive_expression
38 | shift_expression RIGHT_OP additive_expression
39 relational_expression : shift_expression
40 | relational_expression ' < ' shift_expression
41 | relational_expression ' > ' shift_expression
42 | relational_expression LE_OP shift_expression
43 | relational_expression GE_OP shift_expression
44 equality_expression : relational_expression
45 | equality_expression EQ_OP relational_expression
46 | equality_expression NE_OP relational_expression
47 and_expression : equality_expression
48 | and_expression '&' equality_expression
49 exclusive_or_expression : and_expression
50 | exclusive_or_expression '^' and_expression
51 inclusive_or_expression : exclusive_or_expression
52 | inclusive_or_expression '|' exclusive_or_expression
53 logical_and_expression : inclusive_or_expression
54 | logical_and_expression AND_OP inclusive_or_expression
55 logical_or_expression : logical_and_expression
56 | logical_or_expression OR_OP logical_and_expression
57 conditional_expression : logical_or_expression
58 | logical_or_expression '?' expression ':' conditional_expression
59 assignment_expression : conditional_expression
60 | unary_expression assignment_operator assignment_expression
61 assignment_operator : ' = '
62 | MUL_ASSIGN
63 | DIV_ASSIGN
64 | MOD_ASSIGN
65 | ADD_ASSIGN
66 | SUB_ASSIGN
67 | LEFT_ASSIGN
68 | RIGHT_ASSIGN
69 | AND_ASSIGN
70 | XOR_ASSIGN
71 | OR_ASSIGN
72 expression : assignment_expression
73 | expression ' , ' assignment_expression
74 constant_expression : conditional_expression
75 declaration : declaration_specifiers ' ; '
76 | declaration_specifiers init_declarator_list ' ; '
77 declaration_specifiers : storage_class_specifier
78 | storage_class_specifier declaration_specifiers
79 | type_specifier
80 | type_specifier declaration_specifiers
81 | type_qualifier
82 | type_qualifier declaration_specifiers
83 init_declarator_list : init_declarator
84 | init_declarator_list ' , ' init_declarator
85 init_declarator : declarator
86 | declarator ' = ' initializer
87 storage_class_specifier : TYPEDEF
88 | EXTERN
89 | STATIC
90 | AUTO
91 | REGISTER
92 type_specifier : VOID
93 | CHAR
94 | SHORT
95 | INT
96 | LONG
97 | FLOAT
98 | DOUBLE
99 | SIGNED
100 | UNSIGNED
101 | struct_or_union_specifier
102 | enum_specifier
103 | TYPE_NAME
104 struct_or_union_specifier : struct_or_union IDENTIFIER '{' struct_declaration_list '}'
105 | struct_or_union '{' struct_declaration_list '}'
106 | struct_or_union IDENTIFIER
107 struct_or_union : STRUCT
108 | UNION
109 struct_declaration_list : struct_declaration
110 | struct_declaration_list struct_declaration
111 struct_declaration : specifier_qualifier_list struct_declarator_list ' ; '
112 specifier_qualifier_list : type_specifier specifier_qualifier_list
113 | type_specifier
114 | type_qualifier specifier_qualifier_list
115 | type_qualifier
116 struct_declarator_list : struct_declarator
117 | struct_declarator_list ' , ' struct_declarator
118 struct_declarator : declarator
119 | ':' constant_expression
120 | declarator ':' constant_expression
121 enum_specifier : ENUM '{' enumerator_list '}'
122 | ENUM IDENTIFIER '{' enumerator_list '}'
123 | ENUM IDENTIFIER
124 enumerator_list : enumerator
125 | enumerator_list ' , ' enumerator
126 enumerator : IDENTIFIER
127 | IDENTIFIER ' = ' constant_expression
128 type_qualifier : CONST
129 | VOLATILE
130 declarator : pointer direct_declarator
131 | direct_declarator
132 direct_declarator : IDENTIFIER
133 | ' ( ' declarator ' ) '
134 | direct_declarator '[' constant_expression ']'
135 | direct_declarator '[' ']'
136 | direct_declarator ' ( ' parameter_type_list ' ) '
137 | direct_declarator ' ( ' identifier_list ' ) '
138 | direct_declarator ' ( ' ' ) '
139 pointer : '*'
140 | '*' type_qualifier_list
141 | '*' pointer
142 | '*' type_qualifier_list pointer
143 type_qualifier_list : type_qualifier
144 | type_qualifier_list type_qualifier
145 parameter_type_list : parameter_list
146 | parameter_list ' , ' ELLIPSIS
147 parameter_list : parameter_declaration
148 | parameter_list ' , ' parameter_declaration
149 parameter_declaration : declaration_specifiers declarator
150 | declaration_specifiers abstract_declarator
151 | declaration_specifiers
152 identifier_list : IDENTIFIER
153 | identifier_list ' , ' IDENTIFIER
154 type_name : specifier_qualifier_list
155 | specifier_qualifier_list abstract_declarator
156 abstract_declarator : pointer
157 | direct_abstract_declarator
158 | pointer direct_abstract_declarator
159 direct_abstract_declarator : ' ( ' abstract_declarator ' ) '
160 | '[' ']'
161 | '[' constant_expression ']'
162 | direct_abstract_declarator '[' ']'
163 | direct_abstract_declarator '[' constant_expression ']'
164 | ' ( ' ' ) '
165 | ' ( ' parameter_type_list ' ) '
166 | direct_abstract_declarator ' ( ' ' ) '
167 | direct_abstract_declarator ' ( ' parameter_type_list ' ) '
168 initializer : assignment_expression
169 | '{' initializer_list '}'
170 | '{' initializer_list ' , ' '}'
171 initializer_list : initializer
172 | initializer_list ' , ' initializer
173 statement : labeled_statement
174 | compound_statement
175 | expression_statement
176 | selection_statement
177 | iteration_statement
178 | jump_statement
179 labeled_statement : IDENTIFIER ':' statement
180 | CASE constant_expression ':' statement
181 | DEFAULT ':' statement
182 compound_statement : '{' '}'
183 | '{' statement_list '}'
184 | '{' declaration_list '}'
185 | '{' declaration_list statement_list '}'
186 declaration_list : declaration
187 | declaration_list declaration
188 statement_list : statement
189 | statement_list statement
190 expression_statement : ' ; '
191 | expression ' ; '
192 selection_statement : IF ' ( ' expression ' ) ' statement
193 | IF ' ( ' expression ' ) ' statement ELSE statement
194 | SWITCH ' ( ' expression ' ) ' statement
195 iteration_statement : WHILE ' ( ' expression ' ) ' statement
196 | DO statement WHILE ' ( ' expression ' ) ' ' ; '
197 | FOR ' ( ' expression_statement expression_statement ' ) ' statement
198 | FOR ' ( ' expression_statement expression_statement expression ' ) ' statement
199 jump_statement : GOTO IDENTIFIER ' ; '
200 | CONTINUE ' ; '
201 | BREAK ' ; '
202 | RETURN ' ; '
203 | RETURN expression ' ; '
204 translation_unit : external_declaration
205 | translation_unit external_declaration
206 external_declaration : function_definition
207 | declaration
208 function_definition : declaration_specifiers declarator declaration_list compound_statement
209 | declaration_specifiers declarator compound_statement
210 | declarator declaration_list compound_statement
211 | declarator compound_statement