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

Making examples testable + test for examples.

parent 2a5b5d24
......@@ -3,7 +3,7 @@
# Name: bibtex.py
# Purpose: Parser for bibtex files
# 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
#
# This example demonstrates grammar and parser for bibtex files.
......@@ -11,7 +11,7 @@
from __future__ import print_function
import pprint
import sys
import sys, os
from arpeggio import *
from arpeggio.export import PMDOTExporter, PTDOTExporter
from arpeggio import RegExMatch as _
......@@ -105,36 +105,44 @@ field.sem = FieldSem()
fieldvalue_braces.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.
# 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
PMDOTExporter().exportFile(parser.parser_model, "bib_parse_tree_model.dot")
if debug:
# 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
PMDOTExporter().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()
if not file_name:
file_name = os.path.join(os.path.dirname(__file__), 'bibtex_example.bib')
with open(file_name, "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
parse_tree = parser.parse(bibtexfile_content)
# Then we export it to a dot file in order to visualize it.
PTDOTExporter().exportFile(parse_tree, "bib_parse_tree.dot")
if debug:
# Then we export it to a dot file in order to visualize it.
PTDOTExporter().exportFile(parse_tree, "bib_parse_tree.dot")
# getASG will start semantic analysis.
# In this case semantic analysis will list of bibentry maps.
ast = parser.getASG()
# getASG will start semantic analysis.
# In this case semantic analysis will list of bibentry maps.
ast = parser.getASG()
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(ast)
return 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:
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,7 +29,8 @@ class ToFloat(SemanticAction):
Converts node value to float.
"""
def first_pass(self, parser, node, children):
print("Converting {}.".format(node.value))
if parser.debug:
print("Converting {}.".format(node.value))
return float(node.value)
......@@ -38,7 +39,8 @@ class Factor(SemanticAction):
Removes parenthesis if exists and returns what was contained inside.
"""
def first_pass(self, parser, node, children):
print("Factor {}".format(children))
if parser.debug:
print("Factor {}".format(children))
if len(children) == 1:
return children[0]
sign = -1 if children[0] == '-' else 1
......@@ -54,14 +56,16 @@ class Term(SemanticAction):
Factor nodes will be already evaluated.
"""
def first_pass(self, parser, node, children):
print("Term {}".format(children))
if parser.debug:
print("Term {}".format(children))
term = children[0]
for i in range(2, len(children), 2):
if children[i-1] == "*":
term *= children[i]
else:
term /= children[i]
print("Term = {}".format(term))
if parser.debug:
print("Term = {}".format(term))
return term
......@@ -71,7 +75,8 @@ class Expr(SemanticAction):
Term nodes will be already evaluated.
"""
def first_pass(self, parser, node, children):
print("Expression {}".format(children))
if parser.debug:
print("Expression {}".format(children))
expr = 0
start = 0
# Check for unary + or - operator
......@@ -84,7 +89,9 @@ class Expr(SemanticAction):
else:
expr += children[i]
print("Expression = {}".format(expr))
if parser.debug:
print("Expression = {}".format(expr))
return expr
......@@ -94,19 +101,19 @@ factor.sem = Factor()
term.sem = Term()
expression.sem = Expr()
if __name__ == "__main__":
def main(debug=False):
# 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
# are using ParserPython class.
parser = ParserPython(calc)
parser = ParserPython(calc, debug=debug)
# Then we export it to a dot file in order to visualise it.
# 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
# dot -O -Tpng calc_parse_tree_model.dot
PMDOTExporter().exportFile(parser.parser_model,
"calc_parse_tree_model.dot")
if debug:
# Then we export it to a dot file in order to visualise it.
# 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
# dot -O -Tpng calc_parse_tree_model.dot
PMDOTExporter().exportFile(parser.parser_model,
"calc_parse_tree_model.dot")
# An expression we want to evaluate
input_expr = "-(4-1)*5+(2+4.67)+5.89/(.2+7)"
......@@ -114,11 +121,19 @@ if __name__ == "__main__":
# We create a parse tree out of textual input_expr
parse_tree = parser.parse(input_expr)
# Then we export it to a dot file in order to visualise it.
# This is also optional.
PTDOTExporter().exportFile(parse_tree, "calc_parse_tree.dot")
if debug:
# Then we export it to a dot file in order to visualise it.
# This is also optional.
PTDOTExporter().exportFile(parse_tree, "calc_parse_tree.dot")
result = parser.getASG()
if debug:
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# returned value will be the result of the input_expr expression.
print("{} = {}".format(input_expr, result))
if __name__ == "__main__":
main(debug=True)
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# returned value will be the result of the input_expr expression.
print("{} = {}".format(input_expr, parser.getASG()))
......@@ -38,28 +38,36 @@ sem_actions = {
"expression" : Expr(),
}
def main(debug=False):
# 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
# are using ParserPEG class. Root rule name (parsing expression) is "calc".
parser = ParserPEG(calc_grammar, "calc", debug=True)
# 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
# are using ParserPEG class. Root rule name (parsing expression) is "calc".
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.
PMDOTExporter().exportFile(parser.parser_model, "calc_peg_parser_model.dot")
# An expression we want to evaluate
input_expr = "-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# An expression we want to evaluate
input_expr = "-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# Then parse tree is created out of the input_expr expression.
parse_tree = parser.parse(input_expr)
# Then parse tree is created out of the input_expr expression.
parse_tree = parser.parse(input_expr)
if debug:
# 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.
PTDOTExporter().exportFile(parse_tree, "calc_peg_parse_tree.dot")
result = parser.getASG(sem_actions)
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# returned value will be evaluated result of the input_expr expression.
# Semantic actions are supplied to the getASG function.
print("{} = {}".format(input_expr, parser.getASG(sem_actions)))
if debug:
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# returned value will be evaluated result of the input_expr expression.
# Semantic actions are supplied to the getASG function.
print("{} = {}".format(input_expr, result))
if __name__ == "__main__":
main(debug=True)
......@@ -16,34 +16,37 @@ def field_content(): return _(r'([^,\n])+')
def field_content_quoted(): return _(r'(("")|([^"]))+')
def csvfile(): return OneOrMore([record, '\n']), EOF
if __name__ == "__main__":
test_data = '''
test_data = '''
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 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.
# Parser model is given in the form of python constructs therefore we
# are using ParserPython class.
# Skipping of whitespace will be done only for tabs and spaces. Newlines
# 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)
# Then we export it to a dot file in order to visualise it.
# 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:
# dot -O -Tpng calc_parse_tree_model.dot
PMDOTExporter().exportFile(parser.parser_model, "csv_parse_tree_model.dot")
if debug:
# Then we export it to a dot file in order to visualise it.
# 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:
# dot -O -Tpng calc_parse_tree_model.dot
PMDOTExporter().exportFile(parser.parser_model, "csv_parse_tree_model.dot")
# Creating parse tree out of textual input
parse_tree = parser.parse(test_data)
# Then we export it to a dot file in order to visualise it.
# This is also optional.
# dot -O -Tpng calc_parse_tree.dot
PTDOTExporter().exportFile(parse_tree, "csv_parse_tree.dot")
if debug:
# Then we export it to a dot file in order to visualise it.
# This is also optional.
# dot -O -Tpng calc_parse_tree.dot
PTDOTExporter().exportFile(parse_tree, "csv_parse_tree.dot")
if __name__ == "__main__":
main(debug=True)
......@@ -2,7 +2,7 @@
# Name: json.py
# Purpose: Implementation of a simple JSON parser in arpeggio.
# 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
#
# This example is based on jsonParser.py from the pyparsing project
......@@ -51,44 +51,51 @@ def jsonObject(): return "{", Optional(jsonMembers), "}"
def jsonFile(): return jsonObject, EOF
if __name__ == "__main__":
testdata = """
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList":
{
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"TrueValue": true,
"FalseValue": false,
"Gravity": -9.8,
"LargestPrimeLessThan100": 97,
"AvogadroNumber": 6.02E23,
"EvenPrimesGreaterThan2": null,
"PrimesLessThan10" : [2,3,5,7],
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML", "markup"],
"EmptyDict": {},
"EmptyList" : []
}
}
testdata = """
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList":
{
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"TrueValue": true,
"FalseValue": false,
"Gravity": -9.8,
"LargestPrimeLessThan100": 97,
"AvogadroNumber": 6.02E23,
"EvenPrimesGreaterThan2": null,
"PrimesLessThan10" : [2,3,5,7],
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML", "markup"],
"EmptyDict": {},
"EmptyList" : []
}
}
}
"""
}
"""
def main(debug=False):
# Creating parser from parser model.
parser = ParserPython(jsonFile, debug=True)
parser = ParserPython(jsonFile, debug=debug)
# Exporting parser model to dot file in order to visualise it.
PMDOTExporter().exportFile(parser.parser_model, "json_parser_model.dot")
if debug:
# Exporting parser model to dot file in order to visualise it.
PMDOTExporter().exportFile(parser.parser_model, "json_parser_model.dot")
# Parse json string
parse_tree = parser.parse(testdata)
# Export parse tree for visualization
PTDOTExporter().exportFile(parser.parse_tree, "json_parse_tree.dot")
if debug:
# Export parse tree for visualization
PTDOTExporter().exportFile(parser.parse_tree, "json_parse_tree.dot")
if __name__ == "__main__":
main(debug=True)
......@@ -60,34 +60,43 @@ peg_grammar = r"""
comment <- '//' r'.*\n';
"""
def main(debug=False):
# ParserPEG will use ParserPython to parse peg_grammar definition and
# create parser_model for parsing PEG based grammars
parser = ParserPEG(peg_grammar, 'grammar', debug=True)
# ParserPEG will use ParserPython to parse peg_grammar definition and
# create parser_model for parsing PEG based grammars
parser = ParserPEG(peg_grammar, 'grammar', debug=debug)
# Exporting parser model to dot file for visualization.
PMDOTExporter().exportFile(parser.parser_model,
"peg_peg_parser_model.dot")
# 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
# using PEG itself.
parser.parse(peg_grammar)
if debug:
# Exporting parser model to dot file for visualization.
PMDOTExporter().exportFile(parser.parser_model,
"peg_peg_parser_model.dot")
# Again we export parse tree in dot file for visualization.
PTDOTExporter().exportFile(parser.parse_tree,
"peg_peg_parse_tree.dot")
# 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
# using PEG itself.
parser.parse(peg_grammar)
# ASG should be the same as parser.parser_model because semantic
# actions will create PEG parser (tree of ParsingExpressions).
asg = parser.getASG(sem_actions)
if debug:
# Again we export parse tree in dot file for visualization.
PTDOTExporter().exportFile(parser.parse_tree,
"peg_peg_parse_tree.dot")
# This graph should be the same as peg_peg_parser_model.dot because
# they define the same parser.
PMDOTExporter().exportFile(asg,
"peg_peg_asg.dot")
# ASG should be the same as parser.parser_model because semantic
# actions will create PEG parser (tree of ParsingExpressions).
asg = parser.getASG(sem_actions)
if debug:
# This graph should be the same as peg_peg_parser_model.dot because
# they define the same parser.
PMDOTExporter().exportFile(asg,
"peg_peg_asg.dot")
# If we replace parser_mode with ASG constructed parser it will still
# parse PEG grammars
parser.parser_model = asg
parser.parse(peg_grammar)
if __name__ == '__main__':
main(debug=True)
# If we replace parser_mode with ASG constructed parser it will still
# parse PEG grammars
parser.parser_model = asg
parser.parse(peg_grammar)
......@@ -35,37 +35,43 @@ def right(): return 'right'
# Semantic actions
class Up(SemanticAction):
def first_pass(self, parser, node, children):
print("Going up")
if parser.debug:
print("Going up")
return (0, 1)
class Down(SemanticAction):
def first_pass(self, parser, node, children):
print("Going down")
if parser.debug:
print("Going down")
return (0, -1)
class Left(SemanticAction):
def first_pass(self, parser, node, children):
print("Going left")
if parser.debug:
print("Going left")
return (-1, 0)
class Right(SemanticAction):
def first_pass(self, parser, node, children):
print("Going right")
if parser.debug:
print("Going right")
return (1, 0)
class Command(SemanticAction):
def first_pass(self, parser, node, children):
print("Command")
if parser.debug:
print("Command")
return children[0]
class Program(SemanticAction):
def first_pass(self, parser, node, children):
print("Evaluating position")
if parser.debug:
print("Evaluating position")
position = [0, 0]
for move in children[1:-2]:
position[0] += move[0]
......@@ -80,8 +86,7 @@ down.sem = Down()
left.sem = Left()
right.sem = Right()
if __name__ == "__main__":
def main(debug=False):
# Program code
input_program = '''
begin
......@@ -96,24 +101,33 @@ if __name__ == "__main__":
# 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
# are using ParserPython class.
parser = ParserPython(program, debug=True)
parser = ParserPython(program, debug=debug)
# Then we export it to a dot file in order to visualize it.
# 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
# dot -O -Tpng robot_parser_model.dot
PMDOTExporter().exportFile(parser.parser_model,
"robot_parser_model.dot")
if debug:
# Then we export it to a dot file in order to visualize it.
# 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
# dot -O -Tpng robot_parser_model.dot
PMDOTExporter().exportFile(parser.parser_model,
"robot_parser_model.dot")
# We create a parse tree out of textual input
parse_tree = parser.parse(input_program)
# Then we export it to a dot file in order to visualize it.
# dot -O -Tpng robot_parse_tree.dot
PTDOTExporter().exportFile(parse_tree,
"robot_parse_tree.dot")
if debug:
# Then we export it to a dot file in order to visualize it.
# dot -O -Tpng robot_parse_tree.dot
PTDOTExporter().exportFile(parse_tree,
"robot_parse_tree.dot")
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# 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 = {
}
if __name__ == "__main__":
def main(debug=False):
# Program code
input = '''
begin
......@@ -63,25 +62,30 @@ if __name__ == "__main__":
# 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
# are using ParserPEG class.
parser = ParserPEG(robot_grammar, 'program', debug=True)
parser = ParserPEG(robot_grammar, 'program', debug=debug)
# Then we export it to a dot file in order to visualize it.
# 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
# dot -O -Tpng robot_peg_parser_model.dot
PMDOTExporter().exportFile(parser.parser_model,
"robot_peg_parser_model.dot")
if debug:
# Then we export it to a dot file in order to visualize it.
# 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
# dot -O -Tpng robot_peg_parser_model.dot
PMDOTExporter().exportFile(parser.parser_model,
"robot_peg_parser_model.dot")
# We create a parse tree out of textual input
parse_tree = parser.parse(input)
# Then we export it to a dot file in order to visualize it.
# dot -O -Tpng robot_peg_parse_tree.dot
PTDOTExporter().exportFile(parse_tree,
"robot_peg_parse_tree.dot")
if debug:
# Then we export it to a dot file in order to visualize it.
# dot -O -Tpng robot_peg_parse_tree.dot
PTDOTExporter().exportFile(parse_tree,
"robot_peg_parse_tree.dot")
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# 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, ")"
def function(): return Kwd("function"), symbol, parameterlist, block
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 = """
function fak(n) {
if (n==0) {
......@@ -53,8 +40,29 @@ input = """
};
}
"""
parse_tree = parser.parse(input)
# Export parse tree for visualization
PTDOTExporter().exportFile(parse_tree, "simple_parse_tree.dot")
def main(debug=False):
# 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