Manuscript Offprint
arXiv:2610.39224PyReprism: A Shared Lexical Foundation for Polyglot Code Analysis
Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.
Daniel Ogenrwot Ⓤ, Jorge Gonzalo Delgado Cervantes Ⓤ, Richard Sserunjogi Ⓤ, John Businge Ⓤ
University of Nevada Las Vegas
Las Vegas, USA
{ogenrwot, delgaj10, sserunjo}@unlv.nevada.edu, john.businge@unlv.edu
Abstract—Empirical studies of polyglot repositories apply the same analyses to every language of a corpus, such as separating code from comments, computing metrics, normalizing identifiers, and screening for duplicated code. Assembling them from specialized tools introduces differences in language coverage, measurement conventions, and workflows. We present PYREPRISM, an open-source Python library and command-line tool that separates language-specific lexical processing from reusable analyses: each language declares its lexical surface, which yields a lossless stream of typed tokens, and every analysis is implemented once on that stream. Across 187 built-in languages, language-specific declarations have a median length of 16 lines, and no language overrides a shared analysis. Against a reference parser, comment and string classification reach F1 scores of 0.994 and 0.958 on 1,412 files in 12 languages that were never used to develop the tokenizer. In a study of a polyglot repository, one tool replaces three; with matched inputs and analysis operations, it takes 2.9s instead of 8.7s on one core. We also characterize where lexical analysis falls short, in complexity estimation and clone screening.
Tool: https://github.com/unlv-evol/PyReprism Video: https://youtu.be/IXc-NP5t4GU
Index Terms—mining software repositories, multi-language analysis, lexical analysis, software metrics, code clones
I. INTRODUCTION
Software projects frequently combine multiple programming languages [1], so empirical studies of them must apply the same analyses to every language of a corpus, such as measuring code characteristics, separating comments from code, normalizing identifiers, and detecting duplicated code [2]. Since no single tool supports all of these analyses for all languages, researchers typically combine tools that differ in language coverage, supported operations, and measurement conventions. Such differences can change a study's outcome: tools implementing the same metrics produce different values for the same systems [3].
To put this challenge into perspective, consider OpenPAI [4], which we use as a running example throughout this paper. OpenPAI combines Bash, Python, Java, JavaScript, and TypeScript, so analyzing its documentation, complexity, and duplication requires tools that differ in both language coverage and measurement conventions. In our evaluation, we analyzed it with CLOC [5], LIZARD [6], and JSCPD [7], and the three tools did not even analyze the same files: of the 718 files in these languages, CLOC analyzed 667, LIZARD 408, JSCPD 735, because it counts JSX and TSX files twice.
Per-language measurements from these tools therefore cover different sets of files, which the researcher must identify, reconcile, and report before any comparison. This motivates the shared lexical foundation of PYREPRISM, in which analyses and their measurement policies are implemented once and reused across languages, so that every analysis sees the same files under the same conventions.
Researchers could instead build analyses on language infrastructure such as Pygments [8] or tree-sitter [9], but it provides tokens or syntax trees rather than metrics. Researchers must still decide how token categories map to code and comment lines, what counts as a decision point, and which tokens to rename, and then implement and validate each analysis. Specialized tools thus offer complete analyses for different sets of languages with their own conventions, while infrastructure offers broad coverage but no analyses. To the best of our knowledge, no existing tool lets researchers implement an analysis and its measurement policy once and apply it to every language of a study. Yet many of these analyses need only lexical information, including line accounting, Halstead [10] and approximate McCabe [11] metrics, identifier normalization, n-gram naturalness [12], and fingerprint-based clone screening [13]. This raises a practical question: can a shared lexical representation support reusable analyses across languages with sufficient accuracy for empirical research, and what trade-offs does this introduce in precision, extensibility, and performance?
We present PYREPRISM, whose main contribution is a set of analyses implemented once and reused for every supported language. Each language declares only its lexical surface (file extensions, keywords, and comment and string syntax), from which PYREPRISM produces a lossless stream of typed tokens. All analyses operate on this stream, from line classification and metrics to normalization, n-grams, fingerprints, and clone and diff reports. Measurement policies, such as when a string counts as documentation, are therefore defined once and applied uniformly to all 187 built-in languages. This does not remove methodological choices, but it makes them explicit and consistent.
Our evaluation shows that no language overrides a shared analysis, that comment and string classification reach F1 scores of 0.994 and 0.958 against a reference parser on held-out files, and that in the OpenPAI study one tool replaces three and runs a matched workload about three times faster, although it is slower than CLOC for counting lines alone. It also characterizes where lexical analysis falls short: complexity estimates rank files well but are not interchangeable with syntax-aware values, and lexical clone screening finds copied and lightly edited fragments but few natural clones.
PYREPRISM is an MIT-licensed Python package on PyPI (pip install PyReprism), with source code on GitHub [14] and documentation on Read the Docs [15]. A replication package [16] with all sampled files, scripts, runtimes, and tool versions accompanies this paper.
Lexical infrastructure. Pygments [8] provides lexical analysis for more than 500 languages and is the closest underlying infrastructure to our approach, but its token categories serve syntax highlighting rather than measurement. Researchers using it must still map each token category to each measurement, handle lexer-specific behavior, and implement and validate every analysis themselves. PYREPRISM provides this missing layer: 32 token-based analysis methods and modules for metrics, fingerprints, clones, n-grams, diffs, and batch processing, shared by all 187 languages. Because its measurement policies refer only to token types (Section III-A), they apply unchanged whether tokens come from the default dependency-free engine or from an optional Pygments engine, although the engines classify some tokens differently (Section IV-B).
Syntax-aware infrastructure. TREE-SITTER [9] and SR-CML [21] expose structured representations of source code that enable analyses beyond lexical tokens, but depend on per-language parsing support. Separating language-specific front ends from analyses is a well-known design principle; our contribution is a validated set of lexical analyses built on it, together with an empirical characterization of when they suffice (Section IV). An optional [ast] extension provides per-function measurements for languages with an available parser.
III. TOOL DESCRIPTION
A. Architecture and Token Stream
Fig. 1 shows the architecture of PYREPRISM. In the language-specific layer (top), a registry holds the lexical declaration of

LANGUAGE-INDEPENDENT: Implemented once (1.7k lines), reused by every language
Fig. 1: Architecture of PYREPRISM.
each language (Section III-B), and a tokenization engine turns source code into a token stream: either the dependency-free default engine, which uses each language's regular expressions, or an optional Pygments engine. In the language-independent layer (bottom), every analysis consumes only that stream and is available through the Python API and a pyreprism subcommand.
Each token has one of eight types (comment, string, number, keyword, operator, identifier, whitespace, or other), a text, offsets, and a line number. The stream is lossless: concatenating the token texts reproduces the input exactly. Transformations are filters that keep the order, content, and positions of the tokens they do not select, so their correctness depends on token classification (Section IV-B). Policies that would otherwise differ between tools are defined once. A Python string that starts a line is a docstring and hence a comment (a lexical approximation of the first statement of a module, class, or function), as is a shebang line; preprocessor directives are code; a line is a comment line if it lies within a comment and contains no code, including blank lines inside block comments; and cyclomatic complexity is 1 plus the number of decision tokens in a file. Language-specific lexical rules, such as nested comments, template literals, and regular-expression literals, stay in the language modules.
B. Adding a Language
A module declares a file extension, keywords, and a comment regular expression whose named comment and noncomment groups separate comments from code and keep string literals whole (Listing 1); string, number, and operator patterns have defaults that a module may override. Languages sharing comment syntax (C-style, hash, Lisp-style) are rows of a family table, and those requiring context, such as COBOL's column-sensitive comments or nested /* */ comments, use small dedicated scanners. A registered language inherits every
Listing 1: TOML declared as a module; all analyses are inherited.
@LanguageRegistry.register class Toml(BaseLanguage): @classmethod def file_extension(cls): return ".toml" @classmethod def keywords(cls): return ["true", "false"] @classmethod def comment_regex(cls): return re.compile(r' (?P<comment>#.?$)' r' | (?P<noncomment>["^"])|.["^#"]*)', re.DOTALL | re.MULTILINE)
Listing 3: Command-line analyses of OpenPAI (pai: microsoft/pai at commit 43269e7; output abridged).
| % pyreprism scan pai --include '.py' --include '.java' \ --include '.js' --include '.ts' --include '.sh' |
|---|
| language files code comment blank |
| Bash 174 2018 3714 1370 |
| Java 148 13510 3738 3248 |
| JavaScript 124 12848 2445 1302 |
| Python 156 8820 2651 3490 |
| TypeScript 30 2774 480 346 |
Listing 2: The workflow of Section IV-D through the Python API (abridged).
from PyReprism import batch, clones EXTS = [".py", ".java", ".js", ".ts", "*.sh"]
def analyze(src, lang): # runs once per file return lang.code_metrics(src), lang.normalize(src)
results = batch.map_files("pai", analyze, include=EXTS, jobs=0) # all cores for path, lang, (m, norm) in results: rows[lang.name].add(m) # same keys for all languages save(path, norm) # ids renamed, no comments files = [path for path, _, _ in results] # same files as above report = clones.detect(files, jobs=0) # Type-1/2 fragments
analysis, so the TOML module in Listing 1 immediately supports the full analysis layer.
C. Usage
Listing 2 shows the Python API as used in the study of Section IV-D. One batch.map_files pass applies analyze to every matching file on all cores, adds each file's metrics to a per-language row, and saves a normalized, comment-free copy. Because every language returns the same metric keys, the rows need no mapping between tools. clones.detect then receives the paths of the same files and reports Type-1/2 duplicated fragments and the share of duplicated lines. The full script has 40 code lines. Each analysis is also a CLI subcommand (Listing 3): scan counts lines per language, and clones reports duplicated fragments as text, JSON, SARIF, Markdown, or HTML. With --max-duplication, clones exits with a nonzero status, so a container image and a GitHub Action can use it as a gate in continuous integration.
IV. TOOL EVALUATION
We evaluate how much language-specific code the shared layer requires, how accurately it classifies tokens, how it agrees with established tools, and what it changes in an end-to-end study. We use 12 languages (Python, Java, C, C++, JavaScript, TypeScript, Go, C#, Scala, Ruby, Bash, and CSS), which cover the main comment conventions and have reference tools; nine are among the ten most used on GitHub [22]. From 620,942 files in 501 cloned repositories and local open-source installations, we drew 250 files per language (300 B–200 KB, round-robin across projects, deduplicated by content): 3,000 files from 748 projects. A disjoint draw of 120 files per language serves the comparison with CLOC. The replication package documents the sampling and contains every file, script, and result.
A. Extensibility
We measure the language-specific code the shared layer requires, not the effort to write or validate it. A recent release added 42 languages, including COBOL and three nested-comment languages, in 392 lines of code and 140 lines of tests without changing any analysis. Of all 187 languages, 145 are modules, 38 are family-table rows, and 4 use a scanner. Declarations have a median of 16 lines (90th percentile: 25; maximum: 73), and none overrides any of the 32 token-based analysis methods of the base class, which together with the shared modules form 1,672 lines of code. Short declarations are not necessarily correct ones: several misclassified tokens until a reference parser exposed them (Section IV-B).
B. Token Classification
Losslessness shows that no character is lost, not that each is classified correctly. We labeled every non-whitespace character as comment, string, or code from PYREPRISM's tokens and from a tree-sitter parse, a reference parser rather than an infallible ground truth, which applies PYREPRISM's docstring policy; C macro bodies, which tree-sitter does not parse, are not scored. We report character-level F1, micro-averaged per language (Table I); excluding the 370 files with parse errors changes scores by at most 0.003.
Comparisons on two earlier development samples exposed misclassified strings in eight languages, characters dropped by comment removal in 21 modules, and preprocessor lines taken for comments by the Pygments engine; all were fixed before the samples reported here (details in the replication package). On the main sample, the default engine reaches pooled F1 scores of 0.998 for comments and 0.967 for strings (Pygments engine: 0.998 and 0.936). Because this sample partly overlaps the development samples, our key estimate comes from a held-out sample of 1,412 files from 480 projects that excludes every development file and OpenPAI. On it, the tokenizer reaches 0.994 and 0.958 (unweighted means over languages: 0.996 and 0.954; per-language scores in Table S1 of the replication package). Ruby and Go are thinly represented in it (92 Ruby files from 2 projects; Go files from 5 projects).
TABLE I: Token classification F1 against tree-sitter, files with incorrectly deleted code (Del.), and exact comment-line agreement with CLOC. F1 and Del. use 250 files per language.
| Lang. | Cmt. | Str. | Del. | CLOC | Lang. | Cmt. | Str. | Del. | CLOC |
|---|---|---|---|---|---|---|---|---|---|
| Python | 0.98 | 0.97 | 3 | 0.38 | Go | 1.00 | 1.00 | 0 | 0.78 |
| Java | 1.00 | 1.00 | 0 | 0.92 | C# | 1.00 | 0.87 | 0 | 0.90 |
| C | 1.00 | 1.00 | 0 | 0.80 | Scala | 1.00 | 0.97 | 1 | 0.92 |
| C++ | 1.00 | 0.99 | 4 | 0.93 | Ruby | 1.00 | 0.93 | 15 | 0.97 |
| JS | 1.00 | 0.99 | 0 | 0.89 | Bash | 0.99 | 0.92 | 9 | 0.12 |
| TS | 1.00 | 0.97 | 0 | 0.84 | CSS | 1.00 | 0.84 | 1 | 0.87 |
Concatenating the tokens reproduced every file exactly, but a high F1 does not make every transformation safe. Judged by the reference, remove_comments deletes code in 33 of the 3,000 files (1.1%; column “Del.” of Table I) and in 7 of the 1,412 held-out files (0.5%), and leaves part of a comment in 24 files. Bash and Ruby account for 24 of the 33 files, mostly through heredocs and string interpolation, which need more context than a lexer has. Studies that use transformed Bash or Ruby source should therefore validate it, for example by parsing the output.
C. Agreement with Established Tools
Line classification. On the second draw (1,440 files that CLOC recognized), comment-line counts match CLOC exactly for 0.78 of the files (95% CI [0.76, 0.80]; Table I), with a median absolute difference of 0 lines and Spearman’s in every language. The low rates for Python and Bash reflect policy: CLOC counts blank lines inside docstrings and block comments as blank, and shebang lines as code.
Cyclomatic complexity. The lexical estimate counts decision tokens per file rather than walking a syntax tree. Since both it and the references grow with file size, Fig. 2 reports size-adjusted Spearman correlations. Against RADON (Python) and LIZARD (others), they range from 0.55 (Ruby) to 0.80 (JavaScript), and SCC’s token-based estimate is within 0.06 of PYREPRISM in every language. Low values mostly reflect definitions: LIZARD adds 1 per function, a median 62% of a Java file’s total. Against LIZARD’s decision points alone, the partial is 0.94 [0.89, 0.97] for Java and 0.79 (Ruby) to 0.98 (Go) for the other languages, yet only 60–98% of files are within , so the values are not interchangeable. For ranking, the lexical estimate recovers 55–100% of the files in LIZARD’s top 10% by decision points, against 22–79% for file size alone, and beats size in every language. It thus suits shortlisting complex files for inspection, not labeling individual files or replacing syntax-aware values.
D. An End-to-End Polyglot Study
We implemented the OpenPAI study of the introduction (microsoft/pai at commit 43269e7) with PYREPRISM (Listing 2) and with CLOC, LIZARD, and JSCPD (Table II).
Functional coverage. Run with their defaults, both approaches report per-language files, code lines, comment density, complexity, and duplicated lines, and write comment-free copies. The toolchain needs three runtimes and a mapping

Fig. 2: Agreement of lexical cyclomatic complexity with RADON (Python) and LIZARD (other languages). Dots and squares show size-adjusted Spearman for PYREPRISM and SCC, respectively; diamonds show unadjusted . Error bars indicate bootstrap 95% CIs.
between three sets of language names, and its tools see different subsets of the 718 files: LIZARD skips Bash and style sheets and reports only files with functions (408), CLOC skips files that duplicate another (667), JSCPD skips empty and very large files but counts the 33 JSX and TSX files twice (735), and PYREPRISM skips build/ directories (691). None of these defaults is a defect, but each changes the denominator of a per-language measurement and must be found and reported when tools are combined. Moreover, CLOC’s stripped copies keep trailing comments, no tool renames identifiers, and definitions change results: for Java, PYREPRISM finds 6.2% duplicated lines for identical token sequences and 37.4% once identifiers are renamed, against 3.6% for JSCPD. These default runs take 5.0 s and 7.6 s on one core.
Equivalent workload. To compare runtime on equal work, both approaches processed the same 680 files (the 691 minus empty files and three one-line files that JSCPD always skips, with CLOC’s duplicate check off and JSCPD’s size limits raised) with four matched operations: line counts, complexity for the six languages LIZARD supports, Type-1 duplicated fragments, and comment-free copies. The tools keep their own algorithms, so their outputs differ. Table II reports the median wall-clock time of ten runs, start-up included, on a shared Apple M3 (IQR at most 0.3 s; all timings, versions, and flags are in the replication package). This shows a faster workflow, not faster individual operations: on the 3,000 corpus files, pyreprism scan counts 480 files per second on one core against 880 for CLOC (2,094 vs. 1,866 on eight), and the compiled SCC reaches 35,714 but exposes no tokens for other analyses. PYREPRISM is thus not the fastest choice for an isolated operation; its advantage in the evaluated workflow comes from serving several analyses through one representation.
E. Clone Screening
Normalized fingerprints separate constructed Type-1, Type-2, and Type-3 clones of POJ-104 C/C++ solutions [23], [24]
TABLE II: OpenPAI workflow comparison. Runtime is the median of ten runs, including start-up.
| PYREPRISM | CLOC / LIZARD / JSCPD | |
|---|---|---|
| Functional coverage (each tool's defaults) | ||
| Tools / runtimes | 1 / 1 | 3 / 3 (Perl, Python, Node.js) |
| Driver script (code lines) | 40 | 60 |
| Files analyzed (of 718) | 691 | 667 / 408a / 735b |
| Complexity for Bash | yes | no |
| Identifier normalization | yes | no |
| Equivalent workload (680 files, same operations) | ||
| Time, 1 core | 2.9 s | 8.7 s |
| Time, 8 cores | 1.6 s | 9.0 s |
<sup>a</sup>files with functions; <sup>b</sup>JSX and TSX files counted twice.
from unrelated pairs (AUC 1.0), a setting that favors token fingerprints by design. On 1,000 natural Java clone pairs from BigCloneBench [25], however, PYREPRISM detects only 12 (recall 0.012) and JSCPD 2, with no false positives on 1,000 non-clone pairs, too few detections to establish precision. Lexical clone reports thus screen for copied and lightly edited fragments; they are not a general clone detector. The replication package contains both experiments in full.
V. LIMITATIONS
First, PYREPRISM relies on lexical information and cannot recover control flow, data flow, or bindings. Its complexity measures are therefore approximate, context-dependent constructs may be misclassified, and lexical clone screening misses weakly syntactic and semantic clones. Second, all 187 languages are covered by regression tests, but comparison with a reference parser and established tools covers only 12 languages; accuracy for the remaining languages is therefore not established. Third, a uniform measurement policy does not imply semantic equivalence across languages: constructs that document code or contribute to complexity can differ by language, which limits direct cross-language interpretation. Fourth, only the held-out sample estimates tokenizer accuracy on previously unseen files, and its coverage is limited for Ruby and Go. Finally, the evaluation has broader generalizability limits: the corpus is drawn from open-source software available on one machine rather than a defined project population, extensibility is measured by implementation size rather than developer effort, and the end-to-end workflow is evaluated on a single repository using scripts written by the authors.
VI. CONCLUSION
PYREPRISM implements analyses once on a shared, lossless token stream and applies them across languages. Our evaluation shows that this reduces language-specific implementation, classifies comments and strings close to a reference parser on unseen files, and simplifies a polyglot workflow; it also shows that lexical measures are not interchangeable with syntax-aware ones and that lexical clone screening misses semantic clones. A shared lexical representation is thus a practical foundation for analyses of heterogeneous repositories that do not depend on detailed program semantics. Next, we validate
the [ast] extension and integrate function-level complexity into the shared analyses.
REFERENCES
[1] W. Li, A. Marino, H. Yang, N. Meng, L. Li, and H. Cai, "How are multilingual systems constructed: Characterizing language use and selection in open-source multilingual software," ACM Transactions on Software Engineering and Methodology, vol. 33, no. 3, 2024. [2] C. V. Lopes, P. Maj, P. Martins, V. Saini, D. Yang, J. Zityn, H. Sajnani, and J. Vitek, "Déjávau: A map of code duplicates on GitHub," in Proc. ACM Program. Lang. (OOPSLA), vol. 1, 2017, pp. 1–28. [3] R. Lincke, J. Lundberg, and W. Löwe, "Comparing software metrics tools," in Proceedings of the International Symposium on Software Testing and Analysis. ACM, 2008, pp. 131–142. [4] Microsoft, "OpenPAI: Open platform for AI," https://github.com/microsoft/pai, commit 43269e7. [5] A. Danial, "clos: Count lines of code," https://github.com/AIDanial/cloc, 2006. [6] T. Yin, "Lizard: An extensible cyclomatic complexity analyzer," https://github.com/terryyin/lizard, 2012. [7] A. Kucherenko et al., "jscpd: Copy/paste detector for programming source code," https://github.com/kucherenko/jscpd, 2016. [8] G. Brandl et al., "Pygments: Python syntax highlighter," https://pygments.org/, 2006. [9] M. Brunsfeld, "Tree-sitter: A new parsing system for programming tools," https://tree-sitter.github.io/, 2018. [10] M. H. Halstead, Elements of Software Science. Elsevier North-Holland, 1977. [11] T. J. McCabe, "A complexity measure," IEEE Transactions on Software Engineering, no. 4, pp. 308–320, 1976. [12] A. Hindle, E. T. Barr, Z. Su, M. Gabel, and P. Devanbu, "On the naturalness of software," in Proc. 34th Int. Conf. Software Engineering (ICSE), 2012, pp. 837–847. [13] S. Schleimer, D. S. Wilkerson, and A. Aiken, "Winnowing: Local algorithms for document fingerprinting," in Proc. ACM SIGMOD Int. Conf. Management of Data, 2003, pp. 76–85. [14] D. Ogenrwot, J. G. Delgado Cervantes, R. Sserunjogi, and J. Businge, "PyReprism: Source code repository," https://github.com/unlv-evol/PyReprism, 2026. [15] —, "PyReprism documentation," https://pyreprism.readthedocs.io, 2026. [16] D. Ogenrwot, C. Jorge Gonzalo Delgado, R. Sserunjogi, and J. Businge, "Replication package for pyreprism: A shared lexical foundation for polyglot code analysis," Oct. 2026. [Online]. Available: https://doi.org/10.5281/zenodo.23612247 [17] B. Boyter, "scc: Sloc, clos and code," https://github.com/boyter/scc, 2018. [18] M. Lacchia, "Radon: Python tool that computes various code metrics," https://radon.readthedocs.io/, 2012. [19] T. Kamiya, S. Kusumoto, and K. Inoue, "CCFinder: A multilinguistic token-based code clone detection system for large scale source code," IEEE Transactions on Software Engineering, vol. 28, no. 7, pp. 654–670, 2002. [20] H. Sajnani, V. Saini, J. Svajlenko, C. K. Roy, and C. V. Lopes, "SourceererCC: Scaling code clone detection to big-code," in Proc. 38th Int. Conf. Software Engineering (ICSE), 2016, pp. 1157–1168. [21] M. L. Collard, M. J. Decker, and J. I. Maletic, "srcML: An infrastructure for the exploration, analysis, and manipulation of source code," in Proc. IEEE Int. Conf. Software Maintenance (ICSM), 2013, pp. 516–519. [22] GitHub, "Octoverse: AI leads Python to top language as the number of global developers surges," https://github.blog/news-insights/octoverse/octoverse-2024/, Oct. 2024. [23] L. Mou, G. Li, L. Zhang, T. Wang, and Z. Jin, "Convolutional neural networks over tree structures for programming language processing," in Proc. 30th AAAI Conf. Artificial Intelligence, 2016, pp. 1287–1293. [24] S. Lu, D. Guo, S. Ren et al., "CodeXGLUE: A machine learning benchmark dataset for code understanding and generation," in Proc. NeurIPS Datasets and Benchmarks Track, 2021. [25] J. Svajlenko, J. F. Islam, I. Keivanloo, C. K. Roy, and M. M. Mia, "Towards a big data curated benchmark of inter-project code clones," in Proc. IEEE Int. Conf. Softw. Maintenance Evol. (ICSME), 2014, pp. 476–480.