Skip to main content
QUICK REVIEW

[Paper Review] Algorithmic Programming Language Identification

David Klein, Kyle I. Murray|arXiv (Cornell University)|Jun 21, 2011
Software Engineering ResearchComputer Science1 references10 citations
TL;DR

This paper presents a supervised machine learning approach for identifying programming languages in source code using statistical features, excluding comments and strings to improve accuracy. It outperforms existing tools like SourceClassifier, correctly identifying the language in 48% of test cases and ranking it in the top five for 88%, demonstrating a significant improvement over Bayesian baselines.

ABSTRACT

Motivated by the amount of code that goes unidentified on the web, we introduce a practical method for algorithmically identifying the programming language of source code. Our work is based on supervised learning and intelligent statistical features. We also explored, but abandoned, a grammatical approach. In testing, our implementation greatly outperforms that of an existing tool that relies on a Bayesian classifier. Code is written in Python and available under an MIT license.

Motivation & Objective

  • To develop a practical, automated method for identifying programming languages in source code without prior knowledge of the language.
  • To address the widespread problem of unidentified code on the web, such as in blogs, forums, and documentation, where language tags are missing.
  • To improve upon existing tools like SourceClassifier, which rely on simple Bayesian classifiers and are easily misled by comments and strings.
  • To enable better code searchability, syntax highlighting, and automated code grading by accurately identifying the language of code fragments.
  • To create a scalable, open-source solution using Python and an MIT license for reproducibility and community extension.

Proposed method

  • The system uses a large, curated training dataset of 41,000 source code files from GitHub, filtered by file extension to reduce noise.
  • It excludes comments and strings during training and inference by detecting lines with natural language-like word sequences (the 'words property').
  • A heuristic-based approach identifies string delimiters and block comment markers using a hardcoded database and similarity heuristics for unknown tokens.
  • Statistical features are extracted from token patterns, with matching scored by the number of tokens matched rather than length to ensure fairness across grammars.
  • The model uses supervised learning to rank languages based on feature similarity to known training examples, avoiding reliance on grammar parsing.
  • A grammatical approach using parsing expression grammars (PEGs) and OMeta was explored but abandoned due to difficulties in grammar combination and scalability.

Experimental results

Research questions

  • RQ1Can a statistical, feature-based approach outperform Bayesian classifiers in programming language identification?
  • RQ2To what extent can excluding comments and strings improve identification accuracy in diverse programming languages?
  • RQ3Is it feasible to build a scalable, high-accuracy language identifier without relying on grammar-based parsing?
  • RQ4How does the performance of this system compare to existing tools like SourceClassifier on real-world code fragments?
  • RQ5Can a system be built that is both accurate and practical for deployment in web-scale code indexing and syntax highlighting?

Key findings

  • The system correctly identified the programming language in 12 out of 25 test files (48%), ranking it in the top five for 22 of 25 files (88%).
  • In a direct comparison, SourceClassifier achieved only 12% top-1 accuracy on the same test set, demonstrating a clear performance advantage of the proposed method.
  • The model successfully handled language pairs with high similarity, such as Scheme and Common Lisp, suggesting robustness to syntactic overlap.
  • The exclusion of comments and strings significantly improved feature quality, reducing noise from natural language content.
  • The grammatical approach using PEGs and OMeta was abandoned due to challenges in grammar composition and parsing reliability, despite promising theoretical foundations.
  • The system's open-source implementation under the MIT license enables reproducibility and future extension by the research community.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.