private OperatorNode<StatementOperator> convertProgram(ParserRuleContext program,
                                                           yqlplusParser parser,
                                                           String programName) {
        Scope scope = new Scope(parser, programName);
        List<OperatorNode<StatementOperator>> stmts = new ArrayList<>();
        int output = 0;
        for (ParseTree node : program.children) {
            if (!(node instanceof ParserRuleContext ruleContext)) continue;
            if (ruleContext.getRuleIndex() != yqlplusParser.RULE_statement)
                throw new ProgramCompileException("Unknown program element: " + node.getText());

            // ^(STATEMENT_QUERY source_statement paged_clause? output_spec?)
            StatementContext statementContext = (StatementContext) ruleContext;
            Source_statementContext source_statement = statementContext.output_statement().source_statement();
            OperatorNode<SequenceOperator> query;
            if (source_statement.getChildCount() == 1) {
                query = convertQuery( source_statement.query_statement().getChild(0), scope);
            } else {
                query = convertQuery(source_statement, scope);
            }
            String variable = "result" + (++output);
            boolean isCountVariable = false;
            ParseTree outputStatement = node.getChild(0);
            Location location = toLocation(scope, outputStatement);
            for (int i = 1; i < outputStatement.getChildCount(); ++i) {
                ParseTree child = outputStatement.getChild(i);
                if ( getParseTreeIndex(child) != yqlplusParser.RULE_output_spec)
                    throw new ProgramCompileException( "Unknown statement attribute: " + child.toStringTree());

                Output_specContext outputSpecContext = (Output_specContext) child;
                variable = outputSpecContext.ident().getText();
                if (outputSpecContext.COUNT() != null) {
                    isCountVariable = true;
                }
            }
            scope.defineVariable(location, variable);
            stmts.add(OperatorNode.create(location, StatementOperator.EXECUTE, query, variable));
            stmts.add(OperatorNode.create(location, isCountVariable ? StatementOperator.COUNT:StatementOperator.OUTPUT, variable));
        }
        return OperatorNode.create(StatementOperator.PROGRAM, stmts);
    }
Translate the ANTLR grammar into the logical representation.
container-search/src/main/java/com/yahoo/search/yql/ProgramParser.java