简体   繁体   English

如何在JFlex中引用.bnf解析器的属性?

[英]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. 我正在使用.bnf解析器来检测特定表达式,我正在使用JFlex来检测这些表达式的不同部分。 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 我试图通过使用%include在我的JFlex中包含.bnf解析器,然后使用PARAMETERS = ("'"[:jletter:] [:jletterdigit:]*"'") | expression引用相对宏中的表达式。 PARAMETERS = ("'"[:jletter:] [:jletterdigit:]*"'") | expression . PARAMETERS = ("'"[:jletter:] [:jletterdigit:]*"'") | expression This fails as JFlex reports the .bnf to be malformed. 由于JFlex报告.bnf格式错误,因此失败。

Snippet of JFlex: 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: .bnf解析器的片段:

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

Any input would be greatly appreciated. 任何投入将不胜感激。 Thanks. 谢谢。

I've found the solution to my issue. 我找到了解决问题的方法。 In further depth, the problem was in both my grammar file and my flex file. 更进一步,问题出在我的语法文件和我的flex文件中。 To solve the issue, I recursively called the expression in the grammar file like so: expression = (start value expression? end) 为了解决这个问题,我在语法文件中递归调用表达式,如下所示: 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. 通过JFlex,我宣布了许多状态,直到我找到了一种链接在一起的方式和无穷无尽的表达方式。 Looks a little like this: 看起来有点像这样:

%state = WAITING_EXPRESSION

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM