How to recognise start-of-line in an Antlr grammar?
Asked Answered
C

0

6

In the language I work with, some keywords must be at the start of the line. This is mainly because string values within the language can go over multiple lines, and strings could easily contain these keywords.

The old yacc/lex grammar implementation I have easily deals with this because the lexer uses normal regexes to match text patterns, e.g.

^description    { actions }

matches 'description' at the start of a line and then does actions.

How can I do this in Antlr4? It does not appear to support proper regex for lexical matching. I'm using the IntelliJ plugin, and it complains when I use the '^' character on any lexer rule RHS.

Castellatus answered 30/9, 2015 at 16:8 Comment(4)
Does this answer your question?Selfcontent
That is, using DescKeyWord : BOL [ \t]* 'description' ;.Selfcontent
Yes that was the conclusion I came to as well, although you formalised it with an actual BOL token.Castellatus
Does this answer your question? How to detect beginning of line, or: "The name 'getCharPositionInLine' does not exist in the current context"Diplopod

© 2022 - 2024 — McMap. All rights reserved.