real life examples

Steve Williams sandj.williams at gte.net
Wed Dec 20 11:59:09 EST 2000


huber wrote:

> i try to teach python to my son (13 years old)!
> but instead of stupid for loops, classes nobody ever needs and so on .....
> are there some good real life examples a child could be interested in ?
>
> thanks
> udo

This toy parser is always fun to play with.  My version of an electric train.

It's not very Pythonic, but then I've written it in COBOL, Basic, FORTRAN, 360
assembler, Signetics 2650 ML, . . .

I got it from an ACM Surveys article in about 1969.

#=======================================
#Kids!  Be the first on your block to design your own language!
#Implement boolean data types!
#Create a 'while forever do' statement!

#=======================================
def digit(strByte):
  global show
  if show > 3:
    print "applying digit test to %s" % (strIn[p:p + l])
  if strByte >= '0' and strByte <= '9':
    return 1
  else:
    return 0

#========================================
def alpha(strByte):
  global show
  if show > 3:
    print "applying alpha test to %s" % (strIn[p:p + l])
  if strByte >= 'A' and strByte <= 'Z':
    return 1
  else:
    return 0

#========================================
def match(strTerminal):
  global strIn,p,show,clss
  l = len(strTerminal)
  if p + l - 1 > len(strIn): return 0
  if show > 3:
    print "Matching %s to %s, p: %d, l: %d" % (strIn[p:p + l],strTerminal,p,l)

  if strIn[p:p + l] == strTerminal:
    p = p + l
    return 1
  return 0

#========================================
def parse(goal):
  global strIn,p,show
  r = 0
  rules = lang[goal]
  while r < len(rules):
    if show > 1 and p < len(strIn):
      print 'Testing %s at %s' % (repr(rules[r]),strIn[p])

    if p >= len(strIn) - 1 and rules[r][0] == '<EOF>':
      test = 'true'
    elif lang.has_key(rules[r][0]):
      test = parse(rules[r][0])
    elif clss.has_key(rules[r][0]):
      if clss[rules[r][0]](strIn[p]):
        p = p + 1
        test = 'true'
      else:
        test = 'false'
    elif match(rules[r][0]):
        test = 'true'
    else:
      test = 'false'

    if test == 'true':
      if show > 0:
        print "match %s" % rules[r][0]
      action = rules[r][1]
    else:
      action = rules[r][2]

    if action == 'pass':
      return 'true'
    elif action == 'fail':
      return 'false'
    elif action == 'next':
        r = r + 1
    else:
      r = r + int(action)

#========================================
lang = {}

lang["<PRG>"] = (("<SAE>","next","fail"),("<EOF>","pass","fail"))

lang["<SAE>"] =
(("<AOP>","next","fail"),("+","+2","next"),("-","next","pass"),("<AOP>","-2","fail"))

lang["<AOP>"] =
(("<MOP>","next","fail"),("*","+2","next"),("/","next","pass"),("<MOP>","-2","fail"))

lang["<MOP>"] = (("<SGN>","+0","next"),
("<NUM>","pass","next"),("<VAR>","pass","next"), \
  ("(","next","fail"),("<SAE>","next","fail"),(")","pass","fail"))

lang["<SGN>"] = (("+","pass","next"),("-","pass","fail"))

lang["<NUM>"] = (("<GE1>","pass","next"),("<LT1>","pass","fail"))

lang["<GE1>"] =
(("<DIG>","next","fail"),("<DIG>","+0","next"),("<LT1>","pass","pass"))

lang["<LT1>"] =
((".","next","fail"),("<DIG>","next","fail"),("<DIG>","+0","pass"))

clss = {}
clss["<DIG>"] = digit
clss["<VAR>"] = alpha

strIn = "((A)+(A*B)*-23.6)"
p = 0
goal = "<PRG>"
show = 3

print strIn, goal, show
print parse(goal)






More information about the Python-list mailing list