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

David Olsson vonolsson at gmail.com
Wed Apr 9 00:01:27 PDT 2008


Jim Idle wrote:
>
> 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
>
Actually, I have been looking quite a lot at the C example but as a 
novice when it comes to ANTLR and constructing languages I don't really 
see how and why it works. Perhaps I should rephrase my question; can 
anyone explain how and why the ANSI C example grammar works with the 
multiplication and pointer situation described below? :-)

Best regards
David Olsson

>  
>
> *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/20080409/a9befa40/attachment-0001.html 


More information about the antlr-interest mailing list