help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Useless nonterminals - what and why?


From: Akim Demaille
Subject: Re: Useless nonterminals - what and why?
Date: 21 Nov 2000 11:34:20 +0100
User-agent: Gnus/5.0807 (Gnus v5.8.7) XEmacs/21.1 (Channel Islands)

>>>>> "Matthew" == Matthew Palmer <address@hidden> writes:

Matthew> That, and that only.  "2 useless nonterminals and 3 unused
Matthew> rules".  The parse.output listed which rules and nonterminals
Matthew> it was, but nothing more.

Well, that's more :)

>> Your snippet has several flaws, but most probably due to the fact
>> that you cut some other relevant parts.  In particular I can't see
>> any useless nonterminal here.

Matthew> I thought I'd provided the flow of control, perhaps not.

You may have the starting point of http_proxy_list elsewhere, I don't
know.  And host was not defined.

Matthew> What fixed it, AFAICT, was placing a required colon and port
Matthew> number after the hostname, as in

Matthew> http_proxy: host COLON_TOK PORTNUM_TOK

But then you change the language!  Unless you add a rule

        http_proxy: host

you now allow host only when followed by COLON_TOK PORTNUM_TOK.

Matthew> Although at some stage I think I added the single-element
Matthew> list thingy in there, too, which may have fixed it.
Matthew> Basically I was just moving things around trying to fix it,
Matthew> and stumblerd across the solution.  I don't know what exactly
Matthew> fixed it, because I didn't keep perfect records of everything
Matthew> I did, but it now produces a valid parser, with no errors,
Matthew> and which produces the required behaviour.

Don't trust a grammar when you don't understand why it's fixed.

Basically the grammar you sent was:

----------------------------------------
%token PORTNUM_TOK COLON_TOK
%nterm http_proxy_list http_proxy host
%%
start: http_proxy_list | ;
http_proxy_list: http_proxy_list http_proxy;
http_proxy: host opt_port;
opt_port: COLON_TOK PORTNUM_TOK | ;

/tmp % LC_ALL=C bis foo.y                                        nostromo Err 1
foo.y contains 4 useless nonterminals and 5 useless rules
----------------------------------------

Just like Hans highlighted, a nonterminal defined only with

        list: list elt;

cannot be used: it cannot produce finite sentences.  Hence you can
consider http_proxy_list to be ``dead''.  But the way you cut your
grammar, you made an empty input valid, so actually the grammar above
is equivalent to:

----------------------------------------
%token PORTNUM_TOK COLON_TOK
%nterm http_proxy_list http_proxy host
%%
start: /* NOTHING */ ;
http_proxy_list: http_proxy_list http_proxy;
http_proxy: host opt_port;
opt_port: COLON_TOK PORTNUM_TOK | ;

/tmp % LC_ALL=C bis foo.y                                        nostromo 11:30
foo.y contains 4 useless nonterminals and 4 useless rules
----------------------------------------

note that start is defined to nothing.  As you can see, same error
message: although the rules were reachable from start, seems they
drive to nowhere, they've been ``simplified''.

Now, if you remove the possibility for an empty input, you get a
clearer message:

----------------------------------------
%token PORTNUM_TOK COLON_TOK
%nterm http_proxy_list http_proxy host
%%
http_proxy_list: http_proxy_list http_proxy;
http_proxy: host opt_port;
opt_port: COLON_TOK PORTNUM_TOK | ;

/tmp % LC_ALL=C bis foo.y                                        nostromo 11:30
foo.y contains 4 useless nonterminals and 4 useless rules
foo.y:7: fatal error: Start symbol http_proxy_list does not derive any sentence
----------------------------------------


Maybe we should try to extend this error message to the other
nonterminals.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]