BNFC / bnfc

BNF Converter
http://bnfc.digitalgrammars.com/
586 stars 165 forks source link

Imperative backends misprint mixfix lists #358

Closed andreasabel closed 3 years ago

andreasabel commented 3 years ago

Affects backends:

For the grammar

[].  [Integer] ::= "";
(:). [Integer] ::= "-" Integer ";" [Integer];

and the input

- 1;
- 2;
- 3;

the backends C/C++/Java render the parsed syntax tree wrongly as:

1 - 2 - 3 - 

Haskell does the right job (reproducing the input), and OCaml almost, with some extra space before the semicolon:

- 1 ;
- 2 ;
- 3 ;

The imperative backends seem to assume that the only terminal in the (:) rule is the separator. https://github.com/BNFC/bnfc/blob/e91a342a99b74e4d0a2a1ad36077f51e209645ba/source/src/BNFC/CF.hs#L735-L744 (Found this bug by trying to understand getCons.)

andreasabel commented 3 years ago

Also, terminals in the nil and singleton rules are dropped:

Top.   S ::= "{" [Integer] [Char];

[].    [Integer] ::= "}";
(:).   [Integer] ::= "-" Integer ";" [Integer];

(:[]). [Char]    ::= "finally" Char ";";
(:).   [Char]    ::= "*" Char ";" [Char];

with input

{
- 1;
- 2;
- 3;
}
* 'A';
* 'B';
finally 'C';

prints

{
  1 - 2 - 3 - 'A' * 'B' * 'C' 
andreasabel commented 3 years ago

Fixed for imperative backends by making list printers recursive, instead of using for- or while-loops.