paulzfm / ll1pg

Yet another LL(1) parser generation tool, built in principle.
7 stars 0 forks source link

Should we warn about unused productions? #2

Closed twd2 closed 6 years ago

twd2 commented 6 years ago

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.

paulzfm commented 6 years ago

Please provide some syntax that ts can be empty.

twd2 commented 6 years ago

A modified decaf grammar:

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

Note that the priority is swapped.

twd2 commented 6 years ago

It seems that we should warn, closing.