Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.ripco.com!rahul.net!wasp.rahul.net!rahul.net!news.misty.com!news.iecc.com!nerds-end From: zhiwu Newsgroups: comp.compilers Subject: Question about yacc grammar for Java Date: Thu, 05 Jan 2012 09:49:08 +0100 Organization: Compilers Central Lines: 57 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <12-01-005@comp.compilers> NNTP-Posting-Host: news.iecc.com X-Trace: leila.iecc.com 1325862743 7811 64.57.183.58 (6 Jan 2012 15:12:23 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Fri, 6 Jan 2012 15:12:23 +0000 (UTC) Keywords: yacc, Java, question, comment Posted-Date: 06 Jan 2012 10:12:23 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: x330-a1.tempe.blueboxinc.net comp.compilers:404 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]