2

这是我第一次使用 CUP 解析器,我不断收到以下错误:“警告:*生产”...“从未使用过。我不知道出了什么问题,请帮忙。请参阅附加的代码和错误日志下面,谢谢。

package adtl;

import java_cup.runtime.*;
import java.util.*;
import adtl.Lexer.*;
import adtl.ast.*;

parser code {:
   public void syntax_error(java_cup.runtime.Symbol current) {
      Token t = (Token) current;
       throw new SyntaxError("Syntax error in line " + t.line + " '" + t.toText() + "'");
   }
:};

/* Terminals (tokens returned by the scanner). */
   terminal SET, LOCK, LCBR, RCBR, COLON , SEMICOLON, AT , REV , LP, RP, DOT, COMMA, RETURN, ELSE,...

/* Non-terminals */
non terminal ADT ADT;
non terminal Element Element;
non terminal ElementList ElementList;
non terminal FieldDef FieldDef; 
non terminal Type Type; 
non terminal MethodDef MethodDef;
non terminal Args Arg;
non terminal Args Args;
non terminal Stmt  Stmt  ;
non terminal StmtList  StmtList  ;
non terminal Assign  Assign  ;
non terminal StmtList  SingleOrBlockStmt  ;
non terminal Expr  Expr  ;
non terminal AssignedArgs  AssignedArg  ;
non terminal PathExpr  PathExpr  ;
non terminal PathElement PathElement ;

/* Precedences */
    precedence left PLUS, MINUS, TIMES, DIVIDE, LOR, LAND, ELSE;
    precedence nonassoc CHOOSE, LOR, LAND, NOT, LT, LTE, GT, GTE, EQ...

  /* The grammar */
  ADT ::= ID:name LCBR ElementList:l RCBR
        {: RESULT = new ADT(name, l); :};

  ElementList ::=  ElementList:eList Element:e
                {:RESULT= eList.append(e)  ;:}
                |Element:e
                {:RESULT = new ElementList(e);:}
                | 
                {:RESULT = new ElementList();:};

ELEMENT ::= FieldDef:def | MethodDef: def
        {:RESULT = def;:};

FieldDef ::= ID:name COLON Type:type SEMICOLON
        {:RESULT = new FieldDef(name, type);:};

Type ::= ID:name
        {:RESULT = new Type(name):}
        | SET
        {:RESULT = new Type("set");:};

MethodDef ::= ID:name LP Args:args RP LCBR StmtList:stmts RCBR
            {:RESULT = new MethodDef(name, args, stmts);:}
        |ID:name LP RP LCBR StmtList:stmts RCBR
        {:RESULT = new MethodDef(name, new Args(), stmts);:};

StmtList ::= Stmt:stmt StmtList:sList 
                {:RESULT= sList.append(stmt)  ;:}
                |Stmt:stmt
                {:RESULT = stmt;:}
                | 
                {:RESULT = new StmtList();:};

Args ::= ID:name COLON Type:type COMMA Args:args 
            {:RESULT = args.append(name , type);:}
        | Arg:arg
        {:RESULT = arg ;:};

Arg ::= ID:name COLON Type:type
        {:RESULT = new Args(name, type);:};

Stmt ::= RETURN Expr:exp SEMICOLON
        {:RESULT = new Return(exp);:}
        | Assign:ass SEMICOLON
        {:RESULT = ass;:}
        | IF LP Expr:cond RP SingleOrBlockStmt:sobstmt
        {:RESULT = new Condition(cond, sobstmt);:}
        | IF LP Expr:cond RP SingleOrBlockStmt:sobstmt1 ELSE SingleOrBlockStmt:sobstmt2
        {:RESULT = new Condition(cond, sobstmt1, sobstmt2 ;:}
        | ASSERT Expr:exp SEMICOLON
        {:RESULT = new Assert(exp, "" );:}
        | ASSERT Expr:exp COLON QUOTE:str SEMICOLON
        {:RESULT = new Assert(exp, str );:}
        | LOCK PathExpr:pexp SEMICOLON
        {:RESULT = new Lock(pexp);:};

    Assign ::= PathExpr:pexp ASSIGN Expr:exp
        {:RESULT = new Assign(pexpr, exp);:};
Assign ::= PathExpr:pexp ASSIGN_PLUS Expr:exp
            {:RESULT = new Assign(pexp, new BinaryExpr(Ops.PLUS, pexp, exp));:};
Assign ::= PathExpr:pexp ASSIGN_MINUS Expr:exp
        {:RESULT = new Assign(pexpr, new BinaryExpr(Ops.MINUS,pexp, exp);:};

SingleOrBlockStmt ::= Stmt:stmt
            {:RESULT = stmt ;:}
            | LCBR Stmt:stmt StmtList:list RCBR
            {:RESULT = (new StmtList(list)).append(stmt);:};

Expr ::= CHOOSE Expr:exp
            {:RESULT = new Choose(exp);:}
        | INT:num
            {:RESULT = new INT(num);:}
            | BAR Expr:exp BAR
                {:RESULT = new SizeExp(exp);:}
                | NEW Type:type LP AssignedArg:assArgs COMMA RP
                {:RESULT = new NewExpr(type, assArgs);:}
                | MINUS Expr:exp
                {:RESULT = new BinaryExp(Ops.MINUS, new INT(0), exp);:}
                |Expr:exp1 PLUS Expr:exp2
                {:RESULT = new BinaryExpr(Ops.PLUS ,exp1, exp2);:}
                    |Expr:exp1 TIMES Expr:exp2
            {:RESULT = new BinaryExpr(Ops.TIMES ,exp1, exp2);:}
            |Expr:exp1 MINUS Expr:exp2
            {:RESULT = new BinaryExpr(Ops.MINUS ,exp1, exp2);:}
            |Expr:exp1 DIVIDE Expr:exp2
            {:RESULT = new BinaryExpr(Ops.DIVIDE ,exp1, exp2);:}
            |Expr:exp1 LT Expr:exp2
            {:RESULT = new BinaryExpr(Ops.LT ,exp1, exp2);:}
            |Expr:exp1 LTE Expr:exp2
            {:RESULT = new BinaryExpr(Ops.LTE ,exp1, exp2);:}
            |Expr:exp1 GT Expr:exp2
            {:RESULT = new BinaryExpr(Ops.GT ,exp1, exp2);:}
            |Expr:exp1 GTE Expr:exp2
            {:RESULT = new BinaryExpr(Ops.GTE ,exp1, exp2);:}
                |Expr:exp1 EQ Expr:exp2
                {:RESULT = new BinaryExpr(Ops.EQ ,exp1, exp2);:}
                |Expr:exp1 NEQ Expr:exp2
                {:RESULT = new BinaryExpr(Ops.NEQ ,exp1, exp2);:}
                | NOT Expr:exp
                {:RESULT = new NotExpr(exp);:}
                |Expr:exp1 LAND Expr:exp2
                {:RESULT = new BinaryExpr(Ops.LAND ,exp1, exp2);:}
                |Expr:exp1 LOR Expr:exp2
                {:RESULT = new BinaryExpr(Ops.LOR ,exp1, exp2);:}
                |Expr:exp1 IN Expr:exp2
                {:RESULT = new BinaryExpr(Ops.IN ,exp1, exp2);:}
                | PathExpr:pexp
                {:RESULT = new PathExpr(pexp);:}
                | LP Expr:exp RP
                {:RESULT = exp;:};

AssignedArg ::= ID:name ASSIGN Expr:exp
        {: RESULT = new AssignedArgs(name, exp);:};

PathExpr ::= PathElement:pele DOT PathExpr:pexpr
        {:RESULT = pexpr.append(pele);:}
        | PathElement:pel
        {:RESULT = new PathExpr(pel);:};

PathElement ::= ID:name AT
        {: RESULT = new PathElement(name, false, true);:}
        | ID:name
        {: RESULT = new PathElement(name, false , false);:}
        | REV LP ID:name RP AT
        {: RESULT = new PathElement(name, true , true);:}
        | REV LP ID:name RP
        {: RESULT = new PathElement(name, true , false);:};

错误日志:

\adtl\ADTL.cup

Warning : LHS non terminal "ELEMENT" has not been declared
Warning : Terminal "UMINUS" was declared but never used
Warning : *** Production "PathElement ::= REV LP ID RP " never reduced
Warning : *** Production "PathElement ::= REV LP ID RP AT " never reduced
Warning : *** Production "PathElement ::= ID " never reduced
Warning : *** Production "PathElement ::= ID AT " never reduced
Warning : *** Production "PathExpr ::= PathElement " never reduced
Warning : *** Production "PathExpr ::= PathElement DOT PathExpr " never reduced
Warning : *** Production "Type ::= SET " never reduced
Warning : *** Production "Type ::= ID " never reduced
Warning : *** Production "FieldDef ::= ID COLON Type SEMICOLON " never reduced
------- CUP v0.11a beta 20060608 Parser Generation Summary -------
  0 errors and 53 warnings
  42 terminals, 16 non-terminals, and 56 productions declared,
  producing 9 unique parse states.
  1 terminal declared but not used.
  0 non-terminal declared but not used.
  0 productions never reduced.
  0 conflicts detected (0 expected).
  Code written to "parser.java", and "sym.java".
---------------------------------------------------- (v0.11a beta 20060608)
4

1 回答 1

1

I'm pretty sure that most (if not all) of those errors would go away if you spelled Element correctly in its production, instead of spelling it ELEMENT.

于 2013-12-24T03:21:00.183 回答