Commit c3d8a5be authored by Igor Dejanovic's avatar Igor Dejanovic

Removed logging module from examples.

parent 08653e36
......@@ -13,7 +13,6 @@ import sys
from arpeggio import *
from arpeggio.export import PMDOTExport, PTDOTExport
from arpeggio import RegExMatch as _
import logging
def bibfile(): return ZeroOrMore([bibentry, comment]), EndOfFile
......@@ -29,7 +28,8 @@ def comment(): return _(r'[^@]+')
class BibFileSem(SemanticAction):
'''Just returns list of child nodes (bibentries).'''
def first_pass(self, parser, node, nodes):
logging.debug("Processing Bibfile")
if parser.debug:
print "Processing Bibfile"
return nodes[:-1]
......@@ -37,7 +37,8 @@ class BibEntrySem(SemanticAction):
'''Constructs a map where key is bibentry field name.
Key is returned under 'bibkey' key. Type is returned under 'bibtype'.'''
def first_pass(self, parser, node, nodes):
logging.debug(" Processing bibentry %s" % nodes[2])
if parser.debug:
print " Processing bibentry %s" % nodes[2]
bib_entry_map = {
'bibtype': nodes[0].value,
'bibkey': nodes[2].value
......@@ -51,7 +52,8 @@ class BibEntrySem(SemanticAction):
class FieldSem(SemanticAction):
'''Constructs a tuple (fieldname, fieldvalue).'''
def first_pass(self, parser, node, nodes):
logging.debug(" Processing field %s" % nodes[0])
if parser.debug:
print " Processing field %s" % nodes[0]
field = (nodes[0].value, nodes[3])
return field
......@@ -75,39 +77,34 @@ field.sem = FieldSem()
fieldvalue.sem = FieldValueSem()
if __name__ == "__main__":
try:
logging.basicConfig(level=logging.DEBUG)
# First we will make a parser - an instance of the bib parser model.
# Parser model is given in the form of python constructs therefore we
# are using ParserPython class.
parser = ParserPython(bibfile, reduce_tree=True)
# Then we export it to a dot file in order to visualise it. This is
# particulary handy for debugging purposes.
# We can make a jpg out of it using dot (part of graphviz) like this
# dot -O -Tjpg calc_parse_tree_model.dot
PMDOTExport().exportFile(parser.parser_model,
"bib_parse_tree_model.dot")
# First parameter is bibtex file
if len(sys.argv) > 1:
with open(sys.argv[1], "r") as bibtexfile:
bibtexfile_content = bibtexfile.read()
# We create a parse tree or abstract syntax tree out of textual input
parse_tree = parser.parse(bibtexfile_content)
# Then we export it to a dot file in order to visualise it.
PTDOTExport().exportFile(parse_tree,
"bib_parse_tree.dot")
# getASG will start semantic analysis.
# In this case semantic analysis will list of bibentry maps.
print parser.getASG()
else:
print "Usage: python bibtex.py file_to_parse"
except NoMatch, e:
print "Expected %s at position %s." % (e.value, str(e.parser.pos_to_linecol(e.position)))
# First we will make a parser - an instance of the bib parser model.
# Parser model is given in the form of python constructs therefore we
# are using ParserPython class.
parser = ParserPython(bibfile, reduce_tree=True, debug=True)
# Then we export it to a dot file in order to visualise it. This is
# particulary handy for debugging purposes.
# We can make a jpg out of it using dot (part of graphviz) like this
# dot -O -Tjpg calc_parse_tree_model.dot
PMDOTExport().exportFile(parser.parser_model,
"bib_parse_tree_model.dot")
# First parameter is bibtex file
if len(sys.argv) > 1:
with open(sys.argv[1], "r") as bibtexfile:
bibtexfile_content = bibtexfile.read()
# We create a parse tree or abstract syntax tree out of textual input
parse_tree = parser.parse(bibtexfile_content)
# Then we export it to a dot file in order to visualise it.
PTDOTExport().exportFile(parse_tree,
"bib_parse_tree.dot")
# getASG will start semantic analysis.
# In this case semantic analysis will list of bibentry maps.
print parser.getASG()
else:
print "Usage: python bibtex.py file_to_parse"
......@@ -5,18 +5,17 @@
# Copyright: (c) 2009 Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#
# This example is functionally equivalent to calc.py. The difference is that
# This example is functionally equivalent to calc.py. The difference is that
# in this example grammar is specified using PEG language instead of python constructs.
# Semantic actions are used to calculate expression during semantic
# analysis.
# Parser model as well as parse tree exported to dot files should be
# Parser model as well as parse tree exported to dot files should be
# the same as parser model and parse tree generated in calc.py example.
#######################################################################
from arpeggio import *
from arpeggio.peg import ParserPEG
from arpeggio.export import PMDOTExport, PTDOTExport
import logging
# Semantic actions
from calc import ToFloat, Factor, Term, Expr, Calc
......@@ -31,7 +30,7 @@ calc_grammar = """
calc <- expression EndOfFile;
"""
# Rules are mapped to semantic actions
# Rules are mapped to semantic actions
sem_actions = {
"number" : ToFloat(),
"factor" : Factor(),
......@@ -40,14 +39,13 @@ sem_actions = {
"calc" : Calc()
}
try:
logging.basicConfig(level=logging.DEBUG)
try:
# First we will make a parser - an instance of the calc parser model.
# Parser model is given in the form of PEG notation therefore we
# Parser model is given in the form of PEG notation therefore we
# are using ParserPEG class. Root rule name (parsing expression) is "calc".
parser = ParserPEG(calc_grammar, "calc")
parser = ParserPEG(calc_grammar, "calc", debug=True)
# Then we export it to a dot file.
PMDOTExport().exportFile(parser.parser_model,
......@@ -55,10 +53,10 @@ try:
# An expression we want to evaluate
input = "-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# Then parse tree is created out of the input expression.
parse_tree = parser.parse(input)
# We save it to dot file in order to visualise it.
PTDOTExport().exportFile(parse_tree,
"calc_peg_parse_tree.dot")
......@@ -68,6 +66,6 @@ try:
# returned value will be evaluated result of the input expression.
# Semantic actions are supplied to the getASG function.
print "%s = %f" % (input, parser.getASG(sem_actions))
except NoMatch, e:
print "Expected %s at position %s." % (e.value, str(e.parser.pos_to_linecol(e.position)))
......@@ -36,7 +36,6 @@ value
from arpeggio import *
from arpeggio.export import PMDOTExport, PTDOTExport
from arpeggio import RegExMatch as _
import logging
def TRUE(): return "true"
def FALSE(): return "false"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment