Commit 7eb2e080 authored by Igor Dejanovic's avatar Igor Dejanovic

Removed logging module from examples.

parent 7eef4197
...@@ -4,26 +4,26 @@ ...@@ -4,26 +4,26 @@
# Author: Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com> # Author: Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# 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 based on jsonParser.py from pyparsing project # This example is based on jsonParser.py from pyparsing project
# (see http://pyparsing.wikispaces.com/). # (see http://pyparsing.wikispaces.com/).
############################################################################## ##############################################################################
json_bnf = """ json_bnf = """
object object
{ members } { members }
{} {}
members members
string : value string : value
members , string : value members , string : value
array array
[ elements ] [ elements ]
[] []
elements elements
value value
elements , value elements , value
value value
string string
number number
object object
...@@ -51,15 +51,15 @@ def jsonMembers(): return memberDef, ZeroOrMore(",", memberDef) ...@@ -51,15 +51,15 @@ def jsonMembers(): return memberDef, ZeroOrMore(",", memberDef)
def jsonObject(): return "{", Optional(jsonMembers), "}" def jsonObject(): return "{", Optional(jsonMembers), "}"
def jsonFile(): return jsonObject, EOF def jsonFile(): return jsonObject, EOF
if __name__ == "__main__": if __name__ == "__main__":
testdata = """ testdata = """
{ {
"glossary": { "glossary": {
"title": "example glossary", "title": "example glossary",
"GlossDiv": { "GlossDiv": {
"title": "S", "title": "S",
"GlossList": "GlossList":
{ {
"ID": "SGML", "ID": "SGML",
"SortAs": "SGML", "SortAs": "SGML",
...@@ -83,15 +83,13 @@ if __name__ == "__main__": ...@@ -83,15 +83,13 @@ if __name__ == "__main__":
} }
""" """
try: try:
logging.basicConfig(level=logging.DEBUG)
# Creating parser from parser model. # Creating parser from parser model.
parser = ParserPython(jsonFile) parser = ParserPython(jsonFile, debug=True)
# Exporting parser model to dot file in order to visualise it. # Exporting parser model to dot file in order to visualise it.
PMDOTExport().exportFile(parser.parser_model, PMDOTExport().exportFile(parser.parser_model,
"json_parser_model.dot") "json_parser_model.dot")
parse_tree = parser.parse(testdata) parse_tree = parser.parse(testdata)
PTDOTExport().exportFile(parser.parse_tree, PTDOTExport().exportFile(parser.parse_tree,
...@@ -99,4 +97,4 @@ if __name__ == "__main__": ...@@ -99,4 +97,4 @@ if __name__ == "__main__":
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)))
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
from arpeggio import * from arpeggio import *
from arpeggio.export import PMDOTExport, PTDOTExport from arpeggio.export import PMDOTExport, PTDOTExport
from arpeggio.peg import ParserPEG from arpeggio.peg import ParserPEG
import logging
# Semantic actions # Semantic actions
from arpeggio.peg import SemGrammar, SemRule, SemOrderedChoice, SemSequence,\ from arpeggio.peg import SemGrammar, SemRule, SemOrderedChoice, SemSequence,\
...@@ -67,11 +66,10 @@ peg_grammar = r""" ...@@ -67,11 +66,10 @@ peg_grammar = r"""
try: try:
logging.basicConfig(level=logging.DEBUG)
# ParserPEG will use ParserPython to parse peg_grammar definition and # ParserPEG will use ParserPython to parse peg_grammar definition and
# create parser_model for parsing PEG based grammars # create parser_model for parsing PEG based grammars
parser = ParserPEG(peg_grammar, 'grammar') parser = ParserPEG(peg_grammar, 'grammar', debug=True)
# Exporting parser model to dot file in order to visualise. # Exporting parser model to dot file in order to visualise.
PMDOTExport().exportFile(parser.parser_model, PMDOTExport().exportFile(parser.parser_model,
......
...@@ -22,10 +22,9 @@ ...@@ -22,10 +22,9 @@
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
# Grammar rules # Grammar rules
def program(): return Kwd('begin'), ZeroOrMore(command), Kwd('end'), EndOfFile def program(): return Kwd('begin'), ZeroOrMore(command), Kwd('end'), EndOfFile
def command(): return [up, down, left, right] def command(): return [up, down, left, right]
def up(): return 'up' def up(): return 'up'
def down(): return 'down' def down(): return 'down'
...@@ -36,35 +35,35 @@ def right(): return 'right' ...@@ -36,35 +35,35 @@ def right(): return 'right'
# Semantic actions # Semantic actions
class Up(SemanticAction): class Up(SemanticAction):
def first_pass(self, parser, node, nodes): def first_pass(self, parser, node, nodes):
logging.debug("Going up") print "Going up"
return (0, 1) return (0, 1)
class Down(SemanticAction): class Down(SemanticAction):
def first_pass(self, parser, node, nodes): def first_pass(self, parser, node, nodes):
logging.debug("Going down") print "Going down"
return (0, -1) return (0, -1)
class Left(SemanticAction): class Left(SemanticAction):
def first_pass(self, parser, node, nodes): def first_pass(self, parser, node, nodes):
logging.debug("Going left") print "Going left"
return (-1, 0) return (-1, 0)
class Right(SemanticAction): class Right(SemanticAction):
def first_pass(self, parser, node, nodes): def first_pass(self, parser, node, nodes):
logging.debug("Going right") print "Going right"
return (1, 0) return (1, 0)
class Command(SemanticAction): class Command(SemanticAction):
def first_pass(self, parser, node, nodes): def first_pass(self, parser, node, nodes):
logging.debug("Command") print "Command"
return nodes[0] return nodes[0]
class Program(SemanticAction): class Program(SemanticAction):
def first_pass(self, parser, node, nodes): def first_pass(self, parser, node, nodes):
logging.debug("Evaluating position") print "Evaluating position"
return reduce(lambda x, y: (x[0]+y[0], x[1]+y[1]), nodes[1:-2]) return reduce(lambda x, y: (x[0]+y[0], x[1]+y[1]), nodes[1:-2])
# Connecting rules with semantic actions # Connecting rules with semantic actions
program.sem = Program() program.sem = Program()
command.sem = Command() command.sem = Command()
...@@ -75,7 +74,6 @@ right.sem = Right() ...@@ -75,7 +74,6 @@ right.sem = Right()
if __name__ == "__main__": if __name__ == "__main__":
try: try:
logging.basicConfig(level=logging.DEBUG)
# Program code # Program code
input = ''' input = '''
...@@ -90,9 +88,9 @@ if __name__ == "__main__": ...@@ -90,9 +88,9 @@ if __name__ == "__main__":
# First we will make a parser - an instance of the robot parser model. # First we will make a parser - an instance of the robot parser model.
# Parser model is given in the form of python constructs therefore we # Parser model is given in the form of python constructs therefore we
# are using ParserPython class. # are using ParserPython class.
parser = ParserPython(program) parser = ParserPython(program, debug=True)
# Then we export it to a dot file in order to visualize it. This is # Then we export it to a dot file in order to visualize it. This is
# particularly handy for debugging purposes. # particularly handy for debugging purposes.
...@@ -100,10 +98,10 @@ if __name__ == "__main__": ...@@ -100,10 +98,10 @@ if __name__ == "__main__":
# dot -O -Tjpg robot_parse_tree_model.dot # dot -O -Tjpg robot_parse_tree_model.dot
PMDOTExport().exportFile(parser.parser_model, PMDOTExport().exportFile(parser.parser_model,
"robot_parse_tree_model.dot") "robot_parse_tree_model.dot")
# We create a parse tree out of textual input # We create a parse tree out of textual input
parse_tree = parser.parse(input) parse_tree = parser.parse(input)
# Then we export it to a dot file in order to visualize it. # Then we export it to a dot file in order to visualize it.
# dot -O -Tjpg robot_parse_tree.dot # dot -O -Tjpg robot_parse_tree.dot
PTDOTExport().exportFile(parse_tree, PTDOTExport().exportFile(parse_tree,
...@@ -113,6 +111,6 @@ if __name__ == "__main__": ...@@ -113,6 +111,6 @@ if __name__ == "__main__":
# In this case semantic analysis will evaluate expression and # In this case semantic analysis will evaluate expression and
# returned value will be the final position of the robot. # returned value will be the final position of the robot.
print "position = ", parser.getASG() print "position = ", parser.getASG()
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)))
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
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
from arpeggio.peg import ParserPEG from arpeggio.peg import ParserPEG
# Grammar rules # Grammar rules
...@@ -49,7 +48,6 @@ semantic_actions = { ...@@ -49,7 +48,6 @@ semantic_actions = {
if __name__ == "__main__": if __name__ == "__main__":
try: try:
logging.basicConfig(level=logging.DEBUG)
# Program code # Program code
input = ''' input = '''
...@@ -64,9 +62,9 @@ if __name__ == "__main__": ...@@ -64,9 +62,9 @@ if __name__ == "__main__":
# First we will make a parser - an instance of the robot parser model. # First we will make a parser - an instance of the robot parser model.
# Parser model is given in the form of PEG specification therefore we # Parser model is given in the form of PEG specification therefore we
# are using ParserPEG class. # are using ParserPEG class.
parser = ParserPEG(robot_grammar, 'program') parser = ParserPEG(robot_grammar, 'program', debug=True)
# Then we export it to a dot file in order to visualize it. This is # Then we export it to a dot file in order to visualize it. This is
# particularly handy for debugging purposes. # particularly handy for debugging purposes.
...@@ -74,10 +72,10 @@ if __name__ == "__main__": ...@@ -74,10 +72,10 @@ if __name__ == "__main__":
# dot -O -Tjpg robot_peg_parse_tree_model.dot # dot -O -Tjpg robot_peg_parse_tree_model.dot
PMDOTExport().exportFile(parser.parser_model, PMDOTExport().exportFile(parser.parser_model,
"robot_peg_parse_tree_model.dot") "robot_peg_parse_tree_model.dot")
# We create a parse tree out of textual input # We create a parse tree out of textual input
parse_tree = parser.parse(input) parse_tree = parser.parse(input)
# Then we export it to a dot file in order to visualize it. # Then we export it to a dot file in order to visualize it.
# dot -O -Tjpg robot_peg_parse_tree.dot # dot -O -Tjpg robot_peg_parse_tree.dot
PTDOTExport().exportFile(parse_tree, PTDOTExport().exportFile(parse_tree,
...@@ -87,6 +85,6 @@ if __name__ == "__main__": ...@@ -87,6 +85,6 @@ if __name__ == "__main__":
# In this case semantic analysis will evaluate expression and # In this case semantic analysis will evaluate expression and
# returned value will be the final position of the robot. # returned value will be the final position of the robot.
print "position = ", parser.getASG(sem_actions=semantic_actions) print "position = ", parser.getASG(sem_actions=semantic_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)))
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
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 comment(): return [_("//.*"), _("/\*.*\*/")] def comment(): return [_("//.*"), _("/\*.*\*/")]
def literal(): return _(r'\d*\.\d*|\d+|".*?"') def literal(): return _(r'\d*\.\d*|\d+|".*?"')
...@@ -31,22 +30,21 @@ def function(): return Kwd("function"), symbol, parameterlist, block ...@@ -31,22 +30,21 @@ def function(): return Kwd("function"), symbol, parameterlist, block
def simpleLanguage(): return function def simpleLanguage(): return function
try: try:
logging.basicConfig(level=logging.DEBUG)
# Parser instantiation. simpleLanguage is root definition and comment is # Parser instantiation. simpleLanguage is root definition and comment is
# grammar rule for comments. # grammar rule for comments.
parser = ParserPython(simpleLanguage, comment) parser = ParserPython(simpleLanguage, comment, debug=True)
# We save parser model to dot file in order to visualise it. # We save parser model to dot file in order to visualise it.
# We can make a jpg out of it using dot (part of graphviz) like this # We can make a jpg out of it using dot (part of graphviz) like this
# dot -Tjpg -O simple_parser.dot # dot -Tjpg -O simple_parser.dot
PMDOTExport().exportFile(parser.parser_model, PMDOTExport().exportFile(parser.parser_model,
"simple_parser_model.dot") "simple_parser_model.dot")
# Parser model for comments is handled as separate model # Parser model for comments is handled as separate model
PMDOTExport().exportFile(parser.comments_model, PMDOTExport().exportFile(parser.comments_model,
"simple_parser_comments.dot") "simple_parser_comments.dot")
input = """ input = """
function fak(n) { function fak(n) {
if (n==0) { if (n==0) {
...@@ -58,9 +56,9 @@ try: ...@@ -58,9 +56,9 @@ try:
} }
""" """
parse_tree = parser.parse(input) parse_tree = parser.parse(input)
PTDOTExport().exportFile(parse_tree, PTDOTExport().exportFile(parse_tree,
"simple_parse_tree.dot") "simple_parse_tree.dot")
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)))
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