Commit 2d654c9a authored by Igor Dejanovic's avatar Igor Dejanovic

Examples update.

parent 0a3d2c28
......@@ -42,12 +42,12 @@ class BibFileSem(SemanticAction):
"""
Just returns list of child nodes (bibentries).
"""
def first_pass(self, parser, node, nodes):
def first_pass(self, parser, node, children):
if parser.debug:
print "Processing Bibfile"
# Return only dict nodes
return [x for x in nodes if type(x) is dict]
return [x for x in children if type(x) is dict]
class BibEntrySem(SemanticAction):
......@@ -55,14 +55,14 @@ 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):
def first_pass(self, parser, node, children):
if parser.debug:
print " Processing bibentry %s" % nodes[2]
print " Processing bibentry %s" % children[2]
bib_entry_map = {
'bibtype': nodes[0].value,
'bibkey': nodes[2].value
'bibtype': children[0].value,
'bibkey': children[2].value
}
for field in nodes[3:]:
for field in children[3:]:
if isinstance(field, tuple):
bib_entry_map[field[0]] = field[1]
return bib_entry_map
......@@ -72,10 +72,10 @@ class FieldSem(SemanticAction):
"""
Constructs a tuple (fieldname, fieldvalue).
"""
def first_pass(self, parser, node, nodes):
def first_pass(self, parser, node, children):
if parser.debug:
print " Processing field %s" % nodes[0]
field = (nodes[0].value, nodes[2])
print " Processing field %s" % children[0]
field = (children[0].value, children[2])
return field
......@@ -84,8 +84,8 @@ class FieldValueSem(SemanticAction):
Serbian Serbian letters form latex encoding to Unicode.
Remove braces. Remove newlines.
"""
def first_pass(self, parser, node, nodes):
value = nodes[1].value
def first_pass(self, parser, node, children):
value = children[1].value
value = value.replace(r"\'{c}", u"ć")\
.replace(r"\'{C}", u"Ć")\
.replace(r"\v{c}", u"č")\
......@@ -114,8 +114,7 @@ if __name__ == "__main__":
# 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")
PMDOTExporter().exportFile(parser.parser_model, "bib_parse_tree_model.dot")
# First parameter is bibtex file
if len(sys.argv) > 1:
......@@ -127,8 +126,7 @@ if __name__ == "__main__":
parse_tree = parser.parse(bibtexfile_content)
# Then we export it to a dot file in order to visualise it.
PTDOTExporter().exportFile(parse_tree,
"bib_parse_tree.dot")
PTDOTExporter().exportFile(parse_tree, "bib_parse_tree.dot")
# getASG will start semantic analysis.
# In this case semantic analysis will list of bibentry maps.
......
......@@ -27,7 +27,7 @@ class ToFloat(SemanticAction):
"""
Converts node value to float.
"""
def first_pass(self, parser, node, nodes):
def first_pass(self, parser, node, children):
print "Converting %s." % node.value
return float(node.value)
......@@ -35,32 +35,32 @@ class Factor(SemanticAction):
"""
Removes parenthesis if exists and returns what was contained inside.
"""
def first_pass(self, parser, node, nodes):
print "Factor %s" % nodes
if len(nodes) == 1:
return nodes[0]
sign = -1 if nodes[0] == '-' else 1
def first_pass(self, parser, node, children):
print "Factor %s" % children
if len(children) == 1:
return children[0]
sign = -1 if children[0] == '-' else 1
next = 0
if nodes[0] in ['+', '-']:
if children[0] in ['+', '-']:
next = 1
if nodes[next] == '(':
return sign * nodes[next+1]
if children[next] == '(':
return sign * children[next+1]
else:
return sign * nodes[next]
return sign * children[next]
class Term(SemanticAction):
"""
Divides or multiplies factors.
Factor nodes will be already evaluated.
"""
def first_pass(self, parser, node, nodes):
print "Term %s" % nodes
term = nodes[0]
for i in range(2, len(nodes), 2):
if nodes[i-1]=="*":
term *= nodes[i]
def first_pass(self, parser, node, children):
print "Term %s" % children
term = children[0]
for i in range(2, len(children), 2):
if children[i-1]=="*":
term *= children[i]
else:
term /= nodes[i]
term /= children[i]
print "Term = %f" % term
return term
......@@ -69,26 +69,26 @@ class Expr(SemanticAction):
Adds or substracts terms.
Term nodes will be already evaluated.
"""
def first_pass(self, parser, node, nodes):
print "Expression %s" % nodes
def first_pass(self, parser, node, children):
print "Expression %s" % children
expr = 0
start = 0
# Check for unary + or - operator
if str(nodes[0]) in "+-":
if str(children[0]) in "+-":
start = 1
for i in range(start, len(nodes), 2):
if i and nodes[i-1]=="-":
expr -= nodes[i]
for i in range(start, len(children), 2):
if i and children[i-1]=="-":
expr -= children[i]
else:
expr += nodes[i]
expr += children[i]
print "Expression = %f" % expr
return expr
class Calc(SemanticAction):
def first_pass(self, parser, node, nodes):
return nodes[0]
def first_pass(self, parser, node, children):
return children[0]
# Connecting rules with semantic actions
number.sem = ToFloat()
......@@ -98,34 +98,30 @@ expression.sem = Expr()
calc.sem = Calc()
if __name__ == "__main__":
try:
# 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)
# 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 = "-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# 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 visualise it.
# This is also optional.
PTDOTExporter().exportFile(parse_tree,
"calc_parse_tree.dot")
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# returned value will be the result of the input expression.
print "%s = %f" % (input, parser.getASG())
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 calc parser model.
# Parser model is given in the form of python constructs therefore we
# are using ParserPython class.
parser = ParserPython(calc)
# 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 = "-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# 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 visualise it.
# This is also optional.
PTDOTExporter().exportFile(parse_tree, "calc_parse_tree.dot")
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# returned value will be the result of the input expression.
print "%s = %f" % (input, parser.getASG())
......@@ -39,33 +39,28 @@ sem_actions = {
"calc" : Calc()
}
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
# 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=True)
# 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 = "-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# 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)
# 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.
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")
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# 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))
# getASG will start semantic analysis.
# In this case semantic analysis will evaluate expression and
# 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)))
......@@ -26,29 +26,24 @@ Unquoted test 2, "Quoted test with ""inner"" quotes", 23234, One Two Three, "343
Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45"
'''
try:
# 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)
# 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")
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 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)
# 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")
......@@ -81,19 +81,16 @@ if __name__ == "__main__":
}
}
"""
try:
# Creating parser from parser model.
parser = ParserPython(jsonFile, debug=True)
# Creating parser from parser model.
parser = ParserPython(jsonFile, debug=True)
# Exporting parser model to dot file in order to visualise it.
PMDOTExporter().exportFile(parser.parser_model,
"json_parser_model.dot")
# Exporting parser model to dot file in order to visualise it.
PMDOTExporter().exportFile(parser.parser_model, "json_parser_model.dot")
parse_tree = parser.parse(testdata)
# Parse json string
parse_tree = parser.parse(testdata)
PTDOTExporter().exportFile(parser.parse_tree,
"json_parse_tree.dot")
# Export parse tree for visualization
PTDOTExporter().exportFile(parser.parse_tree, "json_parse_tree.dot")
except NoMatch, e:
print "Expected %s at position %s." % (e.value, str(e.parser.pos_to_linecol(e.position)))
......@@ -49,7 +49,7 @@ peg_grammar = r"""
/ ("(" ordered_choice ")") / literal;
identifier <- r'[a-zA-Z_]([a-zA-Z_]|[0-9])*';
regex <- 'r' '\'' r'(\\\'|[^\'])*' '\'';
regex <- 'r\'' r'(\\\'|[^\'])*' '\'';
literal <- r'\'(\\\'|[^\'])*\'|"[^"]*"';
LEFT_ARROW <- '<-';
SLASH <- '/';
......@@ -65,39 +65,34 @@ peg_grammar = r"""
"""
try:
# 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=True)
# Exporting parser model to dot file for visualization.
PMDOTExporter().exportFile(parser.parser_model,
"peg_peg_parser_model.dot")
# 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)
# 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)
# Again we export parse tree in dot file for vizualization.
PTDOTExporter().exportFile(parser.parse_tree,
"peg_peg_parse_tree.dot")
# Again we export parse tree in dot file for vizualization.
PTDOTExporter().exportFile(parser.parse_tree,
"peg_peg_parse_tree.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)
# ASG should be the same as parser.parser_model because semantic
# actions will create PEG parser (tree of ParsingExpressions).
asg = parser.getASG(sem_actions)
# 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")
# 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 we replace parser_mode with ASG constructed parser it will still
# parse PEG grammars
parser.parser_model = asg
parser.parse(peg_grammar)
except NoMatch, e:
print "Expected %s at position %s." % \
(e.value, str(e.parser.pos_to_linecol(e.position)))
......@@ -33,35 +33,35 @@ def right(): return 'right'
# Semantic actions
class Up(SemanticAction):
def first_pass(self, parser, node, nodes):
def first_pass(self, parser, node, children):
print "Going up"
return (0, 1)
class Down(SemanticAction):
def first_pass(self, parser, node, nodes):
def first_pass(self, parser, node, children):
print "Going down"
return (0, -1)
class Left(SemanticAction):
def first_pass(self, parser, node, nodes):
def first_pass(self, parser, node, children):
print "Going left"
return (-1, 0)
class Right(SemanticAction):
def first_pass(self, parser, node, nodes):
def first_pass(self, parser, node, children):
print "Going right"
return (1, 0)
class Command(SemanticAction):
def first_pass(self, parser, node, nodes):
def first_pass(self, parser, node, children):
print "Command"
return nodes[0]
class Program(SemanticAction):
def first_pass(self, parser, node, nodes):
def first_pass(self, parser, node, children):
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]), children[1:-2])
# Connecting rules with semantic actions
program.sem = Program()
......@@ -72,44 +72,41 @@ left.sem = Left()
right.sem = Right()
if __name__ == "__main__":
try:
# Program code
input = '''
begin
up
up
left
down
right
end
'''
# 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)
# 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)
# 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()
except NoMatch, e:
print "Expected %s at position %s." % (e.value, str(e.parser.pos_to_linecol(e.position)))
# Program code
input = '''
begin
up
up
left
down
right
end
'''
# 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)
# 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)
# 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()
......@@ -46,44 +46,41 @@ semantic_actions = {
if __name__ == "__main__":
try:
# Program code
input = '''
begin
up
up
left
down
right
end
'''
# Program code
input = '''
begin
up
up
left
down
right
end
'''
# 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)
# 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)
# 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")
# 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)
# 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")
# 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)
# 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)
except NoMatch, e:
print "Expected %s at position %s." % (e.value, str(e.parser.pos_to_linecol(e.position)))
......@@ -13,6 +13,7 @@ from arpeggio import *
from arpeggio.export import PMDOTExporter, PTDOTExporter
from arpeggio import RegExMatch as _
# Grammar
def comment(): return [_("//.*"), _("/\*.*\*/")]
def literal(): return _(r'\d*\.\d*|\d+|".*?"')
def symbol(): return _(r"\w+")
......@@ -29,36 +30,31 @@ def functioncall(): return symbol, "(", expressionlist, ")"
def function(): return Kwd("function"), symbol, parameterlist, block
def simpleLanguage(): return function
try:
# Parser instantiation. simpleLanguage is the definition of the root rule
# and comment is a grammar rule for comments.
parser = ParserPython(simpleLanguage, comment, debug=True)
# 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")
# 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")
# 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) {
// For 0! result is 0
return 0;
} else { /* And for n>0 result is calculated recursively */
return n * fak(n - 1);
};
}
"""
parse_tree = parser.parse(input)
input = """
function fak(n) {
if (n==0) {
// For 0! result is 0
return 0;
} else { /* And for n>0 result is calculated recursively */
return n * fak(n - 1);
};
}
"""
parse_tree = parser.parse(input)
PTDOTExporter().exportFile(parse_tree,
"simple_parse_tree.dot")
# Export parse tree for visualization
PTDOTExporter().exportFile(parse_tree, "simple_parse_tree.dot")
except NoMatch, e:
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