この例のAntLR 3 Python文法の問題は何ですか?
質問
AntLRを使用することをお勧めし、この「チュートリアル」の後にエラーに遭遇したようです。 https://theantlrguy.atlassian.net/Wiki / Display / AntLR3 / 5 +分+はじめに+ + ANTLR + 3
ANTLRツールを実行すると
.
java -classpath antlr-3.1.3.jar antlr.Tool SimpleCalc.g
解決
You're using the wrong Tool
class to generate the lexer and parser. Most ANTLR 3.x versions still have dependencies with the older v2.7 version, which is therefor included in v3.x. The Tool
class from v2.7 is called antlr.Tool
, while the v3.x version is called org.antlr.Tool
. The latter is the one you should be using:
java -classpath antlr-3.1.3.jar org.antlr.Tool SimpleCalc.g
EDIT
Here's a little demo based on the grammar from the Wiki, but then with a bit of Python code added to it that evaluates the expression:
grammar SimpleCalc;
options {
language=Python;
}
@header {
import sys
import traceback
from SimpleCalcLexer import SimpleCalcLexer
}
@main {
def main(argv, otherArg=None):
char_stream = ANTLRStringStream(sys.argv[1])
lexer = SimpleCalcLexer(char_stream)
tokens = CommonTokenStream(lexer)
parser = SimpleCalcParser(tokens);
try:
print parser.eval()
except RecognitionException:
traceback.print_stack()
}
eval returns [value]
: add EOF {$value = $add.value}
;
add returns [value]
: m1=mult {$value = $m1.value} ( '+' m2=mult {$value += $m2.value}
| '-' m2=mult {$value -= $m2.value}
)*
;
mult returns [value]
: a1=atom {$value = $a1.value} ( '*' a2=atom {$value *= $a2.value}
| '/' a2=atom {$value /= $a2.value}
)*
;
atom returns [value]
: NUMBER {$value = float($NUMBER.text)}
| '(' add ')' {$value = $add.value}
;
NUMBER : DIGIT+ ('.' DIGIT*)?;
WHITESPACE : ('\t' | ' ' | '\r' | '\n')+ {$channel = HIDDEN;};
fragment DIGIT : '0'..'9' ;
Generating a lexer/parser and evaluating an expression:
java -cp antlr-3.1.3.jar org.antlr.Tool SimpleCalc.g
python SimpleCalcParser.py "5 * (8 + 2)"
produces the output:
50.0