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


Groups > comp.compilers > #404

Question about yacc grammar for Java

From zhiwu <zhiwu@pps.jussieu.fr>
Newsgroups comp.compilers
Subject Question about yacc grammar for Java
Date 2012-01-05 09:49 +0100
Organization Compilers Central
Message-ID <12-01-005@comp.compilers> (permalink)

Show all headers | View raw


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]

Back to comp.compilers | Previous | Next | Find similar


Thread

Question about yacc grammar for Java zhiwu <zhiwu@pps.jussieu.fr> - 2012-01-05 09:49 +0100

csiph-web