Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
A
arpeggio-gm
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
2
Merge Requests
2
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
郭家华
arpeggio-gm
Commits
3f9714aa
Commit
3f9714aa
authored
Aug 07, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
DOT export integrated in parsers
If parser runs in debug mode dot files will be generated.
parent
ac6afe41
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
41 additions
and
130 deletions
+41
-130
__init__.py
arpeggio/__init__.py
+16
-0
peg.py
arpeggio/peg.py
+11
-3
bibtex.py
examples/bibtex.py
+2
-14
calc.py
examples/calc.py
+0
-14
calc_peg.py
examples/calc_peg.py
+0
-9
csv.py
examples/csv.py
+0
-14
json.py
examples/json.py
+0
-9
peg_peg.py
examples/peg_peg.py
+3
-14
robot.py
examples/robot.py
+5
-20
robot_peg.py
examples/robot_peg.py
+4
-19
simple.py
examples/simple.py
+0
-14
No files found.
arpeggio/__init__.py
View file @
3f9714aa
...
@@ -899,6 +899,14 @@ class Parser(object):
...
@@ -899,6 +899,14 @@ class Parser(object):
self
.
input
=
_input
self
.
input
=
_input
self
.
parser_model
.
clear_cache
()
self
.
parser_model
.
clear_cache
()
self
.
parse_tree
=
self
.
_parse
()
self
.
parse_tree
=
self
.
_parse
()
# In debug mode export parse tree to dot file for
# visualization
if
self
.
debug
:
from
arpeggio.export
import
PTDOTExporter
root_rule
=
self
.
parse_tree
.
rule
PTDOTExporter
()
.
exportFile
(
self
.
parse_tree
,
"{}_parse_tree.dot"
.
format
(
root_rule
))
return
self
.
parse_tree
return
self
.
parse_tree
def
getASG
(
self
,
sem_actions
=
None
,
defaults
=
True
):
def
getASG
(
self
,
sem_actions
=
None
,
defaults
=
True
):
...
@@ -1094,6 +1102,14 @@ class ParserPython(Parser):
...
@@ -1094,6 +1102,14 @@ class ParserPython(Parser):
self
.
comments_model
=
self
.
_from_python
(
comment_def
)
\
self
.
comments_model
=
self
.
_from_python
(
comment_def
)
\
if
comment_def
else
None
if
comment_def
else
None
# In debug mode export parser model to dot for
# visualization
if
self
.
debug
:
from
arpeggio.export
import
PMDOTExporter
root_rule
=
language_def
.
__name__
PMDOTExporter
()
.
exportFile
(
self
.
parser_model
,
"{}_parser_model.dot"
.
format
(
root_rule
))
# Comments should be optional and there can be more of them
# Comments should be optional and there can be more of them
if
self
.
comments_model
:
# and not isinstance(self.comments_model, ZeroOrMore):
if
self
.
comments_model
:
# and not isinstance(self.comments_model, ZeroOrMore):
self
.
comments_model
.
root
=
True
self
.
comments_model
.
root
=
True
...
...
arpeggio/peg.py
View file @
3f9714aa
...
@@ -34,7 +34,7 @@ def str_match(): return _(r'(\'(\\\'|[^\'])*\')|("[^"]*")')
...
@@ -34,7 +34,7 @@ def str_match(): return _(r'(\'(\\\'|[^\'])*\')|("[^"]*")')
def
comment
():
return
"//"
,
_
(
".*
\n
"
)
def
comment
():
return
"//"
,
_
(
".*
\n
"
)
# PEG syntax rules
# PEG syntax rules
def
grammar
():
return
OneOrMore
(
rule
),
EOF
def
peggrammar
():
return
OneOrMore
(
rule
),
EOF
def
rule
():
return
rule_name
,
LEFT_ARROW
,
ordered_choice
,
";"
def
rule
():
return
rule_name
,
LEFT_ARROW
,
ordered_choice
,
";"
def
ordered_choice
():
return
sequence
,
ZeroOrMore
(
SLASH
,
sequence
)
def
ordered_choice
():
return
sequence
,
ZeroOrMore
(
SLASH
,
sequence
)
def
sequence
():
return
OneOrMore
(
prefix
)
def
sequence
():
return
OneOrMore
(
prefix
)
...
@@ -205,7 +205,7 @@ class SemStrMatch(SemanticAction):
...
@@ -205,7 +205,7 @@ class SemStrMatch(SemanticAction):
return
StrMatch
(
match_str
,
ignore_case
=
parser
.
ignore_case
)
return
StrMatch
(
match_str
,
ignore_case
=
parser
.
ignore_case
)
grammar
.
sem
=
SemGrammar
()
peg
grammar
.
sem
=
SemGrammar
()
rule
.
sem
=
SemRule
()
rule
.
sem
=
SemRule
()
ordered_choice
.
sem
=
SemOrderedChoice
()
ordered_choice
.
sem
=
SemOrderedChoice
()
sequence
.
sem
=
SemSequence
()
sequence
.
sem
=
SemSequence
()
...
@@ -226,6 +226,14 @@ class ParserPEG(Parser):
...
@@ -226,6 +226,14 @@ class ParserPEG(Parser):
# PEG Abstract Syntax Graph
# PEG Abstract Syntax Graph
self
.
parser_model
=
self
.
_from_peg
(
language_def
)
self
.
parser_model
=
self
.
_from_peg
(
language_def
)
# In debug mode export parser model to dot for
# visualization
if
self
.
debug
:
from
arpeggio.export
import
PMDOTExporter
root_rule
=
self
.
parser_model
.
rule
PMDOTExporter
()
.
exportFile
(
self
.
parser_model
,
"{}_peg_parser_model.dot"
.
format
(
root_rule
))
# Comments should be optional and there can be more of them
# Comments should be optional and there can be more of them
if
self
.
comments_model
:
# and not isinstance(self.comments_model, ZeroOrMore):
if
self
.
comments_model
:
# and not isinstance(self.comments_model, ZeroOrMore):
self
.
comments_model
.
root
=
True
self
.
comments_model
.
root
=
True
...
@@ -235,7 +243,7 @@ class ParserPEG(Parser):
...
@@ -235,7 +243,7 @@ class ParserPEG(Parser):
return
self
.
parser_model
.
parse
(
self
)
return
self
.
parser_model
.
parse
(
self
)
def
_from_peg
(
self
,
language_def
):
def
_from_peg
(
self
,
language_def
):
parser
=
ParserPython
(
grammar
,
comment
,
reduce_tree
=
False
)
parser
=
ParserPython
(
peggrammar
,
comment
,
reduce_tree
=
False
,
debug
=
self
.
debug
)
parser
.
root_rule_name
=
self
.
root_rule_name
parser
.
root_rule_name
=
self
.
root_rule_name
parser
.
parse
(
language_def
)
parser
.
parse
(
language_def
)
...
...
examples/bibtex.py
View file @
3f9714aa
...
@@ -13,7 +13,6 @@ from __future__ import print_function
...
@@ -13,7 +13,6 @@ from __future__ import print_function
import
pprint
import
pprint
import
sys
,
os
import
sys
,
os
from
arpeggio
import
*
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
from
arpeggio
import
RegExMatch
as
_
...
@@ -59,7 +58,7 @@ class BibEntrySem(SemanticAction):
...
@@ -59,7 +58,7 @@ class BibEntrySem(SemanticAction):
"""
"""
def
first_pass
(
self
,
parser
,
node
,
children
):
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
if
parser
.
debug
:
print
(
" Processing bibentry
%
s"
%
children
[
2
])
print
(
" Processing bibentry
%
s"
%
children
[
1
])
bib_entry_map
=
{
bib_entry_map
=
{
'bibtype'
:
children
[
0
],
'bibtype'
:
children
[
0
],
'bibkey'
:
children
[
1
]
'bibkey'
:
children
[
1
]
...
@@ -109,14 +108,7 @@ def main(debug=False, file_name=None):
...
@@ -109,14 +108,7 @@ 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
,
debug
=
debug
)
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"
)
if
not
file_name
:
if
not
file_name
:
file_name
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'bibtex_example.bib'
)
file_name
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'bibtex_example.bib'
)
...
@@ -128,10 +120,6 @@ def main(debug=False, file_name=None):
...
@@ -128,10 +120,6 @@ def main(debug=False, file_name=None):
# 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.
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"bib_parse_tree.dot"
)
# getASG will start semantic analysis.
# getASG will start semantic analysis.
# 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
()
...
...
examples/calc.py
View file @
3f9714aa
...
@@ -12,7 +12,6 @@
...
@@ -12,7 +12,6 @@
from
arpeggio
import
Optional
,
ZeroOrMore
,
OneOrMore
,
EOF
,
SemanticAction
,
\
from
arpeggio
import
Optional
,
ZeroOrMore
,
OneOrMore
,
EOF
,
SemanticAction
,
\
ParserPython
ParserPython
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
from
arpeggio
import
RegExMatch
as
_
def
number
():
return
_
(
r'\d*\.\d*|\d+'
)
def
number
():
return
_
(
r'\d*\.\d*|\d+'
)
...
@@ -107,25 +106,12 @@ def main(debug=False):
...
@@ -107,25 +106,12 @@ def main(debug=False):
# are using ParserPython class.
# are using ParserPython class.
parser
=
ParserPython
(
calc
,
debug
=
debug
)
parser
=
ParserPython
(
calc
,
debug
=
debug
)
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
# An expression we want to evaluate
input_expr
=
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
input_expr
=
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# 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.
# This is also optional.
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"calc_parse_tree.dot"
)
result
=
parser
.
getASG
()
result
=
parser
.
getASG
()
if
debug
:
if
debug
:
...
...
examples/calc_peg.py
View file @
3f9714aa
...
@@ -15,7 +15,6 @@
...
@@ -15,7 +15,6 @@
from
__future__
import
absolute_import
from
__future__
import
absolute_import
from
arpeggio.peg
import
ParserPEG
from
arpeggio.peg
import
ParserPEG
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
# Semantic actions
# Semantic actions
from
calc
import
ToFloat
,
Factor
,
Term
,
Expr
from
calc
import
ToFloat
,
Factor
,
Term
,
Expr
...
@@ -45,20 +44,12 @@ def main(debug=False):
...
@@ -45,20 +44,12 @@ def main(debug=False):
# 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
=
debug
)
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"
)
# An expression we want to evaluate
# An expression we want to evaluate
input_expr
=
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
input_expr
=
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# Then parse tree is created out of the input_expr expression.
# Then parse tree is created out of the input_expr expression.
parse_tree
=
parser
.
parse
(
input_expr
)
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"
)
result
=
parser
.
getASG
(
sem_actions
)
result
=
parser
.
getASG
(
sem_actions
)
if
debug
:
if
debug
:
...
...
examples/csv.py
View file @
3f9714aa
...
@@ -6,7 +6,6 @@
...
@@ -6,7 +6,6 @@
# License: MIT License
# License: MIT License
##############################################################################
##############################################################################
from
arpeggio
import
*
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
from
arpeggio
import
RegExMatch
as
_
def
record
():
return
field
,
ZeroOrMore
(
","
,
field
)
def
record
():
return
field
,
ZeroOrMore
(
","
,
field
)
...
@@ -31,22 +30,9 @@ def main(debug=False):
...
@@ -31,22 +30,9 @@ def main(debug=False):
# 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
=
debug
)
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.
# 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
# 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.
# This is also optional.
# dot -O -Tpng calc_parse_tree.dot
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"csv_parse_tree.dot"
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
main
(
debug
=
True
)
main
(
debug
=
True
)
examples/json.py
View file @
3f9714aa
...
@@ -34,7 +34,6 @@ value
...
@@ -34,7 +34,6 @@ value
"""
"""
from
arpeggio
import
*
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
from
arpeggio
import
RegExMatch
as
_
def
TRUE
():
return
"true"
def
TRUE
():
return
"true"
...
@@ -85,17 +84,9 @@ def main(debug=False):
...
@@ -85,17 +84,9 @@ def main(debug=False):
# Creating parser from parser model.
# Creating parser from parser model.
parser
=
ParserPython
(
jsonFile
,
debug
=
debug
)
parser
=
ParserPython
(
jsonFile
,
debug
=
debug
)
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 json string
parse_tree
=
parser
.
parse
(
testdata
)
parse_tree
=
parser
.
parse
(
testdata
)
if
debug
:
# Export parse tree for visualization
PTDOTExporter
()
.
exportFile
(
parser
.
parse_tree
,
"json_parse_tree.dot"
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
main
(
debug
=
True
)
main
(
debug
=
True
)
examples/peg_peg.py
View file @
3f9714aa
...
@@ -19,7 +19,7 @@ from arpeggio.peg import SemGrammar, SemRule, SemOrderedChoice, SemSequence,\
...
@@ -19,7 +19,7 @@ from arpeggio.peg import SemGrammar, SemRule, SemOrderedChoice, SemSequence,\
SemPrefix
,
SemSufix
,
SemExpression
,
SemRegEx
,
SemStrMatch
,
SemRuleCrossRef
SemPrefix
,
SemSufix
,
SemExpression
,
SemRegEx
,
SemStrMatch
,
SemRuleCrossRef
sem_actions
=
{
sem_actions
=
{
"grammar"
:
SemGrammar
(),
"
peg
grammar"
:
SemGrammar
(),
"rule"
:
SemRule
(),
"rule"
:
SemRule
(),
"ordered_choice"
:
SemOrderedChoice
(),
"ordered_choice"
:
SemOrderedChoice
(),
"sequence"
:
SemSequence
(),
"sequence"
:
SemSequence
(),
...
@@ -34,7 +34,7 @@ sem_actions = {
...
@@ -34,7 +34,7 @@ sem_actions = {
# PEG defined using PEG itself.
# PEG defined using PEG itself.
peg_grammar
=
r"""
peg_grammar
=
r"""
grammar <- rule+ EOF;
peg
grammar <- rule+ EOF;
rule <- rule_name LEFT_ARROW ordered_choice ';';
rule <- rule_name LEFT_ARROW ordered_choice ';';
ordered_choice <- sequence (SLASH sequence)*;
ordered_choice <- sequence (SLASH sequence)*;
sequence <- prefix+;
sequence <- prefix+;
...
@@ -64,24 +64,13 @@ def main(debug=False):
...
@@ -64,24 +64,13 @@ 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
=
debug
)
parser
=
ParserPEG
(
peg_grammar
,
'peggrammar'
,
debug
=
debug
)
if
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
# 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
)
if
debug
:
# Again we export parse tree in dot file for visualization.
PTDOTExporter
()
.
exportFile
(
parser
.
parse_tree
,
"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
)
...
...
examples/robot.py
View file @
3f9714aa
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
# Name: robot.py
# Name: robot.py
# Purpose: Simple DSL for defining robot movement.
# Purpose: Simple DSL for defining robot movement.
# Author: Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# Author: Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2011 Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2011
-2014
Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
# License: MIT License
#
#
# This example is inspired by an example from LISA tool (http://labraj.uni-mb.si/lisa/)
# This example is inspired by an example from LISA tool (http://labraj.uni-mb.si/lisa/)
...
@@ -21,10 +21,9 @@
...
@@ -21,10 +21,9 @@
from
__future__
import
print_function
from
__future__
import
print_function
from
arpeggio
import
*
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
# Grammar rules
# Grammar rules
def
program
():
return
Kwd
(
'begin'
),
ZeroOrMore
(
command
),
Kwd
(
'end'
),
EOF
def
robot
():
return
Kwd
(
'begin'
),
ZeroOrMore
(
command
),
Kwd
(
'end'
),
EOF
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'
...
@@ -68,7 +67,7 @@ class Command(SemanticAction):
...
@@ -68,7 +67,7 @@ class Command(SemanticAction):
return
children
[
0
]
return
children
[
0
]
class
Program
(
SemanticAction
):
class
Robot
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
children
):
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
if
parser
.
debug
:
print
(
"Evaluating position"
)
print
(
"Evaluating position"
)
...
@@ -79,7 +78,7 @@ class Program(SemanticAction):
...
@@ -79,7 +78,7 @@ class Program(SemanticAction):
return
position
return
position
# Connecting rules with semantic actions
# Connecting rules with semantic actions
program
.
sem
=
Program
()
robot
.
sem
=
Robot
()
command
.
sem
=
Command
()
command
.
sem
=
Command
()
up
.
sem
=
Up
()
up
.
sem
=
Up
()
down
.
sem
=
Down
()
down
.
sem
=
Down
()
...
@@ -101,25 +100,11 @@ def main(debug=False):
...
@@ -101,25 +100,11 @@ def main(debug=False):
# 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
=
debug
)
parser
=
ParserPython
(
robot
,
debug
=
debug
)
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
# 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.
# dot -O -Tpng robot_parse_tree.dot
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"robot_parse_tree.dot"
)
# 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.
...
...
examples/robot_peg.py
View file @
3f9714aa
...
@@ -21,12 +21,11 @@
...
@@ -21,12 +21,11 @@
from
__future__
import
print_function
from
__future__
import
print_function
from
arpeggio
import
*
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
from
arpeggio.peg
import
ParserPEG
from
arpeggio.peg
import
ParserPEG
# Grammar rules
# Grammar rules
robot_grammar
=
'''
robot_grammar
=
'''
program
<- 'begin' (command)* 'end' EOF;
robot
<- 'begin' (command)* 'end' EOF;
command <- UP/DOWN/LEFT/RIGHT;
command <- UP/DOWN/LEFT/RIGHT;
UP <- 'up';
UP <- 'up';
DOWN <- 'down';
DOWN <- 'down';
...
@@ -35,9 +34,9 @@ RIGHT <- 'right';
...
@@ -35,9 +34,9 @@ RIGHT <- 'right';
'''
'''
# Semantic actions
# Semantic actions
from
robot
import
Up
,
Down
,
Left
,
Right
,
Command
,
Program
from
robot
import
Up
,
Down
,
Left
,
Right
,
Command
,
Robot
semantic_actions
=
{
semantic_actions
=
{
'
program'
:
Program
(),
'
robot'
:
Robot
(),
'command'
:
Command
(),
'command'
:
Command
(),
'UP'
:
Up
(),
'UP'
:
Up
(),
'DOWN'
:
Down
(),
'DOWN'
:
Down
(),
...
@@ -62,25 +61,11 @@ def main(debug=False):
...
@@ -62,25 +61,11 @@ def main(debug=False):
# 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
=
debug
)
parser
=
ParserPEG
(
robot_grammar
,
'robot'
,
debug
=
debug
)
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
# 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.
# dot -O -Tpng robot_peg_parse_tree.dot
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"robot_peg_parse_tree.dot"
)
# 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.
...
...
examples/simple.py
View file @
3f9714aa
...
@@ -10,7 +10,6 @@
...
@@ -10,7 +10,6 @@
#######################################################################
#######################################################################
from
arpeggio
import
*
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
from
arpeggio
import
RegExMatch
as
_
# Grammar
# Grammar
...
@@ -47,21 +46,8 @@ def main(debug=False):
...
@@ -47,21 +46,8 @@ def main(debug=False):
# and comment is a grammar rule for comments.
# and comment is a grammar rule for comments.
parser
=
ParserPython
(
simpleLanguage
,
comment
,
debug
=
debug
)
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
)
parse_tree
=
parser
.
parse
(
input
)
if
debug
:
# Export parse tree for visualization
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"simple_parse_tree.dot"
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
main
(
debug
=
True
)
main
(
debug
=
True
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment