CUP Grammar

Joshua Franco <hexnet@hexnet.com>
25 Feb 2007 12:46:27 -0500

          From comp.compilers

Related articles
CUP Grammar hexnet@hexnet.com (Joshua Franco) (2007-02-25)
Re: CUP Grammar cdodd@acm.org (Chris Dodd) (2007-02-26)
| List of all articles for this month |

From: Joshua Franco <hexnet@hexnet.com>
Newsgroups: comp.compilers
Date: 25 Feb 2007 12:46:27 -0500
Organization: SBC http://yahoo.sbc.com
Keywords: Java, parse
Posted-Date: 25 Feb 2007 12:46:27 EST

I am having a bitch of a time with a grammar that I could swear is perfect
but seems to be horribly wrong.


It's the damnedest thing because I am POSITIVE the grammar is right, and
yet lines such as "y[0] = 1337;" and "y = new int[0..0];" are not being
recognized as members even though one can easily step through the parse
rules mentally and see that they are quite obviously members.


What is going wrong with CUP here?


Here is my grammar.


classList ::= _classDecl_plus ;


_classDecl_plus ::= classDecl | _classDecl_plus classDecl ;


classDecl ::= CLASS ID LBRACE _decl_star RBRACE SEMI
| CLASS ID EXTENDS ID LBRACE _decl_star RBRACE SEMI ;


_decl_star ::= _decl_plus | /*epsilon*/ ;
_decl_plus ::= decl | _decl_plus decl ;


decl ::= type ID SEMI
| type ID LPAREN _formals_opt RPAREN LBRACE _statement_star RBRACE
| ID LPAREN _formals_opt RPAREN LBRACE _statement_star RBRACE ;


_formals_opt ::= formals | /*epsilon*/ ;


formals ::= formal _cformals_star ;


_cformals_star ::= _cformals_plus | /*epsilon*/ ;
_cformals_plus ::= COMMA formal | _cformals_plus COMMA formal ;


_statement_star ::= _statement_plus | /*epsilon*/ ;
_statement_plus ::= statement | _statement_plus statement ;


statement ::= expr SEMI
| type ID SEMI
| lvalue ASSIGN expr SEMI
| LBRACE _statement_star RBRACE
| WHILE LPAREN expr RPAREN statement
| IF LPAREN expr RPAREN statement
| IF LPAREN expr RPAREN statement ELSE statement
| RETURN expr SEMI
| SEMI ;


formal ::= type ID ;


type ::= basicType PBRACKET | basicType LBRACKET RBRACKET | basicType
| ID PBRACKET | ID LBRACKET RBRACKET | ID ;


basicType ::= TINT | TBOOL | TVOID ;


_cexpr_star ::= _cexpr_plus | /*epsilon*/ ;
_cexpr_plus ::= COMMA expr | _cexpr_plus COMMA expr ;


expr ::= expr op expr
| uop expr
| NUMBER
| TRUE
| FALSE
| LPAREN expr RPAREN
| lvalue
| NEW ID LPAREN _actuals_opt RPAREN
| NEW type LBRACKET expr DOTDOT expr RBRACKET ;


op ::= AND | OR | EQL | NEQ | SMEQ | GTEQ
| SMALLER | GREATER | PLUS | MINUS | TIMES | DIVIDE | MOD ;


uop ::= NOT | UMINUS ;


lvalue ::= ID _actuals_block
| lvalue LBRACKET expr RBRACKET
| lvalue DOT ID _actuals_block
| SELF ;


_actuals_block ::= LPAREN _actuals_opt RPAREN | /*epsilon*/ ;
_actuals_opt ::= actuals | /*epsilon*/ ;


actuals ::= expr _cexpr_star ;



Post a followup to this message

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