code: def generate_parser_c_extension(
      grammar: Grammar,
      path: pathlib.PurePath,
      debug: bool = False,
      library_dir: Optional[str] = None,
  ) -> Any:
      """Generate a parser c extension for the given grammar in the given path
  
      Returns a module object with a parse_string() method.
      TODO: express that using a Protocol.
      """
      # Make sure that the working directory is empty: reusing non-empty temporary
      # directories when generating extensions can lead to segmentation faults.
      # Check issue #95 (https://github.com/gvanrossum/pegen/issues/95) for more
      # context.
      assert not os.listdir(path)
      source = path / "parse.c"
      with open(source, "w", encoding="utf-8") as file:
          genr = CParserGenerator(
              grammar,
              ALL_TOKENS,
              EXACT_TOKENS,
              NON_EXACT_TOKENS,
              file,
              debug=debug,
          )
          genr.generate("parse.c")
      compile_c_extension(
          str(source),
          build_dir=str(path),
          # Significant test_peg_generator speedups
          disable_optimization=True,
          library_dir=library_dir,
      )
  
imports: import importlib.util
  import io
  import os
  import pathlib
  import sys
  import textwrap
  import token
  import tokenize
  from typing import IO, Any, Dict, Final, Optional, Type, cast
  from pegen.build import compile_c_extension
  from pegen.c_generator import CParserGenerator
  from pegen.grammar import Grammar
  from pegen.grammar_parser import GeneratedParser
  from pegen.parser import Parser
  from pegen.python_generator import PythonParserGenerator
  from pegen.tokenizer import Tokenizer
label_desc: Use image node_id 7 for calling a global function with example usage: generate_parser_c_extension(grammar, path, debug, library_dir) without return types