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
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
backend
arpeggio-gm
Commits
7eb2e080
Commit
7eb2e080
authored
Jan 24, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removed logging module from examples.
parent
7eef4197
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
53 additions
and
63 deletions
+53
-63
json.py
examples/json.py
+21
-23
peg_peg.py
examples/peg_peg.py
+1
-3
robot.py
examples/robot.py
+20
-22
robot_peg.py
examples/robot_peg.py
+5
-7
simple.py
examples/simple.py
+6
-8
No files found.
examples/json.py
View file @
7eb2e080
...
...
@@ -4,26 +4,26 @@
# Author: Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2009 Igor R. Dejanovic <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#
# This example is based on jsonParser.py from pyparsing project
#
# This example is based on jsonParser.py from pyparsing project
# (see http://pyparsing.wikispaces.com/).
##############################################################################
json_bnf
=
"""
object
{ members }
{}
members
string : value
members , string : value
array
object
{ members }
{}
members
string : value
members , string : value
array
[ elements ]
[]
elements
value
elements , value
value
[]
elements
value
elements , value
value
string
number
object
...
...
@@ -51,15 +51,15 @@ def jsonMembers(): return memberDef, ZeroOrMore(",", memberDef)
def
jsonObject
():
return
"{"
,
Optional
(
jsonMembers
),
"}"
def
jsonFile
():
return
jsonObject
,
EOF
if
__name__
==
"__main__"
:
testdata
=
"""
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"GlossDiv": {
"title": "S",
"GlossList":
"GlossList":
{
"ID": "SGML",
"SortAs": "SGML",
...
...
@@ -83,15 +83,13 @@ if __name__ == "__main__":
}
"""
try
:
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
# Creating parser from parser model.
parser
=
ParserPython
(
jsonFile
)
parser
=
ParserPython
(
jsonFile
,
debug
=
True
)
# Exporting parser model to dot file in order to visualise it.
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
"json_parser_model.dot"
)
parse_tree
=
parser
.
parse
(
testdata
)
PTDOTExport
()
.
exportFile
(
parser
.
parse_tree
,
...
...
@@ -99,4 +97,4 @@ if __name__ == "__main__":
except
NoMatch
,
e
:
print
"Expected
%
s at position
%
s."
%
(
e
.
value
,
str
(
e
.
parser
.
pos_to_linecol
(
e
.
position
)))
examples/peg_peg.py
View file @
7eb2e080
...
...
@@ -13,7 +13,6 @@
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio.peg
import
ParserPEG
import
logging
# Semantic actions
from
arpeggio.peg
import
SemGrammar
,
SemRule
,
SemOrderedChoice
,
SemSequence
,
\
...
...
@@ -67,11 +66,10 @@ peg_grammar = r"""
try
:
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
# ParserPEG will use ParserPython to parse peg_grammar definition and
# create parser_model for parsing PEG based grammars
parser
=
ParserPEG
(
peg_grammar
,
'grammar'
)
parser
=
ParserPEG
(
peg_grammar
,
'grammar'
,
debug
=
True
)
# Exporting parser model to dot file in order to visualise.
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
...
...
examples/robot.py
View file @
7eb2e080
...
...
@@ -22,10 +22,9 @@
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio
import
RegExMatch
as
_
import
logging
# Grammar rules
def
program
():
return
Kwd
(
'begin'
),
ZeroOrMore
(
command
),
Kwd
(
'end'
),
EndOfFile
def
program
():
return
Kwd
(
'begin'
),
ZeroOrMore
(
command
),
Kwd
(
'end'
),
EndOfFile
def
command
():
return
[
up
,
down
,
left
,
right
]
def
up
():
return
'up'
def
down
():
return
'down'
...
...
@@ -36,35 +35,35 @@ def right(): return 'right'
# Semantic actions
class
Up
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
logging
.
debug
(
"Going up"
)
return
(
0
,
1
)
print
"Going up"
return
(
0
,
1
)
class
Down
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
logging
.
debug
(
"Going down"
)
return
(
0
,
-
1
)
print
"Going down"
return
(
0
,
-
1
)
class
Left
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
logging
.
debug
(
"Going left"
)
return
(
-
1
,
0
)
print
"Going left"
return
(
-
1
,
0
)
class
Right
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
logging
.
debug
(
"Going right"
)
return
(
1
,
0
)
print
"Going right"
return
(
1
,
0
)
class
Command
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
logging
.
debug
(
"Command"
)
return
nodes
[
0
]
print
"Command"
return
nodes
[
0
]
class
Program
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
logging
.
debug
(
"Evaluating position"
)
print
"Evaluating position"
return
reduce
(
lambda
x
,
y
:
(
x
[
0
]
+
y
[
0
],
x
[
1
]
+
y
[
1
]),
nodes
[
1
:
-
2
])
# Connecting rules with semantic actions
program
.
sem
=
Program
()
command
.
sem
=
Command
()
...
...
@@ -75,7 +74,6 @@ right.sem = Right()
if
__name__
==
"__main__"
:
try
:
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
# Program code
input
=
'''
...
...
@@ -90,9 +88,9 @@ 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
# Parser model is given in the form of python constructs therefore we
# are using ParserPython class.
parser
=
ParserPython
(
program
)
parser
=
ParserPython
(
program
,
debug
=
True
)
# Then we export it to a dot file in order to visualize it. This is
# particularly handy for debugging purposes.
...
...
@@ -100,10 +98,10 @@ if __name__ == "__main__":
# dot -O -Tjpg robot_parse_tree_model.dot
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
"robot_parse_tree_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 -Tjpg robot_parse_tree.dot
PTDOTExport
()
.
exportFile
(
parse_tree
,
...
...
@@ -113,6 +111,6 @@ if __name__ == "__main__":
# 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
)))
examples/robot_peg.py
View file @
7eb2e080
...
...
@@ -22,7 +22,6 @@
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio
import
RegExMatch
as
_
import
logging
from
arpeggio.peg
import
ParserPEG
# Grammar rules
...
...
@@ -49,7 +48,6 @@ semantic_actions = {
if
__name__
==
"__main__"
:
try
:
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
# Program code
input
=
'''
...
...
@@ -64,9 +62,9 @@ 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
# Parser model is given in the form of PEG specification therefore we
# are using ParserPEG class.
parser
=
ParserPEG
(
robot_grammar
,
'program'
)
parser
=
ParserPEG
(
robot_grammar
,
'program'
,
debug
=
True
)
# Then we export it to a dot file in order to visualize it. This is
# particularly handy for debugging purposes.
...
...
@@ -74,10 +72,10 @@ if __name__ == "__main__":
# dot -O -Tjpg robot_peg_parse_tree_model.dot
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
"robot_peg_parse_tree_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 -Tjpg robot_peg_parse_tree.dot
PTDOTExport
()
.
exportFile
(
parse_tree
,
...
...
@@ -87,6 +85,6 @@ if __name__ == "__main__":
# 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
)))
examples/simple.py
View file @
7eb2e080
...
...
@@ -12,7 +12,6 @@
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio
import
RegExMatch
as
_
import
logging
def
comment
():
return
[
_
(
"//.*"
),
_
(
"/
\
*.*
\
*/"
)]
def
literal
():
return
_
(
r'\d*\.\d*|\d+|".*?"'
)
...
...
@@ -31,22 +30,21 @@ def function(): return Kwd("function"), symbol, parameterlist, block
def
simpleLanguage
():
return
function
try
:
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
# Parser instantiation. simpleLanguage is root definition and comment is
# grammar rule for comments.
parser
=
ParserPython
(
simpleLanguage
,
comment
)
parser
=
ParserPython
(
simpleLanguage
,
comment
,
debug
=
True
)
# We save parser model to dot file in order to visualise it.
# We can make a jpg out of it using dot (part of graphviz) like this
# dot -Tjpg -O simple_parser.dot
PMDOTExport
()
.
exportFile
(
parser
.
parser_model
,
"simple_parser_model.dot"
)
# Parser model for comments is handled as separate model
PMDOTExport
()
.
exportFile
(
parser
.
comments_model
,
"simple_parser_comments.dot"
)
input
=
"""
function fak(n) {
if (n==0) {
...
...
@@ -58,9 +56,9 @@ try:
}
"""
parse_tree
=
parser
.
parse
(
input
)
PTDOTExport
()
.
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
)))
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