[antlr-interest] predicate, uh?: bug in c-grammar

Davin McCall davmac at deakin.edu.au
Thu May 26 22:37:23 PDT 2005


Not sure if it applies in this particular case, but often things are 
done that way to make precedence work. If this is the case then 
"castExpr" should be interpreted as a rule matching "any expression 
whose precedence is equal to or higher than a cast".

For a simple example, consider multiplication and addition:

--- begin pseudo-grammar ---
Expression: AdditionExpr

MultExpr: INTEGER "*" INTEGER

AdditionExpr:
          MultExpr
          | AdditionExpr "+" MultExpr
--- end pseudo-grammar ---

... So that "4 + 5 * 6" is correctly parsed as (+ (* 5 6)) rather than 
(* (+ 4 5) 6) for instance. However, "AdditionExpr" will match "2 * 3", 
an expression whose precedence is higher than addition.

Davin


Lloyd Dupont wrote:

> found it!
> for some obscure reason castExpr could be something else than a cast. 
> expression such as sizof(), __alignof().... are recognized by 
> castExp(??!!!)
> why they put such production in castExpr is beyond my uderstanding, 
> but at least it's solved.
> (I now use a subrule like that:
> protected strictCast:
>      (LPAREN)=> castExpr
>      |
>      ;
> )
>  
> Anyway I would call that 'another bug in the c-grammar'
> I think that should be fixed, other could fall for the same trap!
>  
> PS: not to denigrate the c-grammar which is a great job, and I guess 
> hard to debug for lack of advanced grammar checking tool, just to warn 
> future users: it's slightly bugged!
>  
>
>     ----- Original Message -----
>     *From:* Lloyd Dupont <mailto:lloyd at nova-mind.com>
>     *To:* antlr-interest at antlr.org <mailto:antlr-interest at antlr.org>
>     *Sent:* Friday, May 27, 2005 10:19 AM
>     *Subject:* [antlr-interest] predicate, uh?
>
>     I have a simple grammar rule like that (inheriting from cgram)
>      
>     objcMethodDefinition  returns [String selName]
>     {
>      selName = "";
>     }
>      : ( PLUS | MINUS ) (castExpr)?
>       (
>         ( ID COLON )=> 
>          id0:ID COLON (castExpr)? ID { selName = id0.getText() + ":"; }
>          (
>           (id1:ID  { selName += id1.getText(); })? (COLON) (castExpr)?
>     ID { selName += ":"; }
>          ) *
>        | id2:ID { selName = id2.getText(); }
>       )
>      
>     note: there is no explicit end to this rule, because I reuse it
>     for both declaration (ending by a ';') and definition (ending by '{').
>      
>     when I test it I get this parsing error (while evaluating the (ID
>     COLON)=> !!!) (on something as simple as "- foo { ...."
>     ANTLR Parsing Error: # 1 "":line 22:1896: unexpected token: {
>     token name:LCURLY
>     line 22:1896: unexpected token: {
>             at ObjectiveCParser.postfixExpr(ObjectiveCParser.java:5876)
>             at ObjectiveCParser.unaryExpr(ObjectiveCParser.java:5293)
>             at ObjectiveCParser.castExpr(ObjectiveCParser.java:1548)
>             at
>     ObjectiveCParser.objcMethodDefinition(ObjectiveCParser.java:1129)
>             at
>     ObjectiveCParser.objcClassImplementation(ObjectiveCParser.java:1056)
>             at ObjectiveCParser.externalDef(ObjectiveCParser.java:213)
>             at ObjectiveCParser.externalList(ObjectiveCParser.java:1649)
>             at
>     ObjectiveCParser.translationUnit(ObjectiveCParser.java:1610)
>             at Test.main(Test.java:40)
>      
>     ?!!!
>     how come?
>     what did I do wrong?
>


-- 
Davin McCall, Research Programmer
Deakin University, Burwood, Australia.
Phone: 03 9251 7045 International: +61 3 9251 7045
Email: Davin.McCall at deakin.edu.au
Website: http://www.deakin.edu.au
Deakin University CRICOS Provider Code 00113B (Vic)



More information about the antlr-interest mailing list