How to recognize string in Lex file

2019-09-06 11:16发布

问题:

Hi what would be appropriate to recognize string in a lex.

I have already tried

enter code here

import java_cup.runtime.*;

%%
%cup
%line
NUM = [0-9]
ID = [a-zA-Z]
Pun= [:=;#@$^~]
WhiteSpace = [ \t\r\n\f]
SDQuo = [\"]
%%


({SDQuo}+) ({ID}|{NUM})* ({SDQuo}+) { return new Symbol(sym.STR, new String(yytext()));}

but the macro fail to be recognized. The error message that I kept getting is: Processing first section -- user code. Processing second section -- JLex declarations. Processing third section -- lexical rules. Creating NFA machine representation. Error: Parse error at line 39. Description: Missing brace at start of lexical action. Parse error.

回答1:

Loose the = signs in the definitions of NUM etc. and don't place them between %%. Instead place the last rule between %%.



标签: lex