Skip to content

Question about C grammar and tree decimation #347

@ricardaxel

Description

@ricardaxel

Hello,

when using the example C grammar I have an issue that might be a Pegged bug.

I have this very simple piece of code:

import std.stdio;

mixin(grammar(Cgrammar));
const code = `b + c`;
auto parsed = C.Expression(code);

writeln("No decimation:\n", parsed);

parsed = C.decimateTree(parsed);

writeln("================");
writeln("After decimation:\n", parsed);

when looking at the NON-decimated tree, we can see that there is a failure when entering PostfixExpression, especially the second part even though this part is a zeroOrMore rule (so no reason to fail, right ? The input doesn't match this part of the rule, we should move forward without any failure) . Note that the global Parsed Tree is marked as successful (which is expected as this code is accepted thanks to the AdditiveExpression rule).

....
+-C.PostfixExpression (failure)                                                                                                                      
|  +-and!(wrapAround, zeroOrMore) (failure)
      +-C.PrimaryExpression[0, 2]["b"] 
      |  +-or!(wrapAround!(C.Spacing, C.Identifier, C.Spacing), wrapAround!(C.Spacing, C.CharLiteral, C.Spacing), wrapAround!(C.Spacing, C.StringLiteral, C.Spacing), wrapAround!(C.Spacing, C.FloatLiteral, C.Spacing), wrapAround!(C.Spacing, C.IntegerLiteral, C.Spacing), and!(wrapAround, wrapAround, wrapAround))[0, 2]["b"]                                                                
      |     +-C.Identifier[0, 2]["b"]
      |        +-and!(negLookahead, IdentifierStart, option)[0, 1]["b"]  
      +-zeroOrMore!(wrapAround!(C.Spacing, or!(and!(wrapAround, wrapAround, wrapAround), and!(wrapAround, wrapAround), and!(wrapAround, wrapAround, wrapAround), and!(wrapAround, wrapAround), and!(wrapAround, wrapAround), wrapAround!(C.Spacing, literal!("++"), C.Spacing), wrapAround!(C.Spacing, literal!("--"), C.Spacing)), C.Spacing)) (failure)
          +-or!(and!(wrapAround, wrapAround, wrapAround), and!(wrapAround, wrapAround), and!(wrapAround, wrapAround, wrapAround), and!(wrapAround, wrapAround), and!(wrapAround, wrapAround), wrapAround!(C.Spacing, literal!("++"), C.Spacing), wrapAround!(C.Spacing, literal!("--"), C.Spacing)) (failure)                                                                                      
             +-and!(wrapAround, wrapAround, wrapAround) (failure)                                                                                     
                +-literal!("[") Failure at line 0, col 2, after "b " expected "\"[\"", but got "+ c;"                                                 
             +-and!(wrapAround, wrapAround) (failure)                                                                                                 
                +-literal!("(") Failure at line 0, col 2, after "b " expected "\"(\"", but got "+ c;"                                                 
             +-and!(wrapAround, wrapAround, wrapAround) (failure)                                                                                     
               +-literal!("(") Failure at line 0, col 2, after "b " expected "\"(\"", but got "+ c;"                                                 
             +-and!(wrapAround, wrapAround) (failure)                                                                                                 
                +-literal!(".") Failure at line 0, col 2, after "b " expected "\".\"", but got "+ c;"                                                 
             +-and!(wrapAround, wrapAround) (failure)                                                                                                 
                +-literal!("->") Failure at line 0, col 2, after "b " expected "\"->\"", but got "+ c;"                                               
             +-literal!("++") Failure at line 0, col 2, after "b " expected "\"++\"", but got "+ c;"                                                  
             +-literal!("--") Failure at line 0, col 2, after "b " expected "\"--\"", but got "+ c;" 
...

We can't even get rid of this because the decimation keeps failures. So the decimated tree is the following :

C.Expression[0, 5]["b", "+", "c"]
 +-C.AssignmentExpression[0, 5]["b", "+", "c"]
    +-C.ConditionalExpression[0, 5]["b", "+", "c"]
       +-C.LogicalORExpression[0, 5]["b", "+", "c"]
          +-C.LogicalANDExpression[0, 5]["b", "+", "c"]
             +-C.InclusiveORExpression[0, 5]["b", "+", "c"] 
                +-C.ExclusiveORExpression[0, 5]["b", "+", "c"]
                   +-C.ANDExpression[0, 5]["b", "+", "c"]
                      +-C.EqualityExpression[0, 5]["b", "+", "c"]
                         +-C.RelationalExpression[0, 5]["b", "+", "c"]
                            +-C.ShiftExpression[0, 5]["b", "+", "c"]
                               +-C.AdditiveExpression[0, 5]["b", "+", "c"]
                                  +-C.MultiplicativeExpression[0, 2]["b"]
                                  |  +-C.CastExpression[0, 2]["b"]
                                  |     +-C.UnaryExpression[0, 2]["b"]
                                  |        +-C.PostfixExpression (failure)
                                  |           +-C.PrimaryExpression[0, 2]["b"]
                                  |           |  +-C.Identifier[0, 2]["b"]
                                  |           +-or!(and!(wrapAround, wrapAround, wrapAround), and!(wrapAround, wrapAround), and!(wrapAround, wrapAround, wrapAround), and!(wrapAround, wrapAround), and!(wrapAround, wrapAround), wrapAround!
(C.Spacing, literal!("++"), C.Spacing), wrapAround!(C.Spacing, literal!("--"), C.Spacing)) (failure)
                                  |              +-literal!("[") Failure at line 0, col 2, after "b " expected "\"[\"", but got "+ c;"
                                  |              +-literal!("(") Failure at line 0, col 2, after "b " expected "\"(\"", but got "+ c;"
                                  |              +-literal!("(") Failure at line 0, col 2, after "b " expected "\"(\"", but got "+ c;"
                                  |              +-literal!(".") Failure at line 0, col 2, after "b " expected "\".\"", but got "+ c;"
                                  |              +-literal!("->") Failure at line 0, col 2, after "b " expected "\"->\"", but got "+ c;"
                                  |              +-literal!("++") Failure at line 0, col 2, after "b " expected "\"++\"", but got "+ c;"
                                  |              +-literal!("--") Failure at line 0, col 2, after "b " expected "\"--\"", but got "+ c;"
                                  +-C.AdditiveExpression[4, 5]["c"]
                                     +-C.MultiplicativeExpression[4, 5]["c"]
                                        +-C.CastExpression[4, 5]["c"]
                                           +-C.UnaryExpression[4, 5]["c"]
                                              +-C.PostfixExpression[4, 5]["c"]
                                                 +-C.PrimaryExpression[4, 5]["c"]
                                                    +-C.Identifier[4, 5]["c"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions