Search code examples
compiler-constructionbisonflex-lexeryacclex

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


I am trying to create a simple compiler for a formatting language using Flex and Bison. The page setup information is specified as follows in the input file:

\pagesetup{2,80}

The first integer is irrelevant to my question. The second (80) is the line width. In the output file,

  1. I want a new line to be inserted when (with this example) 80 characters have been printed on a line (counting spaces) and printing to continue on the next line.
  2. I want to be able to center-align certain lines (e.g., a title) in the output file.

In my .y file, I have this:

pageSetupProperty: BSLASH PAGESETUP LBRACE INTEGER COMMA INTEGER RBRACE;

The second integer is the one I need to use and I have set its yylval to correspond correctly to its integer value.

However, I am stuck at this point. I have searched the Bison documentation as well as SO for a line width feature but I cannot find a way to do it.


Solution

  • 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.

    And:

    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.

    Full credit goes to Brian Tompsett and EJP for providing the full answer in comments.

    Answer derived from their comments based on SE policy here.