#!/usr/bin/env python3

import sys
import os

# Add the src directory to the Python path
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src'))

# Check what's in the parser module
import alloy.parser as parser_module

# Look at the parser to understand the states
parser = parser_module.get_global_parser()

print("Looking at parser states and rules...")
print(f"Parser: {parser}")

# Check if we can access the parser table
if hasattr(parser, 'lr_table'):
    print("LR Table found")
    table = parser.lr_table
    
    # Look for state 47 and 87
    if hasattr(table, 'lr_action'):
        print("Actions table found")
        if 47 in table.lr_action:
            print(f"State 47 actions: {table.lr_action[47]}")
        if 87 in table.lr_action:
            print(f"State 87 actions: {table.lr_action[87]}")
    
    if hasattr(table, 'lr_goto'):
        print("Goto table found")
        if 47 in table.lr_goto:
            print(f"State 47 gotos: {table.lr_goto[47]}")
        if 87 in table.lr_goto:
            print(f"State 87 gotos: {table.lr_goto[87]}")
else:
    print("No LR table found")

# Try to look at the grammar
print("\nLooking for grammar rules...")
if hasattr(parser, 'lrparser'):
    print("LR Parser found")
    lrparser = parser.lrparser
    if hasattr(lrparser, 'grammar'):
        grammar = lrparser.grammar
        print(f"Grammar: {grammar}")
        
        # Look for productions with IDENTIFIER IDENTIFIER pattern
        if hasattr(grammar, 'Productions'):
            for i, prod in enumerate(grammar.Productions):
                if hasattr(prod, 'str'):
                    rule_str = str(prod)
                    if 'IDENTIFIER IDENTIFIER' in rule_str:
                        print(f"Rule {i}: {rule_str}")
else:
    print("No LR parser found")