paulzfm / ll1pg

Yet another LL(1) parser generation tool, built in principle.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Should we warn about unused productions?

twd2 opened this issue · comments

https://github.com/paulzfm/LL1-Parser-Gen/blob/688381a09a262b31091665838e89143e3d607e4f/src/main/scala/JavaCodeFile.scala#L227

Before write this line, should we check if ts is empty? If it is empty, the production is unused, and should we warn about it?

Currently, in some situation, this program may generate a Table.java like this:

// ...
                switch (lookahead) {
                    case PRINT:
                    case CASE:
                    case COMPLEX:
                    case VOID:
                    case FOR:
                    case '!':
                    case '-':
                    case CLASS:
                    case READ_LINE:
                    case WHILE:
                    case RETURN:
                    case NULL:
                    case INT:
                    case PRINT_COMP:
                    case SCOPY:
                    case '}':
                    case '@':
                    case DO:
                    case IDENTIFIER:
                    case NEW:
                    case '$':
                    case IF:
                    case THIS:
                    case INSTANCEOF:
                    case STRING:
                    case LITERAL:
                    case BRANCH:
                    case ELSE:
                    case '(':
                    case ';':
                    case '#':
                    case OD:
                    case DCOPY:
                    case BOOL:
                    case SUPER:
                    case BREAK:
                    case READ_INTEGER:
                    case '{':
                        return new Pair<>(131, Arrays.asList());           // <---
                        return new Pair<>(43, Arrays.asList(ELSE, Stmt));  // <---
                    default: return null;
                }
// ...

This two lines make Java compiler unhappy.

commented

Please provide some syntax that ts can be empty.

A modified decaf grammar:

// ...
ElseClause      :   /* empty */  // higher priority
                |   ELSE Stmt
                    {
                        $$.stmt = $2.stmt;
                    }
                ;
// ...

Note that the priority is swapped.

It seems that we should warn, closing.