Question about yacc grammar for Java

zhiwu <zhiwu@pps.jussieu.fr>
Thu, 05 Jan 2012 09:49:08 +0100

          From comp.compilers

Related articles
Question about yacc grammar for Java zhiwu@pps.jussieu.fr (zhiwu) (2012-01-05)
| List of all articles for this month |

From: zhiwu <zhiwu@pps.jussieu.fr>
Newsgroups: comp.compilers
Date: Thu, 05 Jan 2012 09:49:08 +0100
Organization: Compilers Central
Keywords: yacc, Java, question, comment
Posted-Date: 06 Jan 2012 10:12:23 EST

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]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.