解析源代码-不同语言的唯一标识符? [英] Parsing Source Code - Unique Identifiers for Different Languages?

查看:124
本文介绍了解析源代码-不同语言的唯一标识符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序接收源代码作为输入并分析代码的多个方面.它可以接受来自许多常见语言的代码,例如C/C ++,C#,Java,Python,PHP,Pascal,SQL等(但不支持许多语言,例如Ada,Cobol,Fortran).一旦知道了语言,我的应用程序就知道要做什么(我对不同的语言有不同的处理程序).

I'm building an application that receives source code as input and analyzes several aspects of the code. It can accept code from many common languages, e.g. C/C++, C#, Java, Python, PHP, Pascal, SQL, and more (however many languages are unsupported, e.g. Ada, Cobol, Fortran). Once the language is known, my application knows what to do (I have different handlers for different languages).

当前,我正在要求用户输入编写代码的编程语言,这很容易出错:尽管用户知道编程语言,但一小部分(很少见)会单击错误的选项,由于鲁re,这破坏了系统(即我的分析失败).

Currently I'm asking the user to input the programming language the code is written in, and this is error-prone: although users know the programming languages, a small percentage of them (on rare occasions) click the wrong option just due to recklessness, and that breaks the system (i.e. my analysis fails).

在我看来,应该有一种方法可以从输入文本本身中找出(在大多数情况下)语言是什么.一些注意事项:

It seems to me like there should be a way to figure out (in most cases) what the language is, from the input text itself. Several notes:

  • 我收到的是纯文本而不是文件名,所以我不能使用扩展名作为提示.
  • 不要求用户输入完整的源代码,也可以输入代码段(即,可能不包括include/import部分).
  • 对我来说很明显,我选择的任何算法都不是100%证明的,当然对于非常短的输入代码(例如,Python和Ruby都可以接受),在这种情况下,我仍然需要用户的帮助.我希望在此过程中尽量减少用户的参与,以最大程度地减少错误.

示例:

  • 如果文本包含"x-> y()",那么我肯定会知道它是C ++(?)
  • 如果文本包含"public static void main",则我肯定可以确定它是Java(?)
  • 如果文本中包含从x:= y到z开始",我肯定可以确定它是Pascal(?)

我的问题:

  1. 您是否熟悉用于自动确定输入源代码的语言是什么的标准库/方法?
  2. 我可以肯定地将一种语言与另一种语言区分开来的独特代码令牌"是什么?

我正在用Python编写代码,但我相信这个问题与语言无关.

I'm writing my code in Python but I believe the question to be language agnostic.

谢谢

推荐答案

构建通用标记生成器,然后对它们使用贝叶斯过滤器.使用现有的用户选中框"系统进行培训.

build a generic tokenizer and then use a Bayesian filter on them. Use the existing "user checks a box" system to train it.

这篇关于解析源代码-不同语言的唯一标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆