2

How can I write a rule to parse C++ comments either on a line alone or after other code?

I've tried lots of combinations, the latest one being:

?comment: "//" /[^\n]*/ NEWLINE
João M. S. Silva
  • 1,078
  • 2
  • 11
  • 24

3 Answers3

2

You had the right idea, but you should define comments as a single terminal (i.e. not a structure), for performance, and also so you can ignore them.

COMMENT: "//" /[^\n]*/ NEWLINE

%ignore COMMENT

Example grammar:

from lark import Lark

g = r"""
!start: "hello"

COMMENT: "//" /[^\n]*/ _NEWLINE
_NEWLINE: "\n"
%ignore COMMENT
%ignore " "
"""

parser = Lark(g)
print(parser.parse("hello // World \n"))
Erez
  • 1,287
  • 12
  • 18
  • How would you modify this grammar to support the other form of c-style comment -- the multi-line comment e.g. `/* multi-line \n comment */` – Zelazny7 Jan 01 '21 at 21:05
1

You simply define a terminal and then ignore it:

COMMENT : /\/\// /.*/
        | /\/\*/ /.*/ /\*\//

%ignore COMMENT

NOTE: This will work only if you'll ignore all whitespace

%import common.WS
%ignore WS
superbox
  • 53
  • 1
  • 7
0

Using: ?comment: /\/\/[^\n]*/

Then I had to handle the comment as a lark.lexer.Token.

João M. S. Silva
  • 1,078
  • 2
  • 11
  • 24