My name is Péricles and I'm Brazilian and student of computer science
in my city.
I don't know if this list is appropriate place for to do this kind of
question, but i need off a help for determine what type of grammar of
the GO Programming language, if it is of type LL (1)? I looked the
documentation off the grammar in language site, but I confess, I
didn't understand very well.
I would like someone knows the subject and speaks certainty about this
or recommend a good link with this information.
Please excuse me my english and thank you by your attention
--
| .''`. A fé não dá respostas. Só impede perguntas.
| : :' :
| `. `'`
| `- P.J. - http://wiki.dcc.ufba.br/~PeeJay
> My name is Péricles and I'm Brazilian and student of computer science
> in my city.
>
> I don't know if this list is appropriate place for to do this kind of
> question, but i need off a help for determine what type of grammar of
> the GO Programming language, if it is of type LL (1)? I looked the
> documentation off the grammar in language site, but I confess, I
> didn't understand very well.
>
> I would like someone knows the subject and speaks certainty about this
> or recommend a good link with this information.
The Go grammar is LALR(1). You can see it in src/cmd/gc/go.y. It's
nearly LL(1), but I think it fails to be LL(1) (or even LL(k)) when
parsing a function signature, because it requires arbitrary lookahead to
distinguish
f(a, b, c, d)
f(a, b, c, d int)
This is no problem for an LALR(1) parser, but I think it is a problem
for an LL(1) parser. Or I could easily have completely misremembered
how this stuff works in which case I'm sure somebody will correct me.
A more interesting feature of the Go grammar is that it can be parsed
without a symbol table.
Ian
> The language specification (golang.org/doc/go_spec.html) is not LL(1)
> or anything like that. In particular, you don't know whether an
> expression like X(Y) is a type conversion or a function call until you
> parse the whole Go package (a package is one or more .go files) and
> perform symbol resolution. As Ian noted, expressions can be parsed
> without a symbol table - but at the cost of *not* distinguishing
> between function calls and type conversions in the parser (see Bison
> file "src/cmd/gc/go.y" in Go implementation).
Well, that's true, but that's more of an antiproblem than a problem.
I wouldn't call it a "cost", more a simplification.
Chris
--
Chris "allusive" Dollin
If you approach it that way, then it needs unlimited lookahead,
even in an LALR(1) grammar, because you don't know until you
have parsed an arbitrary amount of extra stuff how to reduce the 'a'.
However, you can parse the function parameter list as a comma-separated
list of either (a) name, (b) type, or (c) name and type, and then apply
the semantic meaning later. (a) and (b) can be treated as the same case,
so it's a comma-separated list of [name] type, at least as far as the
grammar is concerned.
Russ
> If you approach it that way, then it needs unlimited lookahead,
> even in an LALR(1) grammar, because you don't know until you
> have parsed an arbitrary amount of extra stuff how to reduce the 'a'.
Yeah, but LALR(1) grammars have a form of unlimited lookahead because
they get to use a pushdown stack, unlike LL(1) grammars. Or at least
that is my vague recollection of this stuff.
Ian
2011/10/18 Ian Lance Taylor <ia...@google.com>:
Thank you very much by the help of you (Ian and Russ).
Excuse for the delay in answer.
[ ] 's
Péricles
Or like a var declaration/struct type literal:
func f(a, b int; c byte)(x, y string; z os.Error)
Or bracketed lists:
func f((a, b) int, c byte)((x, y) string, z os.Error)
Or use a keyword:
func f(a diddly b int, c byte)(x diddly y string, z os.Error)
Or...
Depends on your definition of better. Playing with the punctuation sort of makes it less clear that you're defining the name/type bindings of a comma separated list. Though, on the other hand, structs already do this, since composite literals are also comma separated, so it might not be so bad. It would be odd, since nowhere else in the language can variable identifiers sit next to each-other. It would also raise the question about comma separated lists in multiple assignment.
What is the advantage of doing that?
The current grammar is unambiguous, and the syntax is the same as for
a var declaration.
Ian
Those commas are redundant.
The modified grammar is also unambiguous.
func(a, b, int, a string)func(a b int a string)
var int int
var int intvar i int
That surprises me. I would expect
var int int
to parse as a VarDecl with an IdentifierList of "int" and a Type of
"int".
Likewise, I'd expect
var i int
to parse as a VarDecl with an IdentifierList of "i" and a Type of
"int".
Either line in isolation is fine. Also, swapping them is fine.
Is this a bug? If not, what's going on?
-Andrew
Is this a bug? If not, what's going on?
Identifiers name program entities such as variables and types. An identifier is a sequence of one or more letters and digits. The first character in an identifier must be a letter.
One answer for a student is that the grammar/ parser is
controlled by a yacc (Bison) file. See "src/cmd/gc/go.y".
A student should dig into the tutorials and books on "yacc" and
the open source version "bison". This would be an early
stop in a compiler class -- with exercises in designing and
implementing small/medium/large languages and parsers.
http://www.gnu.org/software/bison/manual/bison.html
See also "lex" (yylex).
As always there is more to look at:
http://www.gnu.org/software/bison/manual/bison.html#Bibliography
and
http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools#Second_edition
--
T o m M i t c h e l l
If you take a look into the file "src/cmd/gc/go.y" and compare it to
the grammar in the Go specification, you may be able to see some
examples of the grammar in the specification *not* being parseable by
Yacc/Bison because the Bison grammar uses different grammar rules in
several places and postpones the recognition to a later stage of
compilation.
I believe what I wrote in my 1st post in this forum thread has some
real value. If you have actual arguments (other than "42") that render
my post invalid, I would like you to present them to me. If your
argument is "examining the file src/cmd/gc/go.y" is pointless in
relation to the OP, it is a wrong argument.