#!/usr/bin/env python # python_201_plex1.py # # Sample Plex lexer # import sys import Plex def test(infileName): letter = Plex.Range("AZaz") digit = Plex.Range("09") name = letter + Plex.Rep(letter | digit) number = Plex.Rep1(digit) space = Plex.Any(" \t\n") comment = Plex.Str('"') + Plex.Rep( Plex.AnyBut('"')) + Plex.Str('"') resword = Plex.Str("if", "then", "else", "end") lexicon = Plex.Lexicon([ (resword, 'keyword'), (name, 'ident'), (number, 'int'), ( Plex.Any("+-*/=<>"), Plex.TEXT), (space, Plex.IGNORE), (comment, 'comment'), ]) infile = open(infileName, "r") scanner = Plex.Scanner(lexicon, infile, infileName) while 1: token = scanner.read() position = scanner.position() print '(%d, %d) tok: %s tokType: %s' % \ (position[1], position[2], token[1], token[0]) if token[0] is None: break USAGE_TEXT = """ Usage: python python_201_plex1.py """ def usage(): print USAGE_TEXT sys.exit(-1) def main(): args = sys.argv[1:] if len(args) != 1: usage() infileName = args[0] test(infileName) if __name__ == '__main__': main() #import pdb #pdb.run('main()')