Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-5-haiku-20241022· AnthropicHugging Face LMArena Human Preference 140K

Fix this PegJS grammer to extract filenames and from markdown code blocks { const DEBUG…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-2025-04-16’s answer.
  1. User

    Fix this PegJS grammer to extract filenames and from markdown code blocks

    { const DEBUG = true;

    function normalizePath(pathStr) { const normalized = pathStr.trim().replace(/\/g, '/'); if (DEBUG) console.log([NormalizePath] Input: "${pathStr}", Output: "${normalized}"); return normalized; }

    function logMatch(ruleName, success, text) { if (!DEBUG) return; // location is a variable, not a function // const loc = location(); const status = success ? "MATCH" : "FAIL"; const snippet = text.replace(/\n/g, '\n').substring(0, 60); console.log([PEG ${status}] ${ruleName} T:"${snippet}"); } }

    Document = segments:Segment* EOF { if (DEBUG) console.log("[Document] EOF reached."); return segments.filter(s => s && s.format === "Markdown Block"); }

    Segment = FileBlock / IgnorableContent

    FileBlock = &(PathIndicatorLine) indicator:PathIndicatorLine blanks:BlankLine* block:CodeBlock { logMatch("FileBlock", true, Path: ${indicator.path}); return { path: indicator.path, content: block, format: "Markdown Block" }; }

    IgnorableContent = !(PathIndicatorLine BlankLine* CodeBlock) consumed:( CodeBlock / IgnorableLine ) { return null; }

    PathIndicatorLine = content:(StandalonePathLine / PathInHeading / PathInParagraph) nl:(Newline / EOF) { return content; } BlankLine = Indent nl:Newline { return null; }

    IgnorableLine = $( (!Newline .)* ) Newline { logMatch("IgnorableLine", true, text()); return null; }

    StandalonePathLine = Indent path:FilePath Indent { return { path: normalizePath(path) }; } PathInHeading = HASH+ Indent (PathMarker ":"? Indent)? path:FilePath (""?)? Indent { return { path: normalizePath(path) }; } PathInParagraph = !CodeFenceStart Indent path:(QuotedFilePath / UnquotedFilePath) suffix:( (""?)? Indent TextAfterPath? Indent (":" Indent)? ) { return { path: normalizePath(path) }; }

    QuotedFilePath = "" path:FilePath "" { return path; } UnquotedFilePath = path:FilePath { return path; }

    // Allow any extension, not just a fixed list FilePath = path:(NameSegment(PathSeparatorNameSegment)∗("."[a−zA−Z0−9]+)?)returnpath;NameSegment=( NameSegment ( PathSeparator NameSegment )* ( "." [a-zA-Z0-9_]+ )? ) { return path; } NameSegment = ( [a-zA-Z0-9_@.-]+ ) / ".." PathSeparator = "/" / "\" // FileExtension = "." $( ("bash"i/"cjs"i/"config"i/"cs"i/"css"i/"dockerfile"i/"env"i/"gitignore"i/"go"i/"html"i/"java"i/"js"i/"json"i/"jsx"i/"less"i/"md"i/"mjs"i/"npmrc"i/"py"i/"scss"i/"sh"i/"sql"i/"toml"i/"ts"i/"tsx"i/"txt"i/"xml"i/"yaml"i/"yml"i/"zsh"i) )

    CodeBlock = start:CodeFenceLineStart content:CodeBlockContent end:CodeFenceLineEnd { const fullBlockText = start + content + end; logMatch("CodeBlock", true, fullBlockText); return content.replace(/\r?\n$/, ''); }

    CodeFenceLineStart = Indent CodeFenceStart LangTag? optionalWhitespace Newline { return text(); } CodeFenceLineEnd = Indent CodeFenceEnd optionalWhitespace (Newline / EOF) { return text(); } CodeBlockContent = $((CodeContentLine)*) // capture as string

    CodeContentLine = !(Indent CodeFenceEnd) line:$((!Newline .)* Newline) { return line; }

    CodeFenceStart = "" CodeFenceEnd = "" LangTag = $( [a-zA-Z0-9_-]+ ) optionalWhitespace = [ \t]*

    TextAfterPath = $( (!Newline .)* ) PathMarker = ("File" / "file" / "Path" / "path" / "Updating" / "updating" / "Creating" / "creating") Indent = [ \t]* Newline = "\n" / "\r\n" EOF = !. HASH = "#"

  2. Claude Assistant

    Here's an improved version of the PEG.js grammar for extracting filenames from markdown code blocks:

    {
      const DEBUG = true;
    
      function normalizePath(pathStr) {
        const normalized = pathStr.trim().replace(/\\/g, '/');
        if (DEBUG) console.log(`[NormalizePath] Input: "${pathStr}", Output: "${normalized}"`);
        return normalized;
      }
    
      function logMatch(ruleName, success, text) {
        if (!DEBUG) return;
        const status = success ? "MATCH" : "FAIL";
        const snippet = text.replace(/\n/g, '\\n').substring(0, 60);
        console.log(`[PEG ${status}] ${ruleName} T:"${snippet}"`);
      }
    }
    
    Document = segments:Segment* EOF {
      if (DEBUG) console.log("[Document] EOF reached.");
      return segments.filter(s => s && s.format === "Markdown Block");
    }
    
    Segment = FileBlock / IgnorableContent
    
    FileBlock =
      indicator:PathIndicatorLine
      blanks:BlankLine*
      block:CodeBlock
      {
        logMatch("FileBlock", true, `Path: ${indicator.path}`);
        return { path: indicator.path, content: block, format: "Markdown Block" };
      }
    
    IgnorableContent =
      !(PathIndicatorLine BlankLine* CodeBlock)
      consumed:( CodeBlock / IgnorableLine )
      { return null; }
    
    PathIndicatorLine = content:(StandalonePathLine / PathInHeading / PathInParagraph) nl:(Newline / EOF) { return content; }
    BlankLine = Indent nl:Newline { return null; }
    
    IgnorableLine =
      $( (!Newline .)* ) Newline
      {
        logMatch("IgnorableLine", true, text());
        return null;
      }
    
    StandalonePathLine = Indent path:FilePath Indent { return { path: normalizePath(path) }; }
    PathInHeading = HASH+ Indent (PathMarker ":"? Indent)? path:FilePath ("`"?)? Indent { return { path: normalizePath(path) }; }
    PathInParagraph =
      !CodeFenceStart
      Indent
      path:(QuotedFilePath / UnquotedFilePath)
      suffix:( ("`"?)? Indent TextAfterPath? Indent (":" Indent)? )
      { return { path: normalizePath(path) }; }
    
    QuotedFilePath = "`" path:FilePath "`" { return path; }
    UnquotedFilePath = path:FilePath { return path; }
    
    FilePath = path:$( NameSegment ( PathSeparator NameSegment )* ( "." [a-zA-Z0-9_]+ )? ) { return path; }
    NameSegment = $( [a-zA-Z0-9_@.-]+ ) / ".."
    PathSeparator = "/" / "\\"
    
    CodeBlock =
      start:CodeFenceLineStart
      content:CodeBlockContent
      end:CodeFenceLineEnd
      {
        const fullBlockText = start + content + end;
        logMatch("CodeBlock", true, fullBlockText);
        return content.replace(/\r?\n$/, '');
      }
    
    CodeFenceLineStart = Indent CodeFenceStart LangTag? optionalWhitespace Newline { return text(); }
    CodeFenceLineEnd = Indent CodeFenceEnd optionalWhitespace (Newline / EOF) { return text(); }
    CodeBlockContent = $((CodeContentLine)*) 
    
    CodeContentLine =
      !(Indent CodeFenceEnd)
      line:$((!Newline .)* Newline)
      { return line; }
    
    CodeFenceStart = "```"
    CodeFenceEnd = "```"
    LangTag = $( [a-zA-Z0-9_-]+ )
    optionalWhitespace = [ \t]*
    
    TextAfterPath = $( (!Newline .)* )
    PathMarker = ("File" / "file" / "Path" / "path" / "Updating" / "updating" / "Creating" / "creating")
    Indent = [ \t]*
    Newline = "\n" / "\r\n"
    EOF = !.
    HASH = "#"
    

    Key improvements and changes:

    1. Removed the commented-out FileExtension rule
    2. Simplified the FileBlock rule to remove the unnecessary lookahead
    3. Kept the core logic for extracting filenames from various markdown contexts
    4. Maintained the debug logging and path normalization functions

    This grammar should now more robustly extract filenames from markdown code blocks, supporting various formats like:

    • Standalone path lines
    • Paths in headings
    • Paths in paragraphs
    • Quoted and unquoted paths

    The parser will return an array of objects with path and content properties for each matched markdown code block with a preceding path indicator.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.