Example #1
0
def _parse_grammar_file(fd, params={}):
    """Read a grammar file and return the resulting parse tree.

    The return value of this function is a tuple, consisting of the
    parse tree (or None in case of an unrecoverable error) and a
    boolean indicating whether errors (recoverable or unrecoverable)
    were found.

    If the grammar file contains errors, error messages are printed to
    stderr.
    """
    max_err = 100
    p = Parser(max_err=max_err)
    try:
        tree = p.parse(tokens(fd))
        has_errors = False
    except SyntaxError, e:
        _print_error(e.msg, e.lineno, e.offset,
                     fname=params.get("fname", None))
        tree = None
        has_errors = True
Example #2
0
def _parse_grammar_file(fd, params={}):
    """Read a grammar file and return the resulting parse tree.

    The return value of this function is a tuple, consisting of the
    parse tree (or None in case of an unrecoverable error) and a
    boolean indicating whether errors (recoverable or unrecoverable)
    were found.

    If the grammar file contains errors, error messages are printed to
    stderr.
    """
    max_err = 100
    p = Parser(max_err=max_err)
    try:
        tree = p.parse(tokens(fd))
        has_errors = False
    except SyntaxError, e:
        _print_error(e.msg,
                     e.lineno,
                     e.offset,
                     fname=params.get("fname", None))
        tree = None
        has_errors = True
Example #3
0
from parser import Parser
from scanner import tokens

def print_tree(tree, terminals, indent=0):
    """Print a parse tree to stdout."""
    prefix = "    "*indent
    if tree[0] in terminals:
        print prefix + repr(tree)
    else:
        print prefix + str(tree[0])
        for x in tree[1:]:
            print_tree(x, terminals, indent+1)

fname = sys.argv[1]
text = open(fname).read()
input = tokens(text)

p = Parser()
try:
    tree = p.parse(input)
except p.ParseErrors, e:
    for token,expected in e.errors:
        if token[0] == p.EOF:
            print >>sys.stderr, "unexpected end of file"
            continue

        found = repr(token[0])
        if len(expected) == 1:
            msg = "%s:%d:%d:missing %s (found %s)"%(fname,token[2],token[3],repr(expected[0]),found)
        else:
            msg1 = "%s:%d:%d:parse error before %s, "%(fname,token[2],token[3],found)
Example #4
0
  (';', ';', 3, 6)
]

in3 = r"""
"this is a
string"
"this is a \"string\""
"'"
'"'
"""
out3 = [
  ('string', 'this is a\nstring', 2, 1),
  ('string', 'this is a "string"', 4, 1),
  ('string', "'", 5, 1),
  ('string', '"', 6, 1)
]

names = sorted(name for name in dir() if name.startswith("in"))
for name in names:
    label = name[2:]
    text = eval(name)
    result = eval("out"+label)

    tt = list(tokens(text.splitlines()))
    if tt == result:
        print "test %s: OK"%label
    else:
        print "test %s: FAIL"%label
        for t in tt:
            print t
Example #5
0
# c1
a: bc; # c2
"""
out2 = [('token', 'a', 3, 1), (':', ':', 3, 2), ('token', 'bc', 3, 4),
        (';', ';', 3, 6)]

in3 = r"""
"this is a
string"
"this is a \"string\""
"'"
'"'
"""
out3 = [('string', 'this is a\nstring', 2, 1),
        ('string', 'this is a "string"', 4, 1), ('string', "'", 5, 1),
        ('string', '"', 6, 1)]

names = sorted(name for name in dir() if name.startswith("in"))
for name in names:
    label = name[2:]
    text = eval(name)
    result = eval("out" + label)

    tt = list(tokens(text.splitlines()))
    if tt == result:
        print "test %s: OK" % label
    else:
        print "test %s: FAIL" % label
        for t in tt:
            print t
Example #6
0

def print_tree(tree, terminals, indent=0):
    """Print a parse tree to stdout."""
    prefix = "    " * indent
    if tree[0] in terminals:
        print prefix + repr(tree)
    else:
        print prefix + str(tree[0])
        for x in tree[1:]:
            print_tree(x, terminals, indent + 1)


fname = sys.argv[1]
text = open(fname).read()
input = tokens(text)

p = Parser()
try:
    tree = p.parse(input)
except p.ParseErrors, e:
    for token, expected in e.errors:
        if token[0] == p.EOF:
            print >> sys.stderr, "unexpected end of file"
            continue

        found = repr(token[0])
        if len(expected) == 1:
            msg = "%s:%d:%d:missing %s (found %s)" % (
                fname, token[2], token[3], repr(expected[0]), found)
        else: