I also wrote a parser for Java but I just copied the grammar Java Developer 
Tools (JDT) for Eclipse uses. It turns out that this is actually a fairly 
complicated problem. Keep in mind that there is also the >>> operator which 
further complicates this. Add to that that Java has both type arguments as 
well as type parameters with different syntaxes. The grammar JDT uses 
creates new symbols in order to keep track how many brackets have been 
opened.

I hope it's OK to link my ported solution here. The juicy bits are:

   - type arguments: 
   
https://github.com/musiKk/plyj/blob/41bce63c2a2bed6defd12e336b55c207b0cf0677/plyj/parser.py#L1129
   - type parameters: 
   
https://github.com/musiKk/plyj/blob/41bce63c2a2bed6defd12e336b55c207b0cf0677/plyj/parser.py#L1275
   
By the way being fairly new to parsing I found the Java grammar extremely 
complex. Kudos for doing well so far.



On Thursday, October 4, 2012 7:58:37 PM UTC+2, Erez wrote:
>
> Hi, I'm trying to parse Java and it's going ok so far, except for one 
> problem that I just can't solve, regarding the > token.
>
> Consider the following 3 lines:
>
>     someType<anotherType>> a;
>     if (b > c)
>         d >> e;
>
> If I define shift-right as:
>
>     SHR: '>>'
>     expression: var SHR var
>
> then the lexer will match the template code, and the parser will break.
>
> But If I define:
>
>     GT: '>'
>     expression: var GT GT var
>
> then the lexer is fine, but the parser chokes on 'b > c', expecting 
> another > to follow. 
> Perhaps there is a way to get the parser to behave?
>
> Here is the relevant grammar:
>
>     relationalexpression :   shiftexpression 
>                                   |   shiftexpression GT 
> relationalexpression
>
>     shiftexpression : additiveexpression 
>                            | additiveexpression GT GT shiftexpression
>
> And here is the code that breaks it:
>
>     a > b;
>
> And here is the debug info:
>
>     State  : 140
>     Stack  : packagedeclaration _anon_0_star modifiers CLASS identifier 
> IMPLEMENTS typelist LBRACE _anon_10_star modifiers VOID identifier 
> formalparameters LBRACE additiveexpression . LexToken(GT,'>',73,2318)
>     Action : Shift and goto state 254
>
>     State  : 254
>     Stack  : packagedeclaration _anon_0_star modifiers CLASS identifier 
> IMPLEMENTS typelist LBRACE _anon_10_star modifiers VOID identifier 
> formalparameters LBRACE additiveexpression GT . 
> LexToken(IDENTIFIER,'b',73,2320)
>     ERROR: Error  : packagedeclaration _anon_0_star modifiers CLASS 
> identifier IMPLEMENTS typelist LBRACE _anon_10_star modifiers VOID 
> identifier formalparameters LBRACE additiveexpression GT . 
> LexToken(IDENTIFIER,'b',73,2320)    
>
>
> Pleas help me, thank you!
>

-- 
You received this message because you are subscribed to the Google Groups 
"ply-hack" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msg/ply-hack/-/XXZZcJcEgDsJ.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to