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

Jim Idle jimi at temporal-wave.com
Tue Apr 8 13:14:29 PDT 2008


Why not just download the examples tar from the download page and inspect the ANSI C grammar in there? In fact, you could just steal the rules if you want.

Jim

 

From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of David Olsson
Sent: Tuesday, April 08, 2008 1:10 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Multiplication and pointers in C-style language

 

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/1ccd9d18/attachment.html 


More information about the antlr-interest mailing list