Reduce/reduce issue with arrays/typedefs

7 views
Skip to first unread message

Rasmus Knutsson

unread,
May 9, 2011, 9:31:04 AM5/9/11
to Compiler construction 2011
I got a reduce/reduce conflict when I introduced arbitrarily named
types, because the parser does not know how to interpret something
like:

ident[

since this could be the beginning of a declaration of an array of
ident or an indexing into the array variable ident.

I have thought of three solutions to this, each of them with some
downside:

1) Disallowing spaces between the brackets in array declarations. Then
the rule becomes Type "[]" instead of Type "[" "]", thus avoiding the
conflict.

2) Disallowing Arrays of user-defined types, a restriction that I
think is compatible with the test cases, but a rather sad limitation
in my own view.

3) Instead of using the strings "[" "]", I defined a token relying on
the regexp ('[' ' '* ']'). Of course then I have to deal with code
that has this dangling token that I need to discard or create all the
time, and comparisons become a bit messy too. Is there a way to
include a regexp directly in the rule, or to discard tokens
automatically?

If there is any other solution to this problem (I am sure there is!) I
would greatly appreciate any hints! If not so it would be nice to get
an authoritative opinion as to which option is preferable.

Björn von Sydow

unread,
May 9, 2011, 1:50:45 PM5/9/11
to compiler-cons...@googlegroups.com
Hi!

I have a definite opinion here: I prefer alternative 1. The downside of this is, of course, that Java and C take another view, but we are designing a new language... I like to think of "[]" as one token to be read "array". This view would be destroyed by allowing whitespace inbetween.

I just don't like the look of

int [ ] a = new int[12];

but even worse is to allow (as Java and C do) e.g.

int [

] a = new int[12];

I don't see any advantage of allowing this. You may very well consider this opinion to be an excuse to avoid a difficulty; I can live with that :-).

Best regards
Björn

Rasmus Knutsson

unread,
May 9, 2011, 2:04:48 PM5/9/11
to Compiler construction 2011
Well it certainly makes my life easier, so I won't complain :)

Thanks!
Reply all
Reply to author
Forward
0 new messages