我必须使用javacc制作一个python编译器,而且python作用域有问题如何检查不同作用域中有多少行代码?你知道吗
options
{
static = true;
}
PARSER_BEGIN(MyNewGrammar)
package test;
public class MyNewGrammar
{
public static void main(String args []) throws ParseException
{
MyNewGrammar parser = new MyNewGrammar(System.in);
while (true)
{
System.out.println("Reading from standard input...");
System.out.print("Enter an expression like \"1+(2+3)*4;\" :");
try
{
switch (MyNewGrammar.one_line())
{
case 0 :
System.out.println("OK.");
break;
case 1 :
System.out.println("Goodbye.");
break;
default :
break;
}
}
catch (Exception e)
{
System.out.println("NOK.");
System.out.println(e.getMessage());
MyNewGrammar.ReInit(System.in);
}
catch (Error e)
{
System.out.println("Oops.");
System.out.println(e.getMessage());
break;
}
}
}
}
PARSER_END(MyNewGrammar)
SKIP :
{
" "
| "\r"
| "\t"
| "\n"
}
TOKEN : /* OPERATORS */
{
< PLUS : "+" >
| < MINUS : "-" >
| < MULTIPLY : "*" >
| < DIVIDE : "/" >
| <IF: "if">
| <AND: "and">
| <BREAK: "break">
| <CLASS: "class">
| <CONTINUE: "continue">
| <OR: "or">
| <PASS: "pass">
| <PRINT: "print">
| <ELIF: "elif">
| <ELSE: "else">
| <EXEPT: "exept">
| <EXEC: "exec">
| <FINALLY: "finally">
| <FOR: "for">
| <IN: "in">
| <DEF: "def">
| <DEL: "del">
| <IS: "is">
| <NOT: "not">
| <RAIS: "rais">
| <RETURN: "return">
| <TRY: "try">
| <WHILE: "while">
| <WITH: "with">
| <YIELD: "yield">
| <FROM: "from">
| <GLOBAL: "global">
| <IMPORT: "import">
| <RANGE: "range">
| <XRANGE: "xrange">
}
TOKEN :
{
< CONSTANT : (< DIGIT >)+ >
| <id: (<LETTER>)(<LETTER>|<DIGIT>)* >
| <LETTER: (<LOWER>|<UPPER>) >
| <literal:"\""((< LETTER >)|(< DIGIT >))+ "\"" >
| < #DIGIT : [ "0"-"9" ] >
| < #LOWER: ["a" - "z"]>
| < #UPPER: ["A" - "Z"]>
}
int one_line() :
{}
{
sum() |forp()";"
{
return 0;
}
| ";"
{
return 1;
}
}
void sum() :
{}
{
term()
(
(
< PLUS >
| < MINUS >
)
term()
)*
}
void term() :
{}
{
unary()
(
(
< MULTIPLY >
| < DIVIDE >
)
unary()
)*
}
void unary() :
{}
{
< MINUS > element()
| element()
}
void element() :
{}
{
< CONSTANT >
| "(" sum() ")"
}
void forp():
{}
{
"for"< id >"in"range()
}
void range():
{}
{
"range""("(< id >|< CONSTANT >)","(< id >|< CONSTANT >)")"|"xrange""("(< id >|< CONSTANT >)","(< id >|< CONSTANT >)")"
}
如何使用其范围内的所有语句解析
让python解析变得有趣的是缩进。该标准给出了插入缩进和DEDENT标记的规则。我们可以在JavaCC中实现这一点,但下面采用另一种方法,即使用语义先行。你知道吗
现在还需要编写一些java方法
这都是未经测试,但我认为它会工作,一旦小错误得到纠正。你知道吗
一旦可以解析,计算行数就很简单了。你知道吗
相关问题 更多 >
编程相关推荐