Re: yacc print_grammar(): loop -> fprintf()

2018-01-03 Thread Anders Andersson
On Wed, Jan 3, 2018 at 1:58 PM, Michael W. Bombardieri  wrote:
> Hello,
>
> In yacc the function print_grammar() writes the file y.output if
> the -v option is used. fprintf() can be used directly for writing
> "spacing" space characters of indentation so a loop can be removed.
>
> - Michael
>
>
> Index: reader.c
> ===
> RCS file: /cvs/src/usr.bin/yacc/reader.c,v
> retrieving revision 1.34
> diff -u -p -u -r1.34 reader.c
> --- reader.c25 May 2017 20:11:03 -  1.34
> +++ reader.c3 Jan 2018 12:40:54 -
> @@ -1806,7 +1806,7 @@ pack_grammar(void)
>  void
>  print_grammar(void)
>  {
> -   int i, j, k;
> +   int i, k;
> int spacing = 0;
> FILE *f = verbose_file;
>
> @@ -1822,9 +1822,8 @@ print_grammar(void)
> spacing = strlen(symbol_name[rlhs[i]]) + 1;
> } else {
> fprintf(f, "%4d  ", i - 2);
> -   j = spacing;
> -   while (--j >= 0)
> -   putc(' ', f);
> +   if (spacing > 0)
> +   fprintf(f, "%*s", spacing, "");
> putc('|', f);
> }


This takes longer to understand, requires a check for a special case,
and likely ends up being slower.



yacc print_grammar(): loop -> fprintf()

2018-01-03 Thread Michael W. Bombardieri
Hello,

In yacc the function print_grammar() writes the file y.output if
the -v option is used. fprintf() can be used directly for writing
"spacing" space characters of indentation so a loop can be removed.

- Michael


Index: reader.c
===
RCS file: /cvs/src/usr.bin/yacc/reader.c,v
retrieving revision 1.34
diff -u -p -u -r1.34 reader.c
--- reader.c25 May 2017 20:11:03 -  1.34
+++ reader.c3 Jan 2018 12:40:54 -
@@ -1806,7 +1806,7 @@ pack_grammar(void)
 void
 print_grammar(void)
 {
-   int i, j, k;
+   int i, k;
int spacing = 0;
FILE *f = verbose_file;
 
@@ -1822,9 +1822,8 @@ print_grammar(void)
spacing = strlen(symbol_name[rlhs[i]]) + 1;
} else {
fprintf(f, "%4d  ", i - 2);
-   j = spacing;
-   while (--j >= 0)
-   putc(' ', f);
+   if (spacing > 0)
+   fprintf(f, "%*s", spacing, "");
putc('|', f);
}