1

I am writing a microJava compiler and I am currently implementing class logic. So far I have implemented field logic but I am struggling with method access. The code bellow is working properly (for fields)

Designator ::= (DesignatorVar) IDENT
           |
           (DesignatorPropertyAccess) DesignatorClassName DesignatorClassMore
           |
           (DesignatorElem) DesignatorArrayName LBRACKET Expr RBRACKET
           |
           (DesignatorElemPropertyAccess) DesignatorClassElem DesignatorClassMore
           ;
DesignatorClassMore ::= (DesignatorClassMoreFinal) DOT IDENT
                    |
                    (DesignatorClassMoreFinalElem) DOT DesignatorClassArrayName LBRACKET Expr RBRACKET
                    |
                    (DesignatorClassMoreNotFinal) DesignatorClassMore DOT IDENT
                    |
                    (DesignatorClassMoreNotFinalElem) DesignatorClassMore DOT DesignatorClassArrayName LBRACKET Expr RBRACKET
                    ;

DesignatorClassElem ::= (DesignatorClassElem) DesignatorArrayName LBRACKET Expr RBRACKET ;
DesignatorClassName ::= (DesignatorClassName) IDENT;
DesignatorClassArrayName ::= (DesignatorClassArrayName) IDENT;
DesignatorArrayName ::= (DesignatorArrayName) IDENT;

The problem appears when I try to apply class method calls, I am doing it like this

DesignatorClassMore ::= (DesignatorClassMoreFinal) DOT IDENT OptCall
                    |
                    (DesignatorClassMoreFinalElem) DOT DesignatorClassArrayName LBRACKET Expr RBRACKET OptCall
                    |
                    (DesignatorClassMoreNotFinal) DesignatorClassMore DOT IDENT OptCall
                    |
                    (DesignatorClassMoreNotFinalElem) DesignatorClassMore DOT DesignatorClassArrayName LBRACKET Expr RBRACKET OptCall
                    ;

OptCall ::= DesignatorFuncCall
        |
        /* epsilon */
        ;

DesignatorFuncCall ::= LPAREN StackInitialize RPAREN
               |
               LPAREN StackInitialize ActPars RPAREN
               ;

Designator function call logic is correct because I have tested it on the simple methods (not class methods) so I believe the problem is shift/reduce conflict but I can't resolve it. What should I do?

For parser generation I am using cup_v10k.jar and I can provide link to this library.

The problem is, as I was doing project (I am at semantic analysis) I realised that I haven't implemented class method invocation in my syntax rules. And I am stuck with grammar problem (probably shift/reduce conflict). I have tried many modifications, and I have one observation but I can't figure why is it the thing. I can successfully compile DOT IDENT | DOT IDENT LBRACKET ... but when I add DOT IDENT LPAREN ... i have error which is basic and doesn't tell me anything.

4
  • 1
    Please edit the specific error message into the question (copy & paste). It would also be worth your while to produce a reduced version of the grammar, as small as possible, that still manifests the issue. Commented May 21 at 14:54
  • 1
    Also, be sure that enough of the grammar is presented to reproduce the issue. I'm inclined to suspect that the particular issue with method invocation gramamr might relate to conflicts with the (omitted) grammar for parenthesized expressions. Commented May 21 at 14:59
  • 1
    Be aware also that the Java Language Specification embeds a formal grammar for Java that you might find useful for reference. Commented May 21 at 15:01
  • 1
    "when I add DOT IDENT LPAREN ... i have error" -- the code excerpt presented does not exhibit such an addition. DOT IDENT OptCall is not the same thing, especially given the production list for OptCall. Please ensure that the code presented aligns with the prose, and that both accurately reflect the issue you are asking about. Commented May 21 at 15:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.