cdecl.py challenge
Simon Burton
simonb at webone.com.au
Sun Dec 8 01:19:51 EST 2002
To the curious C/python programmer,
Am hacking a cdecl.py today (fish book under-arm).
Any one up for a race/collab?
(This is something for the cookbook, i reckon.)
Here is my (barely doing anything) code so far:
#!/usr/bin/env python
import sys
from string import *
type_list = "void char signed unsigned short int long float double struct union enum".split()
qual_list = "const volatile".split()
def classify(s):
if s in type_list: return "TYPE"
if s in qual_list: return "QUAL"
else: return "ID"
def next_tok(s):
i=0
while i < len(s):
if s[i].isalnum():
j=i+1
while j<len(s):
if s[j].isalnum():
j=j+1
continue
else:
break
tok = s[i:j]
s = s[j:]
return tok, classify(tok), s
if s[i] in '*()[];,':
tok = s[i]
s=s[i+1:]
return tok, "PUNCT", s
i=i+1
return "", "", ""
def cdecl(s):
tok, tp, s = next_tok(s)
while tok:
print tok, tp
tok, tp, s = next_tok(s)
sys.stdout.write(">>> ")
if not sys.argv[1:]:
s = sys.stdin.readline()
else:
s = join(sys.argv[1:])
cdecl( s )
More information about the Python-list
mailing list