Commit 6a197bfd authored by Igor Dejanovic's avatar Igor Dejanovic

Making examples testable + test for examples.

parent 2a5b5d24
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Name: bibtex.py # Name: bibtex.py
# Purpose: Parser for bibtex files # Purpose: Parser for bibtex files
# Author: Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com> # Author: Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2013 Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com> # Copyright: (c) 2013-2014 Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# License: MIT License # License: MIT License
# #
# This example demonstrates grammar and parser for bibtex files. # This example demonstrates grammar and parser for bibtex files.
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
from __future__ import print_function from __future__ import print_function
import pprint import pprint
import sys import sys, os
from arpeggio import * from arpeggio import *
from arpeggio.export import PMDOTExporter, PTDOTExporter from arpeggio.export import PMDOTExporter, PTDOTExporter
from arpeggio import RegExMatch as _ from arpeggio import RegExMatch as _
...@@ -105,27 +105,30 @@ field.sem = FieldSem() ...@@ -105,27 +105,30 @@ field.sem = FieldSem()
fieldvalue_braces.sem = FieldValueSem() fieldvalue_braces.sem = FieldValueSem()
fieldvalue_quotes.sem = FieldValueSem() fieldvalue_quotes.sem = FieldValueSem()
if __name__ == "__main__": def main(debug=False, file_name=None):
# First we will make a parser - an instance of the bib parser model. # 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 # Parser model is given in the form of python constructs therefore we
# are using ParserPython class. # are using ParserPython class.
parser = ParserPython(bibfile, reduce_tree=True) parser = ParserPython(bibfile, reduce_tree=True)
if debug:
# Then we export it to a dot file in order to visualise it. This is # Then we export it to a dot file in order to visualise it. This is
# particulary handy for debugging purposes. # particulary handy for debugging purposes.
# 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 -O -Tjpg calc_parse_tree_model.dot # dot -O -Tjpg calc_parse_tree_model.dot
PMDOTExporter().exportFile(parser.parser_model, "bib_parse_tree_model.dot") PMDOTExporter().exportFile(parser.parser_model, "bib_parse_tree_model.dot")
# First parameter is bibtex file if not file_name:
if len(sys.argv) > 1: file_name = os.path.join(os.path.dirname(__file__), 'bibtex_example.bib')
with open(sys.argv[1], "r") as bibtexfile:
with open(file_name, "r") as bibtexfile:
bibtexfile_content = bibtexfile.read() bibtexfile_content = bibtexfile.read()
# We create a parse tree or abstract syntax tree out of # We create a parse tree or abstract syntax tree out of
# textual input # textual input
parse_tree = parser.parse(bibtexfile_content) parse_tree = parser.parse(bibtexfile_content)
if debug:
# 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.
PTDOTExporter().exportFile(parse_tree, "bib_parse_tree.dot") PTDOTExporter().exportFile(parse_tree, "bib_parse_tree.dot")
...@@ -133,8 +136,13 @@ if __name__ == "__main__": ...@@ -133,8 +136,13 @@ if __name__ == "__main__":
# In this case semantic analysis will list of bibentry maps. # In this case semantic analysis will list of bibentry maps.
ast = parser.getASG() ast = parser.getASG()
pp = pprint.PrettyPrinter(indent=4) return ast
pp.pprint(ast)
if __name__ == "__main__":
# First parameter is bibtex file
if len(sys.argv) > 1:
entries = main(debug=True, file_name=sys.argv[1])
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(entries)
else: else:
print("Usage: python bibtex.py file_to_parse") print("Usage: python bibtex.py file_to_parse")
***
*** M6x
***
@inproceedings{DejanovicArpeggioPakratparserinterpreter2010,
author = "Igor Dejanovi\'{c} and Branko Peri\v{s}i\'{c} and Gordana Milosavljevi\'{c}",
title = "Arpeggio: Pakrat parser interpreter",
booktitle = "Zbornik radova na CD-ROM-u, YUInfo 2010",
year = "2010",
address = "Kopaonik",
type = "M63"
}
***
*** M2x
***
@article{DejanovicADomain-SpecificLanguageforDefiningStaticStructureofDatabaseApplications2010,
author = "Igor Dejanovi\'{c} and Gordana Milosavljevi\'{c} and Branko Peri\v{s}i\'{c} and Maja Tumbas",
title = "A {D}omain-Specific Language for Defining Static Structure of Database Applications",
journal = "Computer Science and Information Systems",
year = "2010",
volume = "7",
pages = "409--440",
number = "3",
month = "June",
issn = "1820-0214",
doi = "10.2298/CSIS090203002D",
url = "http://www.comsis.org/ComSIS/Vol7No3/RegularPapers/paper2.htm",
type = "M23"
}
***
*** M3x
***
@inproceedings{MilosavljevicUMLProfileForSpecifyingUI2010,
author = "Gordana Milosavljevi\'{c} and Igor Dejanovi\'{c} and Branko Peri\v{s}i\'{c} and Branko Milosavljevi\'{c}",
title = "UML Profile for Specifying User Interfaces of Business Applications",
booktitle = "Advances in Databases and Information Systems",
year = "2010",
pages = "77-94",
address = "Novi Sad",
type = "M33"
}
@inproceedings{DejanovicComparisonofTextualandVisualNotationsofDOMMLiteDomainSpecificLanguage2010,
author = "Igor Dejanovi\'{c} and Maja Tumbas \v{Z}ivanov and Gordana Milosavljevi\'{c} and Branko Peri\v{s}i\'{c}",
title = "Comparison of Textual and Visual Notations of DOMMLite Domain-Specific Language",
booktitle = "Proceedings of the Advances in Databases and Information Systems",
year = "2010",
pages = "20-24",
address = "Novi Sad",
type = "M33"
}
@article{DejanovicReact2010,
author = "Mirjana Dejanovi\'{c} and Igor Dejanovi\'{c}",
title = "React! An Extensible Software Application for Creating, Performing and Analyzing Results of Psychophysiological Experiments",
journal = "International Journal of Psychophysiology",
year = "2010",
volume = "77",
pages = "301 - 301",
number = "3",
note = "PROCEEDINGS OF THE 15TH WORLD CONGRESS OF PSYCHOPHYSIOLOGY of the
International Organization of Psychophysiology (I.O.P.) Budapest,
Hungary September 1-4, 2010",
doi = "10.1016/j.ijpsycho.2010.06.193",
issn = "0167-8760",
url = "http://www.sciencedirect.com/science/article/B6T3M-50R1KFN-93/2/ca6c51240ae3c5db819938c3fbfd759a",
type = "M34"
}
...@@ -29,6 +29,7 @@ class ToFloat(SemanticAction): ...@@ -29,6 +29,7 @@ class ToFloat(SemanticAction):
Converts node value to float. Converts node value to float.
""" """
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug:
print("Converting {}.".format(node.value)) print("Converting {}.".format(node.value))
return float(node.value) return float(node.value)
...@@ -38,6 +39,7 @@ class Factor(SemanticAction): ...@@ -38,6 +39,7 @@ class Factor(SemanticAction):
Removes parenthesis if exists and returns what was contained inside. Removes parenthesis if exists and returns what was contained inside.
""" """
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug:
print("Factor {}".format(children)) print("Factor {}".format(children))
if len(children) == 1: if len(children) == 1:
return children[0] return children[0]
...@@ -54,6 +56,7 @@ class Term(SemanticAction): ...@@ -54,6 +56,7 @@ class Term(SemanticAction):
Factor nodes will be already evaluated. Factor nodes will be already evaluated.
""" """
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug:
print("Term {}".format(children)) print("Term {}".format(children))
term = children[0] term = children[0]
for i in range(2, len(children), 2): for i in range(2, len(children), 2):
...@@ -61,6 +64,7 @@ class Term(SemanticAction): ...@@ -61,6 +64,7 @@ class Term(SemanticAction):
term *= children[i] term *= children[i]
else: else:
term /= children[i] term /= children[i]
if parser.debug:
print("Term = {}".format(term)) print("Term = {}".format(term))
return term return term
...@@ -71,6 +75,7 @@ class Expr(SemanticAction): ...@@ -71,6 +75,7 @@ class Expr(SemanticAction):
Term nodes will be already evaluated. Term nodes will be already evaluated.
""" """
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug:
print("Expression {}".format(children)) print("Expression {}".format(children))
expr = 0 expr = 0
start = 0 start = 0
...@@ -84,7 +89,9 @@ class Expr(SemanticAction): ...@@ -84,7 +89,9 @@ class Expr(SemanticAction):
else: else:
expr += children[i] expr += children[i]
if parser.debug:
print("Expression = {}".format(expr)) print("Expression = {}".format(expr))
return expr return expr
...@@ -94,13 +101,13 @@ factor.sem = Factor() ...@@ -94,13 +101,13 @@ factor.sem = Factor()
term.sem = Term() term.sem = Term()
expression.sem = Expr() expression.sem = Expr()
if __name__ == "__main__": def main(debug=False):
# 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 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(calc) parser = ParserPython(calc, debug=debug)
if debug:
# Then we export it to a dot file in order to visualise it. # Then we export it to a dot file in order to visualise it.
# This step is optional but it is handy for debugging purposes. # This step is optional but it is handy for debugging purposes.
# We can make a png out of it using dot (part of graphviz) like this # We can make a png out of it using dot (part of graphviz) like this
...@@ -114,11 +121,19 @@ if __name__ == "__main__": ...@@ -114,11 +121,19 @@ if __name__ == "__main__":
# We create a parse tree out of textual input_expr # We create a parse tree out of textual input_expr
parse_tree = parser.parse(input_expr) parse_tree = parser.parse(input_expr)
if debug:
# Then we export it to a dot file in order to visualise it. # Then we export it to a dot file in order to visualise it.
# This is also optional. # This is also optional.
PTDOTExporter().exportFile(parse_tree, "calc_parse_tree.dot") PTDOTExporter().exportFile(parse_tree, "calc_parse_tree.dot")
result = parser.getASG()
if debug:
# getASG will start semantic analysis. # getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and # In this case semantic analysis will evaluate expression and
# returned value will be the result of the input_expr expression. # returned value will be the result of the input_expr expression.
print("{} = {}".format(input_expr, parser.getASG())) print("{} = {}".format(input_expr, result))
if __name__ == "__main__":
main(debug=True)
...@@ -38,28 +38,36 @@ sem_actions = { ...@@ -38,28 +38,36 @@ sem_actions = {
"expression" : Expr(), "expression" : Expr(),
} }
def main(debug=False):
# 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", debug=True) parser = ParserPEG(calc_grammar, "calc", debug=debug)
if debug:
# Then we export it to a dot file.
PMDOTExporter().exportFile(parser.parser_model, "calc_peg_parser_model.dot")
# Then we export it to a dot file. # An expression we want to evaluate
PMDOTExporter().exportFile(parser.parser_model, "calc_peg_parser_model.dot") input_expr = "-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# An expression we want to evaluate # Then parse tree is created out of the input_expr expression.
input_expr = "-(4-1)*5+(2+4.67)+5.89/(.2+7)" parse_tree = parser.parse(input_expr)
# Then parse tree is created out of the input_expr expression. if debug:
parse_tree = parser.parse(input_expr) # We save it to dot file in order to visualise it.
PTDOTExporter().exportFile(parse_tree, "calc_peg_parse_tree.dot")
# We save it to dot file in order to visualise it. result = parser.getASG(sem_actions)
PTDOTExporter().exportFile(parse_tree, "calc_peg_parse_tree.dot")
# getASG will start semantic analysis. if debug:
# In this case semantic analysis will evaluate expression and # getASG will start semantic analysis.
# returned value will be evaluated result of the input_expr expression. # In this case semantic analysis will evaluate expression and
# Semantic actions are supplied to the getASG function. # returned value will be evaluated result of the input_expr expression.
print("{} = {}".format(input_expr, parser.getASG(sem_actions))) # Semantic actions are supplied to the getASG function.
print("{} = {}".format(input_expr, result))
if __name__ == "__main__":
main(debug=True)
...@@ -16,23 +16,22 @@ def field_content(): return _(r'([^,\n])+') ...@@ -16,23 +16,22 @@ def field_content(): return _(r'([^,\n])+')
def field_content_quoted(): return _(r'(("")|([^"]))+') def field_content_quoted(): return _(r'(("")|([^"]))+')
def csvfile(): return OneOrMore([record, '\n']), EOF def csvfile(): return OneOrMore([record, '\n']), EOF
test_data = '''
if __name__ == "__main__":
test_data = '''
Unquoted test, "Quoted test", 23234, One Two Three, "343456.45" Unquoted test, "Quoted test", 23234, One Two Three, "343456.45"
Unquoted test 2, "Quoted test with ""inner"" quotes", 23234, One Two Three, "343456.45" Unquoted test 2, "Quoted test with ""inner"" quotes", 23234, One Two Three, "343456.45"
Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45" Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45"
''' '''
def main(debug=False):
# First we will make a parser - an instance of the CVS parser model. # First we will make a parser - an instance of the CVS 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.
# Skipping of whitespace will be done only for tabs and spaces. Newlines # Skipping of whitespace will be done only for tabs and spaces. Newlines
# have semantics in csv files. They are used to separate records. # have semantics in csv files. They are used to separate records.
parser = ParserPython(csvfile, ws='\t ', reduce_tree=True, debug=True) parser = ParserPython(csvfile, ws='\t ', reduce_tree=True, debug=debug)
if debug:
# Then we export it to a dot file in order to visualise it. # Then we export it to a dot file in order to visualise it.
# This step is optional but it is handy for debugging purposes. # This step is optional but it is handy for debugging purposes.
# We can make a png out of it using dot (part of graphviz) like this: # We can make a png out of it using dot (part of graphviz) like this:
...@@ -42,8 +41,12 @@ Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45" ...@@ -42,8 +41,12 @@ Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45"
# Creating parse tree out of textual input # Creating parse tree out of textual input
parse_tree = parser.parse(test_data) parse_tree = parser.parse(test_data)
if debug:
# Then we export it to a dot file in order to visualise it. # Then we export it to a dot file in order to visualise it.
# This is also optional. # This is also optional.
# dot -O -Tpng calc_parse_tree.dot # dot -O -Tpng calc_parse_tree.dot
PTDOTExporter().exportFile(parse_tree, "csv_parse_tree.dot") PTDOTExporter().exportFile(parse_tree, "csv_parse_tree.dot")
if __name__ == "__main__":
main(debug=True)
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# Name: json.py # Name: json.py
# Purpose: Implementation of a simple JSON parser in arpeggio. # Purpose: Implementation of a simple JSON parser in arpeggio.
# 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-2014 Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# License: MIT License # License: MIT License
# #
# This example is based on jsonParser.py from the pyparsing project # This example is based on jsonParser.py from the pyparsing project
...@@ -51,9 +51,8 @@ def jsonObject(): return "{", Optional(jsonMembers), "}" ...@@ -51,9 +51,8 @@ def jsonObject(): return "{", Optional(jsonMembers), "}"
def jsonFile(): return jsonObject, EOF def jsonFile(): return jsonObject, EOF
if __name__ == "__main__": testdata = """
testdata = """ {
{
"glossary": { "glossary": {
"title": "example glossary", "title": "example glossary",
"GlossDiv": { "GlossDiv": {
...@@ -79,16 +78,24 @@ if __name__ == "__main__": ...@@ -79,16 +78,24 @@ if __name__ == "__main__":
} }
} }
} }
} }
""" """
def main(debug=False):
# Creating parser from parser model. # Creating parser from parser model.
parser = ParserPython(jsonFile, debug=True) parser = ParserPython(jsonFile, debug=debug)
if debug:
# Exporting parser model to dot file in order to visualise it. # Exporting parser model to dot file in order to visualise it.
PMDOTExporter().exportFile(parser.parser_model, "json_parser_model.dot") PMDOTExporter().exportFile(parser.parser_model, "json_parser_model.dot")
# Parse json string # Parse json string
parse_tree = parser.parse(testdata) parse_tree = parser.parse(testdata)
if debug:
# Export parse tree for visualization # Export parse tree for visualization
PTDOTExporter().exportFile(parser.parse_tree, "json_parse_tree.dot") PTDOTExporter().exportFile(parser.parse_tree, "json_parse_tree.dot")
if __name__ == "__main__":
main(debug=True)
...@@ -60,34 +60,43 @@ peg_grammar = r""" ...@@ -60,34 +60,43 @@ peg_grammar = r"""
comment <- '//' r'.*\n'; comment <- '//' r'.*\n';
""" """
def main(debug=False):
# 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', debug=True) parser = ParserPEG(peg_grammar, 'grammar', debug=debug)
# Exporting parser model to dot file for visualization.
PMDOTExporter().exportFile(parser.parser_model, if debug:
# Exporting parser model to dot file for visualization.
PMDOTExporter().exportFile(parser.parser_model,
"peg_peg_parser_model.dot") "peg_peg_parser_model.dot")
# Now we will use created parser to parse the same peg_grammar used for # Now we will use created parser to parse the same peg_grammar used for
# parser initialization. We can parse peg_grammar because it is specified # parser initialization. We can parse peg_grammar because it is specified
# using PEG itself. # using PEG itself.
parser.parse(peg_grammar) parser.parse(peg_grammar)
# Again we export parse tree in dot file for visualization. if debug:
PTDOTExporter().exportFile(parser.parse_tree, # Again we export parse tree in dot file for visualization.
PTDOTExporter().exportFile(parser.parse_tree,
"peg_peg_parse_tree.dot") "peg_peg_parse_tree.dot")
# ASG should be the same as parser.parser_model because semantic # ASG should be the same as parser.parser_model because semantic
# actions will create PEG parser (tree of ParsingExpressions). # actions will create PEG parser (tree of ParsingExpressions).
asg = parser.getASG(sem_actions) asg = parser.getASG(sem_actions)
# This graph should be the same as peg_peg_parser_model.dot because if debug:
# they define the same parser. # This graph should be the same as peg_peg_parser_model.dot because
PMDOTExporter().exportFile(asg, # they define the same parser.
PMDOTExporter().exportFile(asg,
"peg_peg_asg.dot") "peg_peg_asg.dot")
# If we replace parser_mode with ASG constructed parser it will still # If we replace parser_mode with ASG constructed parser it will still
# parse PEG grammars # parse PEG grammars
parser.parser_model = asg parser.parser_model = asg
parser.parse(peg_grammar) parser.parse(peg_grammar)
if __name__ == '__main__':
main(debug=True)
...@@ -35,36 +35,42 @@ def right(): return 'right' ...@@ -35,36 +35,42 @@ def right(): return 'right'
# Semantic actions # Semantic actions
class Up(SemanticAction): class Up(SemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug:
print("Going up") print("Going up")
return (0, 1) return (0, 1)
class Down(SemanticAction): class Down(SemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug:
print("Going down") print("Going down")
return (0, -1) return (0, -1)
class Left(SemanticAction): class Left(SemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug:
print("Going left") print("Going left")
return (-1, 0) return (-1, 0)
class Right(SemanticAction): class Right(SemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug:
print("Going right") print("Going right")
return (1, 0) return (1, 0)
class Command(SemanticAction): class Command(SemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug:
print("Command") print("Command")
return children[0] return children[0]
class Program(SemanticAction): class Program(SemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug:
print("Evaluating position") print("Evaluating position")
position = [0, 0] position = [0, 0]
for move in children[1:-2]: for move in children[1:-2]:
...@@ -80,8 +86,7 @@ down.sem = Down() ...@@ -80,8 +86,7 @@ down.sem = Down()
left.sem = Left() left.sem = Left()
right.sem = Right() right.sem = Right()
if __name__ == "__main__": def main(debug=False):
# Program code # Program code
input_program = ''' input_program = '''
begin begin
...@@ -96,8 +101,9 @@ if __name__ == "__main__": ...@@ -96,8 +101,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, debug=True) parser = ParserPython(program, debug=debug)
if debug:
# 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.
# This step is optional but it is handy for debugging purposes. # This step is optional but it is handy for debugging purposes.
# We can make a png out of it using dot (part of graphviz) like this # We can make a png out of it using dot (part of graphviz) like this
...@@ -108,6 +114,7 @@ if __name__ == "__main__": ...@@ -108,6 +114,7 @@ if __name__ == "__main__":
# We create a parse tree out of textual input # We create a parse tree out of textual input
parse_tree = parser.parse(input_program) parse_tree = parser.parse(input_program)
if debug:
# 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 -Tpng robot_parse_tree.dot # dot -O -Tpng robot_parse_tree.dot
PTDOTExporter().exportFile(parse_tree, PTDOTExporter().exportFile(parse_tree,
...@@ -116,4 +123,11 @@ if __name__ == "__main__": ...@@ -116,4 +123,11 @@ if __name__ == "__main__":
# getASG will start semantic analysis. # getASG will start semantic analysis.
# 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()) result = parser.getASG()
if debug:
print("position = ", result)
if __name__ == "__main__":
main(debug=True)
...@@ -46,8 +46,7 @@ semantic_actions = { ...@@ -46,8 +46,7 @@ semantic_actions = {
} }
if __name__ == "__main__": def main(debug=False):
# Program code # Program code
input = ''' input = '''
begin begin
...@@ -63,8 +62,9 @@ if __name__ == "__main__": ...@@ -63,8 +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', debug=True) parser = ParserPEG(robot_grammar, 'program', debug=debug)
if debug:
# 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.
# This step is optional but it is handy for debugging purposes. # This step is optional but it is handy for debugging purposes.
# We can make a png out of it using dot (part of graphviz) like this # We can make a png out of it using dot (part of graphviz) like this
...@@ -75,6 +75,7 @@ if __name__ == "__main__": ...@@ -75,6 +75,7 @@ if __name__ == "__main__":
# 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)
if debug:
# 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 -Tpng robot_peg_parse_tree.dot # dot -O -Tpng robot_peg_parse_tree.dot
PTDOTExporter().exportFile(parse_tree, PTDOTExporter().exportFile(parse_tree,
...@@ -83,5 +84,8 @@ if __name__ == "__main__": ...@@ -83,5 +84,8 @@ if __name__ == "__main__":
# getASG will start semantic analysis. # getASG will start semantic analysis.
# 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)) return parser.getASG(sem_actions=semantic_actions)
if __name__ == "__main__":
print("position = ", main(debug=True))
...@@ -30,19 +30,6 @@ def functioncall(): return symbol, "(", expressionlist, ")" ...@@ -30,19 +30,6 @@ def functioncall(): return symbol, "(", expressionlist, ")"
def function(): return Kwd("function"), symbol, parameterlist, block def function(): return Kwd("function"), symbol, parameterlist, block
def simpleLanguage(): return function def simpleLanguage(): return function
# Parser instantiation. simpleLanguage is the definition of the root rule
# and comment is a grammar rule for comments.
parser = ParserPython(simpleLanguage, comment, debug=True)
# We save parser model to dot file in order to visualise it.
# We can make a png out of it using dot (part of graphviz) like this
# dot -Tpng -O simple_parser.dot
PMDOTExporter().exportFile(parser.parser_model, "simple_parser_model.dot")
# Parser model for comments is handled as separate model
PMDOTExporter().exportFile(parser.comments_model, "simple_parser_comments.dot")
input = """ input = """
function fak(n) { function fak(n) {
if (n==0) { if (n==0) {
...@@ -53,8 +40,29 @@ input = """ ...@@ -53,8 +40,29 @@ input = """
}; };
} }
""" """
parse_tree = parser.parse(input)
# Export parse tree for visualization def main(debug=False):
PTDOTExporter().exportFile(parse_tree, "simple_parse_tree.dot")
# Parser instantiation. simpleLanguage is the definition of the root rule
# and comment is a grammar rule for comments.
parser = ParserPython(simpleLanguage, comment, debug=debug)
if debug:
# We save parser model to dot file in order to visualise it.
# We can make a png out of it using dot (part of graphviz) like this
# dot -Tpng -O simple_parser.dot
PMDOTExporter().exportFile(parser.parser_model, "simple_parser_model.dot")
# Parser model for comments is handled as separate model
PMDOTExporter().exportFile(parser.comments_model, "simple_parser_comments.dot")
parse_tree = parser.parse(input)
if debug:
# Export parse tree for visualization
PTDOTExporter().exportFile(parse_tree, "simple_parse_tree.dot")
if __name__ == "__main__":
main(debug=True)
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_examples
# Purpose: Test that examples run without errors.
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
import pytest
import os, sys
import imp
def test_examples():
examples_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)),
'../../examples/')
if not examples_dir in sys.path:
sys.path.insert(0, examples_dir)
examples = [f for f in os.listdir(examples_dir) if f != '__init__.py'
and f.endswith('.py')]
for e in examples:
(module_name, _) = os.path.splitext(e)
(module_file, module_path, desc) = imp.find_module(module_name, [examples_dir])
m = imp.load_module(module_name, module_file, module_path, desc)
if hasattr(m, 'main'):
m.main(debug=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