[antlr-interest] Recursive parenthesises in the tree.

Johannes Luber jaluber at gmx.de
Mon Feb 16 22:43:37 PST 2009


Fatih Tolga Ata schrieb:
> Hi,
> 
> I'm learning antlr by implementing a javascript parser. My target 
> language is C#. I'm using Patrick Hulsmeijer's Ecmascript3 grammar. I 
> converted java codes to c# code in the grammar file. I was able to 
> compile a lexer, a parser and a tree walker with this grammar. I tried a 
> lot of js input, and I fixed some errors in the c# code in the grammar 
> file. But in the belov input, tree walker display "no viable alternative 
> at input 'PAREXPR'".
> The input is :
> if ((a1 = this.prototype[a1])) return add(this, a2, a1, a3);
> 
> I looked at grammar and tree walker files, but I didn't solve this 
> problem. In grammar file, I saw:
> 
> primaryExpression
>     : THIS
>     | Identifier
>     | literal
>     | arrayLiteral
>     | objectLiteral
>     | lpar=LPAREN expression RPAREN -> ^( PAREXPR[$lpar, "PAREXPR"] 
> expression )
>     ;
> 
> And in the tree walker file, there is:
> primaryExpression
>     : Identifier
>     | literal
>     ;
> 
> This implementation works well when the input has not recursive 
> parenthesises. If the input has recursive parens like "((( expression 
> )))", the tree walker gives the error I mentioned before.
> 
> I studied compilers before. But I am new at antlr grammar. I couldn't 
> understand what PARAEXPR is and what is the "-> ^(    )" in the 
> primaryExpression.

PAREXPR is a imaginary token, adding semantics to otherwise
indiscriminable situations or just some additional clarity like in this
case. The "-> ^()" is a tree rewrite operator, saying that the created
AST should look differently than the original input. I suppose your
problem with the tree walker is that it doesn't recognize all
possibilities created by the parser. Unless the missing alternatives are
somehow covered in the Identifier and literal rules, try

primaryExpression
    : THIS
    | Identifier
    | literal
    | arrayLiteral
    | objectLiteral
    | ^( PAREXPR expression )
    ;

In general, look at the documentation about ASTs to learn more about them.

Johannes
> 
> How can solve this problem?
> 
> Any help'll be appreciated.
> 
> Regards.
> 
> 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