2015-06-29 21 views
1

我試圖用Java編寫一個編譯器,而使用CUP編譯生成語法的語法。在Java中使用CUP的Parser.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 

我想刪除這個警告,任何幫助嗎?

回答

2

你必須改變與線開始

像這樣的東西可以工作:

開始與說明;