Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question about yacc grammar for Java

6 views
Skip to first unread message

zhiwu

unread,
Jan 5, 2012, 3:49:08 AM1/5/12
to
Hi,

I saw some of the Java grammars downloaded from ftp.iecc.com/pub/file/
define "modifiers" as follows.

modifiers :
modifier
| modifiers modifier
;
modifier :
PUBLIC
| PROTECTED
| PRIVATE
| STATIC
| ABSTRACT
| FINAL
| NATIVE
| SYNCHRONIZED
| TRANSIENT
| VOLATILE
;
class_declaration :
modifiers CLASS IDENTIFIER super interfaces class_body
| CLASS IDENTIFIER super interfaces class_body
;
... ...
class_member_declaration :
field_declaration
| method_declaration
;
field_declaration :
modifiers type variable_declarators ';'
| type variable_declarators ';'
;
... ...

So this grammar will accept input which is not legal Java, like:

class A {
public protected private int b;
}


Is this an error in grammar definition? Thanks!

Regards,
lisa
[No, it's the way you write practical grammars in yacc.
Although it's possible to write grammars that accept
just semantically valid lists of modifiers, they tend
to be enormous and unmaintanable, since you have to write
out all the possibly allowable combinations. It's much
easier to write a grammar that accepts a larger lanaguage,
then make the semantic checks in the action code. That
usually allows better error messages, too, "inconsistent
type modifiers public and private" rather than a generic
"syntax error". -John]
0 new messages