如果可能的话,我的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制作自定义编译器的一些资源。
谢谢。
答案 0 :(得分:3)
有一个教程here。
要解析您在问题中提供的示例,您需要:
TYPE
中,VARIABLE
中。实际上我会重写它,因为它也接受整数。我会做这样的事情:TOKEN: { < VARIABLE: <LETTER>(<LETTER>|<DIGIT>)* > | < #LETTER: ["a"-"z", "A"-"Z", "_"] > | < #DIGIT: ["0"-"9"] > }
void input(): {} { (declaration())* }
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()}
和身体规则类似:
void body(): {} { <START> (statement())* <STOP> }
您还需要更多代币:关键字&#34; START&#34;,&#34; STOP&#34;,&#34; OUTPUT&#34;,&#34; AND&#34;,&#34 ;或&#34;,&#34; NOT&#34;,运营商&#34;:&#34;,&#34;&amp;&#34;,&#34;(&#34;,&#34; ;)&#34;等...
我看到两种类型的语句:赋值和输出语句:
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()
}