java - 在 Java 中使用 CUP 的 Parser.cup

标签 java compiler-construction compiler-warnings cup

我正在尝试用 Java 做一个编译器,并且使用 CUP 来生成语法的语法。

我有这个 Parser.cup

/* Terminals (tokens returned by the scanner). */
terminal PLUS, MINUS;
terminal TIMES, DIV, DIVINT;
terminal LPAREN, RPAREN;
terminal EXP, MOD;
terminal String NUMERIC;
terminal SEMICOLON;
terminal CLOSE_BLOCK;
terminal OPEN_BLOCK;

/* Non terminals */
non terminal expr_list;
non terminal Expression expr;
non terminal Statement statement;
non terminal ListUtil<Statement> stmList;
non terminal ExpressionStatement statementPart;
non terminal BlockStatement block;

/* Precedences */
precedence left OPEN_BLOCK, CLOSE_BLOCK;
precedence left PLUS, MINUS;
precedence left TIMES, DIV, DIVINT, MOD;
precedence left EXP;

start with expr_list;

/* The grammar */

statement ::=
     block:b
    {: RESULT = b; :}
    | statementPart:s
    {: RESULT = s; :}
;

block ::=
    OPEN_BLOCK stmList:s CLOSE_BLOCK
    {: RESULT = new BlockStatement(s); :}
;   

stmList ::= 
    statementPart:s
    {:RESULT = new ListUtil<Statement>(s);:}
    | stmList:stml statementPart:s
    {: RESULT = stml.append(s); :}
| 
    {: RESULT = new ListUtil<Statement>(); :}
;

statementPart ::= expr:e 
    {: 
         RESULT = new ExpressionStatement(e);
    :} 
          SEMICOLON              
;

expr  ::= NUMERIC:n
  {:
    RESULT = new ResultExpression(n);
  :}

| expr:l PLUS expr:r
  {:
    RESULT = new PlusExpression(l, r);
  :}

| expr:l MINUS expr:r
  {:
    RESULT = new MinusExpression(l, r);
  :}

| expr:l TIMES expr:r
  {:
    RESULT = new TimesExpression(l, r);
  :}

| expr:l DIV expr:r
  {:
    RESULT = new DivExpression(l, r);
  :}

| expr:l DIVINT expr:r
  {:
    RESULT = new DivintExpression(l, r);
  :}

| expr:l EXP expr:r
  {:
    RESULT = new ExpExpression(l, r);
  :}

| expr:l MOD expr:r
  {:
    RESULT = new ModExpression(l, r);
  :}

| LPAREN expr:e RPAREN
  {:
    RESULT = e;
  :}
;

当我尝试生成 Parser.java 时,Eclipse 返回以下警告:

  [cup] Warning : *** Production "expr ::= LPAREN expr RPAREN " never reduced
  [cup] Warning : *** Production "expr ::= expr MOD expr " never reduced
  [cup] Warning : *** Production "expr ::= expr EXP expr " never reduced
  [cup] Warning : *** Production "expr ::= expr DIVINT expr " never reduced
  [cup] Warning : *** Production "expr ::= expr DIV expr " never reduced
  [cup] Warning : *** Production "expr ::= expr TIMES expr " never reduced
  [cup] Warning : *** Production "expr ::= expr MINUS expr " never reduced
  [cup] Warning : *** Production "expr ::= expr PLUS expr " never reduced
  [cup] Warning : *** Production "expr ::= NUMERIC " never reduced
  [cup] Warning : *** Production "statementPart ::= expr NT$0 SEMICOLON " never reduced
  [cup] Warning : *** Production "NT$0 ::= " never reduced
  [cup] Warning : *** Production "stmList ::= " never reduced
  [cup] Warning : *** Production "stmList ::= stmList statementPart " never reduced
  [cup] Warning : *** Production "stmList ::= statementPart " never reduced
  [cup] Warning : *** Production "block ::= OPEN_BLOCK stmList CLOSE_BLOCK " never reduced
  [cup] Warning : *** Production "statement ::= statementPart " never reduced
  [cup] Warning : *** Production "statement ::= block " never reduced

我想删除此警告,有什么帮助吗?

最佳答案

您必须更改行的开头

这样的东西可以工作:

start with statement;

关于java - 在 Java 中使用 CUP 的 Parser.cup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31126957/

相关文章:

c# - C# 编译器不会在每次编译时立即报告所有错误吗?

perl - 使用 Perl,如何跟踪常量被重新定义的位置?

c++ - 为什么 g++ 在通过 const 引用将未初始化的值传递给函数时不报告警告?

java - 如何消除java中的空指针异常?

java - 如何在JavaFX中克隆场景图中的节点?

vb.net - 发生错误时,VS2005立即运行我的旧代码

c - 标准库包含多个文件多次?

c++ - Visual Studio 2005 (C++) 默认警告级别

java - 在java中将小长整型转换为大长整型

java - 横向模式在 Android Studio 中不起作用