Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.compilers > #404 > unrolled thread
| Started by | zhiwu <zhiwu@pps.jussieu.fr> |
|---|---|
| First post | 2012-01-05 09:49 +0100 |
| Last post | 2012-01-05 09:49 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.compilers
Question about yacc grammar for Java zhiwu <zhiwu@pps.jussieu.fr> - 2012-01-05 09:49 +0100
| From | zhiwu <zhiwu@pps.jussieu.fr> |
|---|---|
| Date | 2012-01-05 09:49 +0100 |
| Subject | Question about yacc grammar for Java |
| Message-ID | <12-01-005@comp.compilers> |
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 top | Article view | comp.compilers
csiph-web