gpt4 book ai didi

JavaCC - 简单表达式

转载 作者:行者123 更新时间:2023-11-30 06:45:36 31 4
gpt4 key购买 nike

如果可能,我的 void decleration() 应该根据图像解析字符串输入。

SKIP :
{
< " " | "\t" | "\r" | "\n" | "\r\n" >
| <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n") >
| <"/*"(~["/"])* "*""/" >
}
TOKEN : { < VAR: "VAR" > }
TOKEN : { < AS: "AS" >}
TOKEN : /* KEYWORDS */
{
< TYPE: "INT"| "BOOL"|"FLOAT" >
}

TOKEN :
{
< EQUALS: "=" >
}
TOKEN:
{
< PLUS : "+" >|< MINUS :"-" >|< MUL: "*" >|< DIV: "/" >|< COMM: ",">
}
TOKEN:
{
< VARIABLE: (["a"-"z", "A"-"Z", "0"-"9"])+ >
}

void decleration(): {} { <VAR> (<VARIABLE>|< COMM >|<EQUALS>)+ <AS> <TYPE>}

我还是 JavaCC 的新手,所以请原谅我的代码不好,我在哪里可以找到一些使用 JavaCC 制作自定义编译器的资源。

谢谢。

最佳答案

有教程here .

要解析您在问题中给出的示例,您需要:

  • 在 token TYPE中添加“CHAR”,
  • 在 token VARIABLE 中添加“_”。实际上我会重写它,因为它也接受整数。我会做这样的事情:
TOKEN:
{
< VARIABLE: <LETTER>(<LETTER>|<DIGIT>)* >
| < #LETTER: ["a"-"z", "A"-"Z", "_"] >
| < #DIGIT: ["0"-"9"] >
}
  • 为声明列表添加非终端生产方法:
void input(): {} { (declaration())* }
  • 为初始值添加标记。这可能是一个复杂的表达式,但您至少需要 INT、CHAR、FLOAT 和字符串的字面值(如示例中的“TRUE”):
TOKEN:
{
< INT_VALUE: (<DIGIT>)+ >
| < FLOAT_VALUE: <INT_VALUE> "." (<DIGIT>)* | "." (<DIGIT>)+ >
| < CHAR_VALUE: "\'" (~["\'","\\"]|<ESCAPE>) "\'" >
| < STRING_VALUE: "\"" (~["\'","\\"]|<ESCAPE>)* "\"" >
| < #ESCAPE: "\\" ["n","t","b","r","f","\\","\"","\'"] >
}
  • 添加变量初始值的产生式规则:
void varDeclaration(): {} { <VARIABLE> (<EQUALS> literalValue())? }
void literalValue(): {} { <INT_VALUE> | <FLOAT_VALUE> | <CHAR_VALUE> | <STRING_VALUE> }

声明规则变为:

void declaration(): {} { <VAR> varDeclaration() (< COMM > varDeclaration())* <AS> <TYPE>}

请注意,当您跳过行尾字符时,没有什么可以阻止在同一行上出现多个声明。

生成的文件如下所示:

options {
STATIC = false;
OUTPUT_DIRECTORY = "src/parser";
}
PARSER_BEGIN(Parser)
package parser;
public class Parser {
public void parse() throws ParseException {
input();
}
}
PARSER_END(Parser)

SKIP :
{
< " " | "\t" | "\r" | "\n" | "\r\n" >
| <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n") >
| <"/*"(~["/"])* "*""/" >
}
TOKEN : { < VAR: "VAR" > }
TOKEN : { < AS: "AS" >}
TOKEN : /* KEYWORDS */
{
< TYPE: "INT" | "BOOL" | "FLOAT" | "CHAR" >
}

TOKEN :
{
< EQUALS: "=" >
}
TOKEN:
{
< PLUS : "+" >|< MINUS :"-" >|< MUL: "*" >|< DIV: "/" >|< COMM: ",">
}
TOKEN:
{
< VARIABLE: <LETTER>(<LETTER>|<DIGIT>)* >
| < #LETTER: ["a"-"z", "A"-"Z", "_"] >
| < #DIGIT: ["0"-"9"] >
}
TOKEN:
{
< INT_VALUE: (<DIGIT>)+ >
| < FLOAT_VALUE: <INT_VALUE> "." (<DIGIT>)* | "." (<DIGIT>)+ >
| < CHAR_VALUE: "\'" (~["\'","\\"]|<ESCAPE>) "\'" >
| < STRING_VALUE: "\"" (~["\'","\\"]|<ESCAPE>)* "\"" >
| < #ESCAPE: "\\" ["n","t","b","r","f","\\","\"","\'"] >
}

void input(): {} { (declaration())* }
void varDeclaration(): {} { <VARIABLE> (<EQUALS> literalValue())? }
void literalValue(): {} { <INT_VALUE> | <FLOAT_VALUE> | <CHAR_VALUE> | <STRING_VALUE> }
void declaration(): {} { <VAR> varDeclaration() (< COMM > varDeclaration())* <AS> <TYPE>}

更新

添加正文部分;据我所知,输入是一个声明列表,后跟一个正文:

void input(): {} { (declaration())* body() }

body 规则是这样的:

void body(): {} { <START> (statement())* <STOP> }

您还需要一些标记:关键字“START”、“STOP”、“OUTPUT”、“AND”、“OR”、“NOT”、运算符“:”、“&”、“(”、“) "等等...

我看到两种类型的语句:赋值语句和输出语句:

void statement(): {} { assignment() | output() }
void assignment(): {} { <VARIABLE> <EQUALS> expression() }
void output(): {} { <OUTPUT> <COLON> expression() }

它们都需要表达式的规则。它可能看起来像这样:

void expression(): {} { comparison() (logicalOp() comparison())* }
void logicalOp(): {} { <AND> | <OR> }
void comparison(): {} { simpleExpression() ( comparisonOp() simpleExpression() )? }
void comparisonOp(): {} { <LT> | <GT> | <LE> | <GE> | <EQ> | <NE> }
void simpleExpression(): {} { term() ( addOp() term() )* }
void addOp(): {} { <PLUS> | <MINUS> | <CAT> }
void term(): {} { factor() ( mulOp() factor() )* }
void mulOp(): {} { <MUL> | <DIV> | <MOD> }
void factor(): {} {
(<PLUS>|<MINUS>) factor()
| <LPAR> expression() <RPAR>
| <NOT> expression()
| <VARIABLE>
| literalValue()
}

综合起来:

options {
STATIC = false;
OUTPUT_DIRECTORY = "src/parser";
}
PARSER_BEGIN(Parser)
package parser;
public class Parser {
public void parse() throws ParseException {
input();
}
}
PARSER_END(Parser)

SKIP :
{
< " " | "\t" | "\r" | "\n" | "\r\n" >
| <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n") >
| <"/*"(~["/"])* "*""/" >
}
TOKEN : { < VAR: "VAR" > }
TOKEN : { < AS: "AS" >}
TOKEN : { < START: "START" > | < STOP: "STOP" > | < OUTPUT: "OUTPUT" > }
TOKEN : /* KEYWORDS */
{
<INT: "INT">|<BOOL:"BOOL">|<FLOAT:"FLOAT">|<CHAR:"CHAR">
|<AND: "AND"> | <OR: "OR"> | <NOT: "NOT">
}

TOKEN :
{
< EQUALS: "=" > | < COLON: ":" > | <LPAR: "(">|<RPAR: ")">
}
TOKEN:
{
< PLUS : "+" >|< MINUS :"-" >|< MUL: "*" >|< DIV: "/" >|< MOD: "%" >|< COMM: ",">
|<LT: "<">|<GT: ">">|<LE: "<=">|<GE: ">=">|<EQ: "==">|<NE: "<>">
|<CAT: "&">
}
TOKEN:
{
< VARIABLE: <LETTER>(<LETTER>|<DIGIT>)* >
| < #LETTER: ["a"-"z", "A"-"Z", "_"] >
| < #DIGIT: ["0"-"9"] >
}
TOKEN:
{
< INT_VALUE: (<DIGIT>)+ >
| < FLOAT_VALUE: <INT_VALUE> "." (<DIGIT>)* | "." (<DIGIT>)+ >
| < CHAR_VALUE: "\'" (~["\'","\\"]|<ESCAPE>) "\'" >
| < STRING_VALUE: "\"" (~["\"","\\"]|<ESCAPE>)* "\"" >
| < #ESCAPE: "\\" ["n","t","b","r","f","\\","\"","\'"] >
}

void input(): {} { (declaration())* body() }
void varDeclaration(): {} { <VARIABLE> (<EQUALS> literalValue())? }
void literalValue(): {} { <INT_VALUE> | <FLOAT_VALUE> | <CHAR_VALUE> | <STRING_VALUE> }
void declaration(): {} { <VAR> varDeclaration() (<COMM> varDeclaration())* <AS> type()}
void type(): {} {<INT>|<FLOAT>|<BOOL>|<CHAR>}
void body(): {} { <START> (statement())* <STOP> }
void statement(): {} { assignment() | output() }
void assignment(): {} { <VARIABLE> <EQUALS> expression() }
void output(): {} { <OUTPUT> <COLON> expression() }
void expression(): {} { comparison() (logicalOp() comparison())* }
void logicalOp(): {} { <AND> | <OR> }
void comparison(): {} { simpleExpression() ( comparisonOp() simpleExpression() )? }
void comparisonOp(): {} { <LT> | <GT> | <LE> | <GE> | <EQ> | <NE> }
void simpleExpression(): {} { term() ( addOp() term() )* }
void addOp(): {} { <PLUS> | <MINUS> | <CAT> }
void term(): {} { factor() ( mulOp() factor() )* }
void mulOp(): {} { <MUL> | <DIV> | <MOD> }
void factor(): {} {
(<PLUS>|<MINUS>) factor()
| <LPAR> expression() <RPAR>
| <NOT> expression()
| <VARIABLE>
| literalValue()
}

关于JavaCC - 简单表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48679008/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com