具有理解能力的 JavaCC 语法问题

JavaCC Syntax issue with understand ability

我开始学习 Javacc 并试图找出这个问题,但我似乎无法完全理解我这样做是否正确。

所以我正在做的是为自定义语言制作解析器并使用 Javacc.

生成 Java 解析器源代码

我认为我这样做是对的,但对这是否正确有很多疑问。

这是我目前拥有的 .jj 文件。

options {
  JAVA_UNICODE_ESCAPE = true;
  STATIC = false;
}

PARSER_BEGIN(Custom_Lexer)
  public class Custom_Lexer {}
PARSER_END(Custom_Lexer)


void Custom_Lexer_Program() :
{}
{
  <BEGIN> <CLPL>
  ( Custom_Lexer_Statement() )*
  <END>
  <EOF>
}

void Custom_Lexer_Statement():
{}
{
    STATEMENT()
    <SEMICOLON>
}

void STATEMENT():
{}
{
    LOOKAHEAD(2) OUTPUT_STATEMENT()     |
    LOOKAHEAD(2) INPUT_STATEMENT()      |
    LOOKAHEAD(2) VARIABLE_DECLARATION() | 
    LOOKAHEAD(2) VARIABLE_ASSIGNMENT()  |
    LOOKAHEAD(2) IF_THEN_STATEMENT()
}

void OUTPUT_STATEMENT():
{}
{
    <OUTPUT> <EQUALS> EXPRESSION()
}

void INPUT_STATEMENT():
{}
{
    VARIABLE_DECLARATION()*
}

void VARIABLE_DECLARATION():
{}
{
    <VARIABLE> (<EQUALS> <INT> | <BOOL> | <STRING>)?
}

void VARIABLE_ASSIGNMENT():
{}
{
    <VARIABLE> (<EQUALS> EXPRESSION()
}

void IF_THEN_STATEMENT():
{}
{
    <IF> EXPRESSION() <THEN> VARIABLE_ASSIGNMENT() [<ELSE> VARIABLE_ASSIGNMENT()]
}
//Will define these later after the above issues are fixed
void EXPRESSION():
{}
{
    LOOKAHEAD(5) BINARY_EXPRESSION()        |
    LOOKAHEAD(5) IDENTIFIER_EXPRESSION()    |
    LOOKAHEAD(5) LITERAL_VALUE_EXPRESSION() |
    LOOKAHEAD(5) PARENTHESIZED_EXPRESSION()
}


//Reserved words
TOKEN: { <CLPL:   "CLPL"   > }
TOKEN: { <BEGIN:   "BEGIN"   > }
TOKEN: { <END:     "END"     > }
TOKEN: { <OUTPUT:  "OUTPUT"  > }
TOKEN: { <INPUT:   "INPUT"   > }
TOKEN: { <IF:      "IF"      > }
TOKEN: { <THEN:    "THEN"    > }


TOKEN: { <INT:    "int"      > }
TOKEN: { <BOOL:   "bool"     > }
TOKEN: { <STRING: "string"   > }


TOKEN: { <SEMICOLON:     ";" > }
TOKEN: { <LEFT_PAREN:    "(" > }
TOKEN: { <RIGHT_PAREN:   ")" > }
TOKEN: { <PLUS:          "+" > }
TOKEN: { <MINUS:         "-" > }
TOKEN: { <MULTIPLY:      "*" > }
TOKEN: { <DIVIDE:        "/" > }
TOKEN: { <EQUALITY:     "==" > }
TOKEN: { <EQUALS:        "=" > }
TOKEN: { <GT:            ">" > }
TOKEN: { <LT:            "<" > }


TOKEN: { <BOOLEAN_LITERAL: "true" | "false" > }


TOKEN: { <INTEGER_LITERAL: (["0"-"9"])+ > }


TOKEN: { <STRING_LITERAL: "\"" (~["\"","\","\n","\r"] | "\" (["n","t","b","r","f","\","\'","\""] | ["0"-"7"] (["0"-"7"])? | ["0"-"3"] ["0"-"7"] ["0"-"7"]))* "\""> }


TOKEN: { <IDENTIFIER: (["a"-"z"]|["A"-"Z"]|"_")+((["a"-"z","A"-"Z","0"-"9","_"])*)? > } 

它还没有完成,但看起来是一个合理的开始。我建议您避免所有 LOOKAHEAD 规范,直到您更好地理解您在做什么。尝试左因式分解,以便可以使用默认的先行方法做出所有选择。

我看到的一个问题是 VARIABLE_DECLARATIONINPUT_STATEMENT 之间的冲突无法解决,因为任何 VARIABLE_DECLARATION 也是 INPUT_STATEMENT