As the title says: when propagating values from the parsing tree the root node returns None when I call the self.visitChildren(ctx) there. I can see that the other nodes propagate the values upside but the root node is the only that is receiving None.
I am using ANTLR 4.10.1 and Python antlr4-python3-runtime 4.10.
It prints the following when I entered 393939393 and CTRL-D:
Number: 393939393
Atom: {'type': 'number', 'value': '393939393'}
SExpr: None
None
I tried with the follow slight modification of the S-Expression parser and next I show my visitor script:
/*
Port to Antlr4 by Tom Everett
*/
grammar sexpr;
sexpr
/* : item* EOF */
: item EOF
;
item
: atom
| list_
/* | LPAREN item DOT item RPAREN */
;
list_
: LPAREN item* RPAREN
;
atom
: string
| symbol
| number
/* | DOT */
;
string: STRING ;
symbol: SYMBOL ;
number: NUMBER ;
STRING
: '"' ('\\' . | ~ ('\\' | '"'))* '"'
;
WHITESPACE
: (' ' | '\n' | '\t' | '\r')+ -> skip
;
NUMBER
: ('+' | '-')? (DIGIT)+ ('.' (DIGIT)+)?
;
SYMBOL
: SYMBOL_START (SYMBOL_START | DIGIT)*
;
LPAREN
: '('
;
RPAREN
: ')'
;
DOT
: '.'
;
fragment SYMBOL_START
: ('a' .. 'z')
| ('A' .. 'Z')
| '+'
| '-'
| '*'
| '/'
| '.'
;
fragment DIGIT
: ('0' .. '9')
;
Python 3 ANTLR visitor code
#!/usr/bin/python3
import sys
from antlr4 import *
from sexprLexer import sexprLexer
from sexprParser import sexprParser
from sexprVisitor import sexprVisitor
class SExprVisitor(sexprVisitor):
def visitSexpr(self, ctx):
r = self.visitChildren(ctx)
print("SExpr: %s" % r)
return r
def visitItem(self, ctx):
r = self.visitChildren(ctx)
return r
def visitAtom(self, ctx):
r = self.visitChildren(ctx)
print("Atom: %s" % r)
return r
def visitString(self, ctx):
print("String: %s" % ctx.getText())
return {'type':'string', 'value':ctx.getText()}
def visitNumber(self, ctx):
print("Number: %s" % ctx.getText())
return {'type':'number', 'value':ctx.getText()}
def visitSymbol(self, ctx):
print("Symbol: %s" % ctx.getText())
return {'type':'symbol', 'value':ctx.getText()}
def visitor_main(argv):
input_stream = StdinStream()
lexer = sexprLexer(input_stream)
stream = CommonTokenStream(lexer)
parser = sexprParser(stream)
tree = parser.sexpr()
visitor = SExprVisitor()
output = visitor.visit(tree)
print(output)
def main(argv):
visitor_main(argv)
if __name__ == '__main__':
main(sys.argv)