'\" te .\" Copyright 1989 AT&T .\" Copyright (c) 2009, 2011, Oracle and/or its affiliates.All rights reserved. .\" Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation.Original documentation from The Open Group can be obtained online at http://www.opengroup.org/bookstore/. .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation.In the following statement, the phrase "this text" refers to portions of the system documentation.Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document.The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.This notice shall appear on any product containing this material. .TH yacc 1 "2011 年 6 月 8 日" "SunOS 5.11" "用户命令" .SH 名称 yacc \- 用来生成编译器的编译器 .SH 用法概要 .LP .nf \fByacc\fR [\fB-dltVv\fR] [\fB-b\fR \fIfile_prefix\fR] [\fB-Q\fR [y | n]] [\fB-P\fR \fIparser\fR] [\fB-p\fR \fIsym_prefix\fR] \fIfile\fR .fi .SH 描述 .sp .LP \fByacc\fR 命令可将上下文无关的语法转换为一组表,以便简单自动地执行 LALR(1) 解析算法。语法可以不明确。可以使用指定的优先级规则消除不明确性。 .sp .LP 输出文件 \fBy.tab.c\fR 必须由 C 编译器编译才能生成函数 \fByyparse()\fR。该程序必须以词法分析器程序 \fByylex()\fR 以及 \fBmain()\fR 和错误处理例程 \fByyerror()\fR 装入。这些例程必须由用户提供。\fBlex\fR(1) 命令对于创建可供 \fByacc\fR 使用的词法分析器非常有用。 .SH 选项 .sp .LP 支持以下选项: .sp .ne 2 .mk .na \fB\fB-b\fR \fIfile_prefix\fR\fR .ad .RS 18n .rt 使用 \fIfile_prefix\fR 而非 \fBy\fR 作为所有输出文件的前缀。代码文件 \fBy.tab.c\fR、头文件 \fBy.tab.h\fR(在指定 \fB-d\fR 时创建)和说明文件 \fBy.output\fR(在指定 \fB-v\fR 时创建)将分别更改为 \fIfile_prefix\fR\fB\&.tab.c\fR、\fIfile_prefix\fR\fB\&.tab.h\fR 和 \fIfile_prefix\fR\fB\&.output\fR。 .RE .sp .ne 2 .mk .na \fB\fB-d\fR\fR .ad .RS 18n .rt 生成文件 \fBy.tab.h\fR 和将 \fByacc\fR 用户分配的“令牌代码”与用户声明的“令牌名称”关联的 \fB#define\fR 语句。通过这种关联,除 \fBy.tab.c\fR 之外的源文件可以访问令牌代码。 .RE .sp .ne 2 .mk .na \fB\fB-l\fR\fR .ad .RS 18n .rt 指定在 \fBy.tab.c\fR 中生成的代码不包含任何 \fB#line\fR 结构。只有语法和关联的操作经过完全调试后才能使用该选项。 .RE .sp .ne 2 .mk .na \fB\fB-p\fR \fIsym_prefix\fR\fR .ad .RS 18n .rt 使用 \fIsym_prefix\fR 而非 \fByy\fR 作为 \fByacc\fR 生成的所有外部名称的前缀。受影响的名称包括函数 \fByyparse()\fR、\fByylex()\fR 和 \fByyerror()\fR 以及变量 \fIyylval\fR、\fIyychar\fR 和 \fIyydebug\fR。(在本部分的其余地方,引用提及的这六个符号时使用其缺省名称只是为了表示起来方便。)本地名称也会受 \fB-p\fR 选项的影响。但是,\fB-p\fR 选项不会影响 \fByacc\fR 生成的 \fB#define\fR 符号。 .RE .sp .ne 2 .mk .na \fB\fB-P\fR \fIparser\fR\fR .ad .RS 18n .rt 使用该选项,用户可以指定自己选择的解析器而非 \fB/usr/share/lib/ccs/yaccpar\fR。例如,可以指定: .sp .in +2 .nf example% \fByacc -P ~/myparser parser.y\fR .fi .in -2 .sp .RE .sp .ne 2 .mk .na \fB\fB-Q\fR[\fBy\fR|\fBn\fR]\fR .ad .RS 18n .rt \fB-Qy\fR 选项可以将版本时间戳信息放入 \fBy.tab.c\fR。这样一来,用户便可了解构建文件的 \fByacc\fR 版本。\fB-Qn\fR 选项(缺省设置)不写入版本信息。 .RE .sp .ne 2 .mk .na \fB\fB-t\fR\fR .ad .RS 18n .rt 缺省情况下,编译运行时调试代码。运行时调试代码始终在条件编译控制下在 \fBy.tab.c\fR 中生成。缺省情况下,编译 \fBy.tab.c\fR 时不包括该代码。无论是否使用 \fB-t\fR 选项,运行时调试代码都受预处理程序符号 \fBYYDEBUG\fR 的控制。如果 \fBYYDEBUG\fR 值不为零,则包括调试代码。如果其值为 \fB0\fR,则不包括该代码。生成时不带运行时调试代码的程序大小较小且执行速度稍快。 .RE .sp .ne 2 .mk .na \fB\fB-v\fR\fR .ad .RS 18n .rt 准备文件 \fBy.output\fR,其中包含解析表的说明以及因语法不明确引起的冲突的相关报告。 .RE .sp .ne 2 .mk .na \fB\fB-V\fR\fR .ad .RS 18n .rt 在标准错误输出中列显 \fByacc\fR 的版本信息。 .RE .SH 操作数 .sp .LP 必须指定以下操作数: .sp .ne 2 .mk .na \fB\fIfile\fR\fR .ad .RS 8n .rt 文件的路径名,该文件包含要为其创建解析器的指令。 .RE .SH 示例 .LP \fB示例 1 \fR访问 yacc 库 .sp .LP \fByacc\fR 库的访问权限可以使用 \fBcc\fR 的库搜索操作数获取。要使用 \fByacc\fR 库 \fBmain\fR,请执行以下命令: .sp .in +2 .nf example% \fBcc y.tab.c -ly\fR .fi .in -2 .sp .sp .LP \fBlex\fR 库和 \fByacc\fR 库都包含 \fBmain\fR。要访问 \fByacc\fR \fBmain\fR,请执行以下命令: .sp .in +2 .nf example% \fBcc y.tab.c lex.yy.c -ly -ll\fR .fi .in -2 .sp .sp .LP 这样可以确保首先搜索 \fByacc\fR 库,以便使用其 \fBmain\fR。 .sp .LP 以前的 \fByacc\fR 库包含两个通常由应用程序程序员编码的简单函数。这两个库函数类似于以下代码: .sp .in +2 .nf #include int main(void) { extern int yyparse(); setlocale(LC_ALL, ""); /* If the following parser is one created by lex, the application must be careful to ensure that LC_CTYPE and LC_COLLATE are set to the POSIX locale. */ (void) yyparse(); return (0); } #include int yyerror(const char *msg) { (void) fprintf(stderr, "%s\en", msg); return (0); } .fi .in -2 .SH 环境变量 .sp .LP 有关影响 \fByacc\fR 执行的环境变量 \fBLANG\fR、\fBLC_ALL\fR、\fBLC_CTYPE\fR、\fBLC_MESSAGES\fR 和 \fBNLSPATH\fR 的说明,请参见 \fBenviron\fR(5)。 .sp .LP \fByacc\fR 可以将 \fBEUC\fR 主代码集和补充代码集中的字符作为一个令牌的符号进行处理。\fBEUC\fR 代码只能是带引号的单字符终端符号。对于这些一个令牌的符号,\fByacc\fR 要求 \fByylex()\fR 返回宽字符 (\fBwchar_t\fR) 值。 .SH 退出状态 .sp .LP 将返回以下退出值: .sp .ne 2 .mk .na \fB\fB0\fR\fR .ad .RS 6n .rt 成功完成。 .RE .sp .ne 2 .mk .na \fB\fB>0\fR\fR .ad .RS 6n .rt 出现错误。 .RE .SH 文件 .sp .ne 2 .mk .na \fB\fBy.output\fR\fR .ad .RS 14n .rt 生成的解析器的状态转换 .RE .sp .ne 2 .mk .na \fB\fBy.tab.c\fR\fR .ad .RS 14n .rt 生成的解析器的源代码 .RE .sp .ne 2 .mk .na \fB\fBy.tab.h\fR\fR .ad .RS 14n .rt 生成的解析器的头文件 .RE .sp .ne 2 .mk .na \fB\fByacc.acts\fR\fR .ad .RS 14n .rt 临时文件 .RE .sp .ne 2 .mk .na \fB\fByacc.debug\fR\fR .ad .RS 14n .rt 临时文件 .RE .sp .ne 2 .mk .na \fB\fByacc.tmp\fR\fR .ad .RS 14n .rt 临时文件 .RE .sp .ne 2 .mk .na \fB\fByaccpar\fR\fR .ad .RS 14n .rt C 程序的解析器原型 .RE .SH 属性 .sp .LP 有关下列属性的说明,请参见 \fBattributes\fR(5): .sp .sp .TS tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) . 属性类型属性值 _ 可用性developer/base-developer-utilities _ 接口稳定性Committed(已确定) _ 标准请参见 \fBstandards\fR(5)。 .TE .SH 另请参见 .sp .LP \fBlex\fR(1)、\fBattributes\fR(5)、\fBenviron\fR(5)、\fBstandards\fR(5) .SH 诊断 .sp .LP 归约-归约冲突和移位-归约冲突的数目在标准错误输出中报告。更详细的报告可以在 \fBy.output\fR 文件中找到。同样,如果某些规则无法从起始符号访问,也会报告该实例。 .SH 附注 .sp .LP 由于文件名固定不变,因此在给定时间给定目录中最多只能有一个 \fByacc\fR 进程处于活动状态。 .sp .LP 建议用户避免在任何标识符名称中使用 \fB$\fR。