1 year ago

#355338

test-img

beeblebrox

JFlex complaining about caret character

I'm trying to produce a simple standalone scanner with the use of JFlex. What I want to do now is to simply recognize the beginning of a line with the use of the caret (^) symbol.

From the Semantics section of the user manual:

In a lexical rule, a regular expression r may be preceded by a ^ (the beginning of line operator).

This is my code right now:

// lexer.jflex

%%
%standalone
%class Lexer

beginning = ^

%%

{beginning}     { System.out.println("beginning"); }
.                 { ; }

The required behavior is very simple: it should only recognize the beginning of each input line and ignore everything else. However, for some reason, jflex complains about the caret symbol:

Error in file "lexer.jflex" (line 5):
Syntax error.
beginning = ^
            ^

I also tried with some variations, for example with double quotes around the caret ("^") and escape character before it (\^), but obviously this recognizes the caret symbol as a character itself, and not as a regular expression representing the beggining of a line.

EDIT

Problem solved: the caret symbol must not be used in the options and macros section but instead in the actions and rules section, i.e. the third one. For example, the following code matches the # symbol at the beginning of a line:

// lexer.jflex

%% 
...
hash = "#"

%%

^{hash} { System.out.println("hash found!"); }

java

jflex

0 Answers

Your Answer

Accepted video resources