[antlr-interest] Multiplication and pointers in C-style language

David Olsson vonolsson at gmail.com
Tue Apr 8 13:10:23 PDT 2008


I am a novice when it comes to language and compiler construction and is 
using ANTLR to create a C-style language which among other things will 
support pointers, using standard C syntax (*), and multiplication. The 
problem is that ANTLR may potentially be unable to differentiate between 
a pointer declaration and a multiplication statement. Consider the 
following statement;

    ID * ID;

where /ID /denotes a token corresponding to an identifier. The above 
statement can be either a declaration of a pointer to a user declared 
type (eg /Object *obj/) or a multiplication of two variables (eg /i * 
j/). Does anyone have any input on how to write good grammar to handle 
this type of situation?

Currently my parser grammar looks like the following (very simplified);

    block:                    '{' (variableDecl | statement)* '}';
    variableDecl:             'const'? type ('*'* 'const'?)? ID '='
    expression; ';';
    statement:                block | expression;
    expression:               additiveExpression ('=' expression)?;
    additiveExpression:       multiplicativeExpression (('+' | '-')
    multiplicativeExpression)*;
    multiplicativeExpression: primary (('*' | '/') primary)*;
    primary:                  literal | ID | '(' expression ')';
    literal:                  DECIMALLITERAL | REALLITERAL;

Thanks a lot for any input!

Best regards
David Olsson


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080408/da8462a5/attachment.html 


More information about the antlr-interest mailing list