Create a gist now

Instantly share code, notes, and snippets.

Analysis of keywords in few popular languages
"""
Analyzing keywords from different programming languages.
"""
def word_stats(words):
count = len(words)
len_sum = sum(map(len, words))
return {
'count': count,
'total_chars': len_sum,
'average_length': len_sum / float(count)
}
# Python
python_keywords = """
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
""".split()
# C++ (http://en.cppreference.com/w/cpp/keyword)
cpp_keywords = [k for k in """
alignas (since C++11)
alignof (since C++11)
and
and_eq
asm
auto(1)
bitand
bitor
bool
break
case
catch
char
char16_t(since C++11)
char32_t(since C++11)
class
compl
const
constexpr(since C++11)
const_cast
continue
decltype(since C++11)
default(1)
delete(1)
do
double
dynamic_cast
else
enum
explicit
export
extern
false
float
for
friend
goto
if
inline
int
long
mutable
namespace
new
noexcept(since C++11)
not
not_eq
nullptr (since C++11)
operator
or
or_eq
private
protected
public
register
reinterpret_cast
return
short
signed
sizeof
static
static_assert(since C++11)
static_cast
struct
switch
template
this
thread_local(since C++11)
throw
true
try
typedef
typeid
typename
union
unsigned
using(1)
virtual
void
volatile
wchar_t
while
xor
xor_eq
""".splitlines() if len(k.strip()) > 0]
cpp11_keywords = [k[:k.find('(')] if '(' in k else k
for k in cpp_keywords]
cpp03_keywords = [k for i, k in enumerate(cpp11_keywords)
if '11' not in cpp_keywords[i]]
# Java (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html)
java_keywords = [w.strip('*') for w in """
abstract continue for new switch
assert*** default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum**** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while
""".split()]
# C (http://msdn.microsoft.com/en-us/library/befeaky0.aspx)
c_keywords = """
auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while
""".split()
# C# (http://msdn.microsoft.com/en-us/library/x53a06bb%28v=vs.71%29.aspx)
cs_keywords = """
abstract event new struct
as explicit null switch
base extern object this
bool false operator throw
break finally out true
byte fixed override try
case float params typeof
catch for private uint
char foreach protected ulong
checked goto public unchecked
class if readonly unsafe
const implicit ref ushort
continue in return using
decimal int sbyte virtual
default interface sealed volatile
delegate internal short void
do is sizeof while
double lock stackalloc
else long static
enum namespace string
""".split()
# Go (http://golang.org/ref/spec#Keywords)
go_keywords = """
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
""".split()
if __name__ == '__main__':
print "Python 2.7:", word_stats(python_keywords)
print "C++03:", word_stats(cpp03_keywords)
print "C++11:", word_stats(cpp11_keywords)
print "Java 1.7:", word_stats(java_keywords)
print "C:", word_stats(c_keywords)
print "C# 4.0:", word_stats(cs_keywords)
print "Go:", word_stats(go_keywords)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment