[antlr-interest] Java Grammar

Johannes Luber jaluber at gmx.de
Sun Nov 23 06:57:12 PST 2008


Simon schrieb:
> I try to rephrase my question. I don't know how to handle some of the  
> primary constructs of the Java grammar.
> 
>   Integer.parseInt("123")
>   x.y(a, b)
>   x[12][34]
>   String.class
>   java.util.Arrays.class
> 
> are all pretty simple to detect with a symbol table and the  
> information from the imports. But how do I handle qualified type  
> names, such as the one in
> 
>   java.util.Arrays.asList("1", "2")
> 
> Conceptually, I need something like the following:
> 
>   primary
>       :    { isType(Identifier ('.' Identifier)*) } Identifier ('.'  
> Identifier)* ...
>       ;
> 
> That is, I have to stop as soon as I have a type name  
> (the .asList("1", "2") part should be parsed as selector).
> 
> This combination of semantic and syntactic predicate does not exist  
> out of the box. I could write a semantic predicate. But, is there an  
> easier way?
> 
> How would you write your parser to detect qualified type name  
> constructs? Any help is appreciated!
> 
> Simon

On the download page there are example grammars. One of them is a Java
grammar without the heavy interaction with javac. A similar language to
Java is C#. The official grammar for it is available on
<http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf>.

Johannes
> 
> On Nov 21, 2008, at 21:56 , Simon wrote:
> 
>> hi
>>
>> I'm trying to build an AST for a Java like language. The hardest part
>> (if you want to built a meaningful AST) is the section of
>> unaryExpressionNotPlusMinus (see grammar fragments at end or the
>> Java.g grammar on antlr.org).
>>
>> I have successfully built ASTs for the following constructs (using
>> semantic predicates based on a symbol table)
>>
>>   ^(FIELD_ACCESS target Identifier)
>>   ^(INVOKE target Identifier arguments)
>>   ^(ARRAY_ACCESS target expr)
>>
>> However, I'm struggling with fully qualified type names, such as those
>> in
>>
>>   java.lang.Integer.parseInt("123")
>>
>> Of course, I want something like
>>
>>   ^(INVOKE ^(TYPE_REFERENCE ...) arguments)
>>
>> The problem is that I somehow have to look ahead to detect whether it
>> is a qualified type name (don't know how the precedence is if there is
>> a variable named java with a field named lang that has a field named
>> Integer that has method named parseInt, but that's another problem). I
>> could write my own semantic predicate method that looks ahead in the
>> input to detect a qualified type name. Is there an easier way to do
>> that? Or am I approaching the problem from the wrong side?
>>
>> I've tried to look at the Java grammar from langtools recently posted
>> in this list, but didn't get any smarter (they rely heavily on the
>> existing javac classes).
>>
>> Thanks
>> Simon
>>
>>
>>
>> unaryExpressionNotPlusMinus
>>     :   ...
>>     |   primary selector* ('++'|'--')?
>>     ;
>>
>> primary
>>     :   parExpression
>>     |   literal
>>     |   'new' creator
>>     |   Identifier ('.' Identifier)* identifierSuffix?   // this is
>> the hard / interesting part
>>     |   primitiveType ('[' ']')* '.' 'class'
>>     |   'void' '.' 'class'
>>     ;
>>
>> identifierSuffix
>>     :   ('[' ']')+ '.' 'class'
>>     |   ('[' expression ']')+ // can also be matched by selector, but
>> do here
>>     |   arguments
>>     |   '.' 'class'
>>     ;
>>
>> selector
>>     :   '.' Identifier arguments?
>>     |   '[' expression ']'
>>     ;
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 



More information about the antlr-interest mailing list