Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #2666 > unrolled thread

About finding the start symbol of a grammar

Started byEduardo Costa <ecosta.tmp@gmail.com>
First post2021-05-21 03:49 -0700
Last post2021-05-22 06:52 +0300
Articles 5 — 5 participants

Back to article view | Back to comp.compilers


Contents

  About finding the start symbol of a grammar Eduardo Costa <ecosta.tmp@gmail.com> - 2021-05-21 03:49 -0700
    Re: About finding the start symbol of a grammar Kaz Kylheku <563-365-8930@kylheku.com> - 2021-05-21 14:14 +0000
      Re: About finding the start symbol of a grammar anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2021-05-21 15:32 +0000
    Re: About finding the start symbol of a grammar Hans-Peter Diettrich <DrDiettrich1@netscape.net> - 2021-05-21 17:02 +0200
    Re: About finding the start symbol of a grammar "Ev. Drikos" <drikosev@gmail.com> - 2021-05-22 06:52 +0300

#2666 — About finding the start symbol of a grammar

FromEduardo Costa <ecosta.tmp@gmail.com>
Date2021-05-21 03:49 -0700
SubjectAbout finding the start symbol of a grammar
Message-ID<21-05-015@comp.compilers>
Hey guys,

I've been lately dealing with a parser generator for LL grammars, and since
it's inception I've always been blindy assuming the first element read from
within the input file is going to be the start symbol or starting rule.

So I've been wondering all this time, just out of curiosity, if there exists a
method or algorithm to find out the start symbol of a given grammar?

I guess the answer is no.

While there would exist grammars we could recursively check to find out which
it's start symbol is (i.e.: it's the only rule that used the rest of them,
where checking every other resulted in dangling rules that weren't even called
in), there might be other grammars for which more than one rule yields full
coverage (all of these obviously defining different languages) and so leading
to ambiguity.

I only contemplate a simple coverage test, even though other techniques could
exist, again, all of them leading to a point where we couldn't ascertain if
one or the other is what the user meant.

So I'm wondering if this is even an issue in production-grade
parser-generators out there?

Regards,
[yacc and its descendants have an explicit %start declaration, usually defaulting to
the first rule in the file. -John]

[toc] | [next] | [standalone]


#2667

FromKaz Kylheku <563-365-8930@kylheku.com>
Date2021-05-21 14:14 +0000
Message-ID<21-05-016@comp.compilers>
In reply to#2666
On 2021-05-21, Eduardo Costa <ecosta.tmp@gmail.com> wrote:
> Hey guys,
>
> I've been lately dealing with a parser generator for LL grammars, and since
> it's inception I've always been blindy assuming the first element read from
> within the input file is going to be the start symbol or starting rule.
>
> So I've been wondering all this time, just out of curiosity, if there exists a
> method or algorithm to find out the start symbol of a given grammar?
>
> I guess the answer is no.

Surely, the start symbol of a context-free grammar is one which appears
only in the left hand side of a rule. If there is such a unique symbol,
it must be /the/ start symbol.

> While there would exist grammars we could recursively check to find out which
> it's start symbol is (i.e.: it's the only rule that used the rest of them,
> where checking every other resulted in dangling rules that weren't even called
> in), there might be other grammars for which more than one rule yields full
> coverage (all of these obviously defining different languages) and so leading
> to ambiguity.

Ambiguity doesn't imply there is no algorithm to find a start symbol,
but that the algorithm must be able to report situations like the
presence of multiple start symbols, or no start symbols.

On the face of it, this problem does not smell of undecidability, or
even NP completeness. Where do you suspect is the difficulty?

It seems like this is a fairly trivial property of a graph, type of
thing.

Whether rules are dangling is also a graph property: is the graph
connected.

> I only contemplate a simple coverage test, even though other techniques could
> exist, again, all of them leading to a point where we couldn't ascertain if
> one or the other is what the user meant.

But tha seems like an identifiable point where the algorithm can stop
and announce a decision. Then diagnostics can be issued.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
[I have seen useful grammars where the start symbol also appears in the RHS of a rule.
Think of the standard expression grammar.

You pick the start symbol that gives you the language you want to parse. -John]

[toc] | [prev] | [next] | [standalone]


#2669

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2021-05-21 15:32 +0000
Message-ID<21-05-018@comp.compilers>
In reply to#2667
Kaz Kylheku <563-365-8930@kylheku.com> writes:
>Surely, the start symbol of a context-free grammar is one which appears
>only in the left hand side of a rule. If there is such a unique symbol,
>it must be /the/ start symbol.

It could be a now-unused nonterminal, while the start symbol is part
of a strongly connected component of the graph.

If you have one nonterminal that dominates all other nonterminals (the
other nonterminals can be derived from it, but not the other way
round), it probably is the start symbol.  Why "probably"? There is
still the possibility that there is a wrapper rule around the real
start symbol that was used for debugging and is left in the grammar.

>On the face of it, this problem does not smell of undecidability, or
>even NP completeness. Where do you suspect is the difficulty?

It's easy to find nodes with particular properties in a graph.  But
there is no guarantee that the result really is the start symbol.

There is a reason why you specify the start symbol in some way.

>Whether rules are dangling is also a graph property: is the graph
>connected.

"Connected" is an undirected-graph property.  If a nonterminal is
unreachable from the start symbol, it can still be connected to the
reachable graph through a RHS-nonterminal.

- anton
--
M. Anton Ertl
anton@mips.complang.tuwien.ac.at
http://www.complang.tuwien.ac.at/anton/

[toc] | [prev] | [next] | [standalone]


#2668

FromHans-Peter Diettrich <DrDiettrich1@netscape.net>
Date2021-05-21 17:02 +0200
Message-ID<21-05-017@comp.compilers>
In reply to#2666
On 5/21/21 12:49 PM, Eduardo Costa wrote:
> I've been lately dealing with a parser generator for LL grammars, and since
> it's inception I've always been blindy assuming the first element read from
> within the input file is going to be the start symbol or starting rule.
>
> So I've been wondering all this time, just out of curiosity, if there exists a
> method or algorithm to find out the start symbol of a given grammar?

Graph analysis methods exist to find unreachable nodes which can become
start symbols. In short any node that is a predecessor of *all* nodes
can be a start symbol. If no such node exists then the grammar is faulty
(discontiguous).

> While there would exist grammars we could recursively check to find out which
> it's start symbol is (i.e.: it's the only rule that used the rest of them,
> where checking every other resulted in dangling rules that weren't even called
> in), there might be other grammars for which more than one rule yields full
> coverage (all of these obviously defining different languages) and so leading
> to ambiguity.

IMO this problem can be solved by introduction of an artificial start
symbol that allows to reach all other symbols but can not be reached
itself. Please note that this solution solves a syntactic problem but
may not prevent or even cause semantic problems.

> I only contemplate a simple coverage test, even though other techniques could
> exist, again, all of them leading to a point where we couldn't ascertain if
> one or the other is what the user meant.
>
> So I'm wondering if this is even an issue in production-grade
> parser-generators out there?

A useful parser generator should include checks for grammar sanity.

DoDi

[toc] | [prev] | [next] | [standalone]


#2670

From"Ev. Drikos" <drikosev@gmail.com>
Date2021-05-22 06:52 +0300
Message-ID<21-05-019@comp.compilers>
In reply to#2666
On 21/05/2021 13:49, Eduardo Costa wrote:
> While there would exist grammars we could recursively check to find out which
> it's start symbol is (i.e.: it's the only rule that used the rest of them,
> where checking every other resulted in dangling rules that weren't even called
> in), there might be other grammars for which more than one rule yields full
> coverage (all of these obviously defining different languages) and so leading
> to ambiguity.

IMHO, it can be so simple as you describe here without important overhead.
Typically, a parser will reduce the start symbol and finish. All rules
that yield full coverage can be ie alternatives of a single root symbol:

RootSymbol -> R1 | R2 | ... | Rn

Ev. Drikos

[toc] | [prev] | [standalone]


Back to top | Article view | comp.compilers


csiph-web