Search code examples
pythonregexply

How do i do comments in PLY


How do I do comments in ply. I am making an interpreter in PLY called calico. One problem I am experiencing is comments. They should look like \\\\this\\\\. However, I have some trouble implementing them. Here is the calico source code

...
\\inheritance and polymorphism\\;
public class Main(){
    public static main() {
        println("test"); \\hello world\\;
        me = class Person(11, "a", "b", "11/09/09"); \\objects\\;
        println(me.get_details());
        me.tax(); \\encapsulation\\;
        println(me.get_details());
        me2 = class Employee(11, "a", "b", "11/09/09", "n", "11/09/35", 1000)
    }
};

The comment token should look like this \\\\inheritance and polymorphism\\\\ but instead it looks like this \\inheritance and polymorphism\\;public class Main(){ public static main() { println... I have tried to do it like this t_COMMENT = r"\\\\.*\\\\" but that has not work I have also tried it like this

def t_COMMENT(t):
    r'(/\\(.|\n)*?\\/)|(//.*)'
    pass


Solution

  • I recommend that you test your regex separately, in a standalone website like regex101.com

    Here is a link to the regex described below.

    I tested this string to declare comments:

    (\\\\(.|\n)*?\\\\)
    

    And I was able to get three matches for

    \\ a multiline
    comment \\
    print (a)
    \\inheritance and polymorphism\\;print ("ab");\\b\\;print("end")
    

    namely

    \\ a multiline
    comment \\
    

    and

    \\inheritance and polymorphism\\
    

    and

    \\b\\
    

    According to regex101, it needs the doubled backslash because

    \ matches the character \ literally (case sensitive)

    According to regex101, it needs the (.|\n)*? because

    2nd Capturing Group (.|\n)*?

    *? matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)