Пример #1
0
def parse(fname, lexonly):
    import pyggy

    l, ltab = pyggy.getlexer("ansic.pyl")
    p, ptab = pyggy.getparser("ansic.pyg")
    p.setlexer(l)
    l.setinput(preprocess(fname))

    if lexonly:
        while 1:
            tok = l.token()
            if tok is None:
                break
            print tok, l.value
            if tok == "#ERR#":
                print "error at %s:%d" % (lextab.fname, lextab.lineno)
                break
    else:
        # parse the input
        tree = p.parse()
        if tree == None:
            print "error at %s:%d near %s" % (lextab.fname, lextab.lineno, l.value)
        else:
            print "displaying tree"
            # print tree
            # pyggy.glr.dottree(tree)
            t = pyggy.proctree(tree, ptab)
            pprint.pprint(t)
    print "done"
Пример #2
0
def parse(fname, lexonly):
    import pyggy
    l, ltab = pyggy.getlexer("ansic.pyl")
    p, ptab = pyggy.getparser("ansic.pyg")
    p.setlexer(l)
    l.setinput(preprocess(fname))

    if lexonly:
        while 1:
            tok = l.token()
            if tok is None:
                break
            print tok, l.value
            if tok == "#ERR#":
                print "error at %s:%d" % (lextab.fname, lextab.lineno)
                break
    else:
        # parse the input
        tree = p.parse()
        if tree == None:
            print "error at %s:%d near %s" % (lextab.fname, lextab.lineno,
                                              l.value)
        else:
            print "displaying tree"
            #print tree
            #pyggy.glr.dottree(tree)
            t = pyggy.proctree(tree, ptab)
            pprint.pprint(t)
    print "done"
Пример #3
0
"""
pgy_calc.py
	A simple calculator with variables

based on calc.py from the PLY web page.
"""

import sys
import pyggy

# build the lexer and parser
l,ltab = pyggy.getlexer("pyg_calc.pyl")
p,ptab = pyggy.getparser("pyg_calc.pyg")
p.setlexer(l)

while 1:
	sys.stdout.write("calc > ")
	line = sys.stdin.readline()
	if line == "" :
		break
	
	l.setinputstr(line)
	try :
		tree = p.parse()
	except pyggy.ParseError,e :
		print "parse error at '%s'" % e.str
		continue
	pyggy.proctree(tree, ptab)

Пример #4
0
#!/usr/bin/python

import pyggy

# instantiate a lexer and a parser
l, ltab = pyggy.getlexer("test3.pyl")
p, ptab = pyggy.getparser("test3.pyg")
l.setinput("-")
p.setlexer(l)

# parse the input
tree = p.parse()
if tree == None:
    print "error!"
else:
    print pyggy.proctree(tree, ptab)
    # uncomment if you have dotty
    #pyggy.glr.dottree(tree)
Пример #5
0
#!/usr/bin/python

import pyggy

# instantiate a lexer and a parser
l, ltab = pyggy.getlexer("test3.pyl")
p, ptab = pyggy.getparser("test3.pyg")
l.setinput("-")
p.setlexer(l)

# parse the input
tree = p.parse()
if tree == None:
    print "error!"
else:
    print pyggy.proctree(tree, ptab)
    # uncomment if you have dotty
    # pyggy.glr.dottree(tree)