CUP LALR解析器生成器:警告:生产从未减少

时间:2013-12-24 02:20:19

标签: parsing lalr cup

这是我第一次使用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)

1 个答案:

答案 0 :(得分:1)

我很确定如果您在制作中正确拼写Element而不是拼写ELEMENT,那么大多数(如果不是全部)这些错误都会消失。