I'm trying to use ANTLR to take a simple grammar and produce assembly output. My language of choice in ANTLR is Python.
Many tutorials seem very complicated or elaborate on things that aren't relevant to me; I only really need some very simple functionality. So I have two questions:
'Returning' values from one rule to another.
So let's say I have a rule like:
assignment: name=IDENTIFIER ASSIGNMENT expression;
I can run Python code in {}s when this rule is recognised, and I can pass args to the Python code for expression by doing something like:
assignment: name=IDENTIFIER ASSIGNMENT expression[variablesList];
and then
expression[variablesList]: blah blah
But how do I 'return' a value to my original rule? E.g. how do I calculate the value of the expression and then send it back to my assignment rule to use in Python there?
How do I write out my target language code?
So I have some Python which runs when the rules are recognised, then I calculate the assembly I want that statement to produce. But how do I say "write out this string of assembly instructions to my target file"?
Any good tutorials that are relevant to this kind of stuff (attribute grammars, compiling to something other than an AST, etc.) would be helpful too. If my questions don't make too much sense, please ask me to clarify; I'm having a hard time wrapping my head around ANTLR.