0

I'm trying to write a Matlab grammar and I'm inspiring myself from Python's grammar.

However I have some questions about it:

1.

b.rule(M_EXPR).is(FACTOR, b.zeroOrMore(b.firstOf("*", "//", "/", "%", "@"), FACTOR)).skipIfOneChild();//M for multiplicative operator
b.rule(A_EXPR).is(M_EXPR, b.zeroOrMore(b.firstOf("+", "-"), M_EXPR)).skipIfOneChild();//A for additive operator

b.rule(SHIFT_EXPR).is(A_EXPR, b.zeroOrMore(b.firstOf("<<", ">>"), A_EXPR)).skipIfOneChild();
b.rule(AND_EXPR).is(SHIFT_EXPR, b.zeroOrMore("&", SHIFT_EXPR)).skipIfOneChild();
b.rule(XOR_EXPR).is(AND_EXPR, b.zeroOrMore("^", AND_EXPR)).skipIfOneChild();
b.rule(OR_EXPR).is(XOR_EXPR, b.zeroOrMore("|", XOR_EXPR)).skipIfOneChild(); //bitwise operations : voir https://www.tutorialspoint.com/python/bitwise_operators_example.htm


b.rule(COMPARISON).is(OR_EXPR, b.zeroOrMore(COMP_OPERATOR, OR_EXPR)).skipIfOneChild();
b.rule(COMP_OPERATOR).is(b.firstOf(
  "<",
  ">",
  "==",
  ">=",
  "<=",
  "!=",
  "<>",
  b.sequence("is", b.optional("not")),
  b.sequence(b.optional("not"), "in"))); //is and is not compare object identities (true or false if the same object)

b.rule(OR_TEST).is(AND_TEST, b.zeroOrMore("or", AND_TEST)).skipIfOneChild();
b.rule(AND_TEST).is(NOT_TEST, b.zeroOrMore("and", NOT_TEST)).skipIfOneChild();
b.rule(NOT_TEST).is(b.firstOf(COMPARISON, b.sequence("not", NOT_TEST))).skipIfOneChild(); //boolean operations

You can see here that the boolean operations are linked to the bitwise operations. But bitwise operations in Matlab are defined as functions (https://fr.mathworks.com/help/matlab/bit-wise-operations.html). Do you have any idea about what I could do to use them in my grammar definition ?

EDIT : Here is what I did for the first one. I don't know yet if it will work.

    b.rule(M_EXPR).is(FACTOR, b.zeroOrMore(b.firstOf("*", "//", "/", "%", "@"), FACTOR)).skipIfOneChild();//M for multiplicative operator
    b.rule(A_EXPR).is(M_EXPR, b.zeroOrMore(b.firstOf("+", "-"), M_EXPR)).skipIfOneChild();//A for additive operator

    b.rule(COMPARISON).is(A_EXPR, b.zeroOrMore(COMP_OPERATOR, A_EXPR)).skipIfOneChild();
    b.rule(COMP_OPERATOR).is(b.firstOf(
            "<",
            ">",
            "==",
            ">=",
            "<=",
            "~="));

    b.rule(OR_TEST).is(AND_TEST, b.zeroOrMore(OR, AND_TEST)).skipIfOneChild();
    b.rule(AND_TEST).is(NOT_TEST, b.zeroOrMore(AND, NOT_TEST)).skipIfOneChild();
    b.rule(NOT_TEST).is(b.firstOf(COMPARISON, b.sequence(TILDE, NOT_TEST))).skipIfOneChild();  //boolean operations

I hope you can help me :)

Jérôme
  • 51
  • 6
  • MATLAB also has `&` and `|`, but instead of bit-wise operators they are element-wise operators. It also has `&&` and `||` that you need to add to your grammar. The bit-wise functions `or` and `and` are just that: functions; they don't need a special place in the grammar. – Cris Luengo Jul 08 '19 at 14:34
  • Thanks for your answer ! It confirms what I was thinking :) and thank you for reminding me about `&&` and `||` – Jérôme Jul 08 '19 at 14:40
  • 1
    Also, `//`, `%` and `@` are not operators in MATLAB, but `.*`, `./`, `^` and `.^` are. You also need to add the unary `'` and `.'` operators. The MATLAB grammar really is quite different from the Python one. – Cris Luengo Jul 08 '19 at 14:43
  • Yes I saw these ones already :) Indeed, but on the other hand it is quite detailed. Do you have an idea of another grammar that could be more appropriate ? – Jérôme Jul 08 '19 at 14:53
  • https://stackoverflow.com/questions/9583307/where-can-i-find-a-formal-grammar-for-matlab – Cris Luengo Jul 08 '19 at 15:02

0 Answers0