What's wrong with this example ANTLR 3 Python grammer? -
i'm trying learn use antlr, , seem have come across error while following "tutorial": https://theantlrguy.atlassian.net/wiki/display/antlr3/five+minute+introduction+to+antlr+3
essentially, create file simplecalc.g:
grammar simplecalc; options { language = python; } tokens { plus = '+' ; minus = '-' ; mult = '*' ; div = '/' ; } @header { import sys import traceback simplecalclexer import simplecalclexer } @main { def main(argv, otherarg=none): char_stream = antlrfilestream(sys.argv[1]) lexer = simplecalclexer(char_stream) tokens = commontokenstream(lexer) parser = simplecalcparser(tokens); try: parser.expr() except recognitionexception: traceback.print_stack() } /*------------------------------------------------------------------ * parser rules *------------------------------------------------------------------*/ expr : term ( ( plus | minus ) term )* ; term : factor ( ( mult | div ) factor )* ; factor : number ; /*------------------------------------------------------------------ * lexer rules *------------------------------------------------------------------*/ number : (digit)+ ; whitespace : ( '\t' | ' ' | '\r' | '\n'| '\u000c' )+ { $channel = hidden; } ; fragment digit : '0'..'9' ;
when run antlr tools
java -classpath antlr-3.1.3.jar antlr.tool simplecalc.g
i receive errors, starting first line:
antlr parser generator version 2.7.7 (20060906) 1989-2005 simplecalc.g:2:1: unexpected token: grammar error: token stream error reading grammar(s): simplecalc.g:15:1: unexpected char: '@' simplecalc.g:2:1: rule grammar trapped: simplecalc.g:2:1: unexpected token: grammar tokenstreamexception: unexpected char: '@'
this leads me believe i'm doing stupid, i'm not sure what. matter appreciated.
thanks,
chris covert
you're using wrong tool
class generate lexer , parser. antlr 3.x versions still have dependencies older v2.7 version, therefor included in v3.x. tool
class v2.7 called antlr.tool
, while v3.x version called org.antlr.tool
. latter 1 should using:
java -classpath antlr-3.1.3.jar org.antlr.tool simplecalc.g
edit
here's little demo based on grammar wiki, bit of python code added evaluates expression:
grammar simplecalc; options { language=python; } @header { import sys import traceback 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 lexer/parser , evaluating expression:
java -cp antlr-3.1.3.jar org.antlr.tool simplecalc.g python simplecalcparser.py "5 * (8 + 2)"
produces output:
50.0
Comments
Post a Comment