javajflex

How to reference attribute from .bnf parser in JFlex?


I'm using a .bnf parser to detect specific expressions and I'm using JFlex to detect the different sections of these expressions. My issue is, some of these expressions may contain nested expressions and I dont know how to handle that.

I've tried to include the .bnf parser in my JFlex by using %include, then referencing the expression in the relative macro using PARAMETERS = ("'"[:jletter:] [:jletterdigit:]*"'") | expression. This fails as JFlex reports the .bnf to be malformed.

Snippet of JFlex:


%{
  public Lexer() {
    this((java.io.Reader)null);
  }
%}

%public
%class Lexer
%implements FlexLexer
%function advance
%type IElementType
%include filename.bnf
%unicode

PARAMETERS= ("'"[:jletter:] [:jletterdigit:]*"'") | <a new expression element>

%%

<YYINITIAL> {PARAMETERS}   {return BAD_CHARACTER;} some random return

Snippet of .bnf parser:

{
//list of classes used.
}
expression ::= (<expression definition>)

Any input would be greatly appreciated. Thanks.


Solution

  • I've found the solution to my issue. In further depth, the problem was in both my grammar file and my flex file. To solve the issue, I recursively called the expression in the grammar file like so: expression = (start value expression? end)

    With the JFlex, I declared numerous states until I found a way to chain together and endless amount of expressions. Looks a little like this:

    %state = WAITING_EXPRESSION
    
    <WAITING_NEXT> "<something which indicates start of nested expression>"   { yybegin(WAITING_EXPRESSION); return EXPRESSION_START; }