开发者

Syntaxic analysis and parser

开发者 https://www.devze.com 2023-03-28 22:36 出处:网络
I want to parse something like that : path.to.variable \"path\" and \"to\" are object named \"Instance开发者_运维问答\" and variable reference a double.

I want to parse something like that :

path.to.variable

"path" and "to" are object named "Instance开发者_运维问答" and variable reference a double.

I've got the following grammar :

expr                ::= instancePath:i INSTANCE_SEPARATOR SHORTCUT:s
                    {:
                        RESULT = getDouble(s);
                    :}
                ;



instancePath        ::= instanceSubPath:p
                    {:
                        RESULT = p;
                    :}
                ;


instanceSubPath     ::= instanceSubPath:i1 INSTANCE_SEPARATOR instanceSubPath:i2
                    {:
                        RESULT = i2;
                    :}
                | SHORTCUT:s
                    {:
                        RESULT = pushInstance(s);
                    :}
                ;

In this grammar :

  • INSTANCE_SEPARATOR is the "." char.
  • SHORTCUT is a named with only letters like "path", "to" or "variable".
  • expr must return a double value obtained by the call of the function getDouble which takes a name (here "variable"). This function use the current selected instance. An instance contains a hashtable which contains double or other instance.
  • pushInstance is a function which set the current instance

for example we could have :

  • an instance "path" which contains a hashtable which is : "to"=>link to instance "to"
  • an instance "to" which contains a hashtable which is : "variable"=>10
  • the path "path.to.variable" must return 10.

This does not run because the grammar complains a null value. This is due to the fact the parser does this :

expr(
  instancePath(
    instanceSubPath(SHORTCUT("path"))
    INSTANCE_SEPARATOR 
    instanceSubPath(SHORTCUT("to")) 
    INSTANCE_SEPARATOR 
    instanceSubPath(SHORTCUT("variable"))
  )
  INSTANCE_SEPARATOR
  !NULL!

)

instead of doing

expr(
 instancePath(
    instanceSubPath(SHORTCUT("path"))
    INSTANCE_SEPARATOR 
    instanceSubPath(SHORTCUT("to")) 
 )
 INSTANCE_SEPARATOR 
 SHORTCUT("variable")
)

Why ? is there precedence problems ?

To finish, if I remove the instancePath rule, all runs :

expr                ::= instanceSubPath:i INSTANCE_SEPARATOR SHORTCUT:s
                    {:
                        RESULT = getDouble(s);
                    :}
                ;


instanceSubPath     ::= instanceSubPath:i1 INSTANCE_SEPARATOR instanceSubPath:i2
                    {:
                        RESULT = i2;
                    :}
                | SHORTCUT:s
                    {:
                        RESULT = pushInstance(s);
                    :}
                ;

I can't remove this extra rules because I've simplified the example but it's quite more complex in reality.

I don't understand why deleting this extra rule solve my problem...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号