如何使用 Flex 和 Bison (Lex/Yacc) 设置输出文件中的线宽?

How to use Flex and Bison (Lex/Yacc) to set line width in the output file?

我正在尝试使用 Flex 和 Bison 为格式化语言创建一个简单的编译器。页面设置信息在输入文件中指定如下:

\pagesetup{2,80}

第一个整数与我的问题无关。第二个 (80) 是线宽。在输出文件中,

  1. 我希望在一行打印 80 个字符(计算空格)时插入一个新行并在下一行继续打印。
  2. 我希望能够 center-align 输出文件中的某些行(例如标题)。

在我的 .y 文件中,我有这个:

pageSetupProperty: BSLASH PAGESETUP LBRACE INTEGER COMMA INTEGER RBRACE;

第二个整数是我需要使用的,我已将其 yylval 设置为正确对应其整数值。

然而,我卡在了这一点上。我已经在 Bison 文档和 SO 中搜索了线宽功能,但找不到方法。

It doesn't need a bison feature. Everything output by your parser is output by you in your actions written in C. (Bison does not output anything or have anything to do with output). Just keep count of how long those strings are yourself. It is just a coding problem and nothing to do with bison at all.

并且:

There is no line width feature. Neither flex nor bison produces an output file at all, let alone one with a line width feature. Producing output is up to you, in whatever code you write in your production actions, as is its line width.

完全归功于 Brian Tompsett 和 EJP,他们在评论中提供了完整的答案。

根据 SE 政策 here.

从他们的评论中得出的答案