Commit c3d8a5be authored by Igor Dejanovic's avatar Igor Dejanovic

Removed logging module from examples.

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