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
2d654c9a
Commit
2d654c9a
authored
Feb 18, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Examples update.
parent
0a3d2c28
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
110 additions
and
144 deletions
+110
-144
bibtex.py
examples/bibtex.py
+14
-16
calc.py
examples/calc.py
+29
-33
calc_peg.py
examples/calc_peg.py
+17
-22
csv.py
examples/csv.py
+2
-7
json.py
examples/json.py
+4
-7
peg_peg.py
examples/peg_peg.py
+22
-27
robot.py
examples/robot.py
+7
-10
robot_peg.py
examples/robot_peg.py
+0
-3
simple.py
examples/simple.py
+15
-19
No files found.
examples/bibtex.py
View file @
2d654c9a
...
@@ -42,12 +42,12 @@ class BibFileSem(SemanticAction):
...
@@ -42,12 +42,12 @@ class BibFileSem(SemanticAction):
"""
"""
Just returns list of child nodes (bibentries).
Just returns list of child nodes (bibentries).
"""
"""
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
if
parser
.
debug
:
print
"Processing Bibfile"
print
"Processing Bibfile"
# Return only dict nodes
# 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
):
class
BibEntrySem
(
SemanticAction
):
...
@@ -55,14 +55,14 @@ class BibEntrySem(SemanticAction):
...
@@ -55,14 +55,14 @@ class BibEntrySem(SemanticAction):
Constructs a map where key is bibentry field name.
Constructs a map where key is bibentry field name.
Key is returned under 'bibkey' key. Type is returned under 'bibtype'.
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
:
if
parser
.
debug
:
print
" Processing bibentry
%
s"
%
nodes
[
2
]
print
" Processing bibentry
%
s"
%
children
[
2
]
bib_entry_map
=
{
bib_entry_map
=
{
'bibtype'
:
nodes
[
0
]
.
value
,
'bibtype'
:
children
[
0
]
.
value
,
'bibkey'
:
nodes
[
2
]
.
value
'bibkey'
:
children
[
2
]
.
value
}
}
for
field
in
nodes
[
3
:]:
for
field
in
children
[
3
:]:
if
isinstance
(
field
,
tuple
):
if
isinstance
(
field
,
tuple
):
bib_entry_map
[
field
[
0
]]
=
field
[
1
]
bib_entry_map
[
field
[
0
]]
=
field
[
1
]
return
bib_entry_map
return
bib_entry_map
...
@@ -72,10 +72,10 @@ class FieldSem(SemanticAction):
...
@@ -72,10 +72,10 @@ class FieldSem(SemanticAction):
"""
"""
Constructs a tuple (fieldname, fieldvalue).
Constructs a tuple (fieldname, fieldvalue).
"""
"""
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
if
parser
.
debug
:
print
" Processing field
%
s"
%
nodes
[
0
]
print
" Processing field
%
s"
%
children
[
0
]
field
=
(
nodes
[
0
]
.
value
,
nodes
[
2
])
field
=
(
children
[
0
]
.
value
,
children
[
2
])
return
field
return
field
...
@@ -84,8 +84,8 @@ class FieldValueSem(SemanticAction):
...
@@ -84,8 +84,8 @@ class FieldValueSem(SemanticAction):
Serbian Serbian letters form latex encoding to Unicode.
Serbian Serbian letters form latex encoding to Unicode.
Remove braces. Remove newlines.
Remove braces. Remove newlines.
"""
"""
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
value
=
nodes
[
1
]
.
value
value
=
children
[
1
]
.
value
value
=
value
.
replace
(
r"\'{c}"
,
u"ć"
)
\
value
=
value
.
replace
(
r"\'{c}"
,
u"ć"
)
\
.
replace
(
r"\'{C}"
,
u"Ć"
)
\
.
replace
(
r"\'{C}"
,
u"Ć"
)
\
.
replace
(
r"\v{c}"
,
u"č"
)
\
.
replace
(
r"\v{c}"
,
u"č"
)
\
...
@@ -114,8 +114,7 @@ if __name__ == "__main__":
...
@@ -114,8 +114,7 @@ if __name__ == "__main__":
# 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
,
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"bib_parse_tree_model.dot"
)
"bib_parse_tree_model.dot"
)
# First parameter is bibtex file
# First parameter is bibtex file
if
len
(
sys
.
argv
)
>
1
:
if
len
(
sys
.
argv
)
>
1
:
...
@@ -127,8 +126,7 @@ if __name__ == "__main__":
...
@@ -127,8 +126,7 @@ if __name__ == "__main__":
parse_tree
=
parser
.
parse
(
bibtexfile_content
)
parse_tree
=
parser
.
parse
(
bibtexfile_content
)
# 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.
PTDOTExporter
()
.
exportFile
(
parse_tree
,
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"bib_parse_tree.dot"
)
"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.
...
...
examples/calc.py
View file @
2d654c9a
...
@@ -27,7 +27,7 @@ class ToFloat(SemanticAction):
...
@@ -27,7 +27,7 @@ class ToFloat(SemanticAction):
"""
"""
Converts node value to float.
Converts node value to float.
"""
"""
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
print
"Converting
%
s."
%
node
.
value
print
"Converting
%
s."
%
node
.
value
return
float
(
node
.
value
)
return
float
(
node
.
value
)
...
@@ -35,32 +35,32 @@ class Factor(SemanticAction):
...
@@ -35,32 +35,32 @@ 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
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
print
"Factor
%
s"
%
nodes
print
"Factor
%
s"
%
children
if
len
(
nodes
)
==
1
:
if
len
(
children
)
==
1
:
return
nodes
[
0
]
return
children
[
0
]
sign
=
-
1
if
nodes
[
0
]
==
'-'
else
1
sign
=
-
1
if
children
[
0
]
==
'-'
else
1
next
=
0
next
=
0
if
nodes
[
0
]
in
[
'+'
,
'-'
]:
if
children
[
0
]
in
[
'+'
,
'-'
]:
next
=
1
next
=
1
if
nodes
[
next
]
==
'('
:
if
children
[
next
]
==
'('
:
return
sign
*
nodes
[
next
+
1
]
return
sign
*
children
[
next
+
1
]
else
:
else
:
return
sign
*
nodes
[
next
]
return
sign
*
children
[
next
]
class
Term
(
SemanticAction
):
class
Term
(
SemanticAction
):
"""
"""
Divides or multiplies factors.
Divides or multiplies factors.
Factor nodes will be already evaluated.
Factor nodes will be already evaluated.
"""
"""
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
print
"Term
%
s"
%
nodes
print
"Term
%
s"
%
children
term
=
nodes
[
0
]
term
=
children
[
0
]
for
i
in
range
(
2
,
len
(
nodes
),
2
):
for
i
in
range
(
2
,
len
(
children
),
2
):
if
nodes
[
i
-
1
]
==
"*"
:
if
children
[
i
-
1
]
==
"*"
:
term
*=
nodes
[
i
]
term
*=
children
[
i
]
else
:
else
:
term
/=
nodes
[
i
]
term
/=
children
[
i
]
print
"Term =
%
f"
%
term
print
"Term =
%
f"
%
term
return
term
return
term
...
@@ -69,26 +69,26 @@ class Expr(SemanticAction):
...
@@ -69,26 +69,26 @@ class Expr(SemanticAction):
Adds or substracts terms.
Adds or substracts terms.
Term nodes will be already evaluated.
Term nodes will be already evaluated.
"""
"""
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
print
"Expression
%
s"
%
nodes
print
"Expression
%
s"
%
children
expr
=
0
expr
=
0
start
=
0
start
=
0
# Check for unary + or - operator
# Check for unary + or - operator
if
str
(
nodes
[
0
])
in
"+-"
:
if
str
(
children
[
0
])
in
"+-"
:
start
=
1
start
=
1
for
i
in
range
(
start
,
len
(
nodes
),
2
):
for
i
in
range
(
start
,
len
(
children
),
2
):
if
i
and
nodes
[
i
-
1
]
==
"-"
:
if
i
and
children
[
i
-
1
]
==
"-"
:
expr
-=
nodes
[
i
]
expr
-=
children
[
i
]
else
:
else
:
expr
+=
nodes
[
i
]
expr
+=
children
[
i
]
print
"Expression =
%
f"
%
expr
print
"Expression =
%
f"
%
expr
return
expr
return
expr
class
Calc
(
SemanticAction
):
class
Calc
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
return
nodes
[
0
]
return
children
[
0
]
# Connecting rules with semantic actions
# Connecting rules with semantic actions
number
.
sem
=
ToFloat
()
number
.
sem
=
ToFloat
()
...
@@ -98,7 +98,7 @@ expression.sem = Expr()
...
@@ -98,7 +98,7 @@ expression.sem = Expr()
calc
.
sem
=
Calc
()
calc
.
sem
=
Calc
()
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
try
:
# 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.
...
@@ -108,8 +108,7 @@ if __name__ == "__main__":
...
@@ -108,8 +108,7 @@ if __name__ == "__main__":
# 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
# dot -O -Tpng calc_parse_tree_model.dot
# dot -O -Tpng calc_parse_tree_model.dot
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"calc_parse_tree_model.dot"
)
"calc_parse_tree_model.dot"
)
# An expression we want to evaluate
# An expression we want to evaluate
input
=
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
input
=
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
...
@@ -119,13 +118,10 @@ if __name__ == "__main__":
...
@@ -119,13 +118,10 @@ if __name__ == "__main__":
# 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
,
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"calc_parse_tree.dot"
)
"calc_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 result of the input expression.
# returned value will be the result of the input expression.
print
"
%
s =
%
f"
%
(
input
,
parser
.
getASG
())
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
)))
examples/calc_peg.py
View file @
2d654c9a
...
@@ -39,33 +39,28 @@ sem_actions = {
...
@@ -39,33 +39,28 @@ sem_actions = {
"calc"
:
Calc
()
"calc"
:
Calc
()
}
}
try
:
# 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
=
True
)
# Then we export it to a dot file.
# Then we export it to a dot file.
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"calc_peg_parser_model.dot"
)
"calc_peg_parser_model.dot"
)
# An expression we want to evaluate
# An expression we want to evaluate
input
=
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
input
=
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
# Then parse tree is created out of the input expression.
# Then parse tree is created out of the input expression.
parse_tree
=
parser
.
parse
(
input
)
parse_tree
=
parser
.
parse
(
input
)
# We save it to dot file in order to visualise it.
# We save it to dot file in order to visualise it.
PTDOTExporter
()
.
exportFile
(
parse_tree
,
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"calc_peg_parse_tree.dot"
)
"calc_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 evaluated result of the input expression.
# returned value will be evaluated result of the input expression.
# Semantic actions are supplied to the getASG function.
# Semantic actions are supplied to the getASG function.
print
"
%
s =
%
f"
%
(
input
,
parser
.
getASG
(
sem_actions
))
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
)))
examples/csv.py
View file @
2d654c9a
...
@@ -26,7 +26,6 @@ Unquoted test 2, "Quoted test with ""inner"" quotes", 23234, One Two Three, "343
...
@@ -26,7 +26,6 @@ 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"
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.
# 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.
...
@@ -38,8 +37,7 @@ Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45"
...
@@ -38,8 +37,7 @@ Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45"
# 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:
# dot -O -Tpng calc_parse_tree_model.dot
# dot -O -Tpng calc_parse_tree_model.dot
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"csv_parse_tree_model.dot"
)
"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
)
...
@@ -47,8 +45,5 @@ Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45"
...
@@ -47,8 +45,5 @@ Unquoted test 3, "Quoted test 3", 23234, One Two Three, "343456.45"
# 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
,
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"csv_parse_tree.dot"
)
"csv_parse_tree.dot"
)
except
NoMatch
,
e
:
print
"Expected
%
s at position
%
s."
%
(
e
.
value
,
str
(
e
.
parser
.
pos_to_linecol
(
e
.
position
)))
examples/json.py
View file @
2d654c9a
...
@@ -81,19 +81,16 @@ if __name__ == "__main__":
...
@@ -81,19 +81,16 @@ if __name__ == "__main__":
}
}
}
}
"""
"""
try
:
# Creating parser from parser model.
# Creating parser from parser model.
parser
=
ParserPython
(
jsonFile
,
debug
=
True
)
parser
=
ParserPython
(
jsonFile
,
debug
=
True
)
# 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
,
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"json_parser_model.dot"
)
"json_parser_model.dot"
)
# Parse json string
parse_tree
=
parser
.
parse
(
testdata
)
parse_tree
=
parser
.
parse
(
testdata
)
PTDOTExporter
()
.
exportFile
(
parser
.
parse_tree
,
# Export parse tree for visualization
"json_parse_tree.dot"
)
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
)))
examples/peg_peg.py
View file @
2d654c9a
...
@@ -49,7 +49,7 @@ peg_grammar = r"""
...
@@ -49,7 +49,7 @@ peg_grammar = r"""
/ ("(" ordered_choice ")") / literal;
/ ("(" ordered_choice ")") / literal;
identifier <- r'[a-zA-Z_]([a-zA-Z_]|[0-9])*';
identifier <- r'[a-zA-Z_]([a-zA-Z_]|[0-9])*';
regex <- 'r
' '
\'' r'(\\\'|[^\'])*' '\'';
regex <- 'r\'' r'(\\\'|[^\'])*' '\'';
literal <- r'\'(\\\'|[^\'])*\'|"[^"]*"';
literal <- r'\'(\\\'|[^\'])*\'|"[^"]*"';
LEFT_ARROW <- '<-';
LEFT_ARROW <- '<-';
SLASH <- '/';
SLASH <- '/';
...
@@ -65,39 +65,34 @@ peg_grammar = r"""
...
@@ -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
# Exporting parser model to dot file for visualization.
# create parser_model for parsing PEG based grammars
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
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"
)
"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 vizualization.
# Again we export parse tree in dot file for vizualization.
PTDOTExporter
()
.
exportFile
(
parser
.
parse_tree
,
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
# This graph should be the same as peg_peg_parser_model.dot because
# they define the same parser.
# they define the same parser.
PMDOTExporter
()
.
exportFile
(
asg
,
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
)
except
NoMatch
,
e
:
print
"Expected
%
s at position
%
s."
%
\
(
e
.
value
,
str
(
e
.
parser
.
pos_to_linecol
(
e
.
position
)))
examples/robot.py
View file @
2d654c9a
...
@@ -33,35 +33,35 @@ def right(): return 'right'
...
@@ -33,35 +33,35 @@ def right(): return 'right'
# Semantic actions
# Semantic actions
class
Up
(
SemanticAction
):
class
Up
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
print
"Going up"
print
"Going up"
return
(
0
,
1
)
return
(
0
,
1
)
class
Down
(
SemanticAction
):
class
Down
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
print
"Going down"
print
"Going down"
return
(
0
,
-
1
)
return
(
0
,
-
1
)
class
Left
(
SemanticAction
):
class
Left
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
print
"Going left"
print
"Going left"
return
(
-
1
,
0
)
return
(
-
1
,
0
)
class
Right
(
SemanticAction
):
class
Right
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
print
"Going right"
print
"Going right"
return
(
1
,
0
)
return
(
1
,
0
)
class
Command
(
SemanticAction
):
class
Command
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
print
"Command"
print
"Command"
return
nodes
[
0
]
return
nodes
[
0
]
class
Program
(
SemanticAction
):
class
Program
(
SemanticAction
):
def
first_pass
(
self
,
parser
,
node
,
nodes
):
def
first_pass
(
self
,
parser
,
node
,
children
):
print
"Evaluating position"
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
# Connecting rules with semantic actions
program
.
sem
=
Program
()
program
.
sem
=
Program
()
...
@@ -72,7 +72,6 @@ left.sem = Left()
...
@@ -72,7 +72,6 @@ left.sem = Left()
right
.
sem
=
Right
()
right
.
sem
=
Right
()
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
try
:
# Program code
# Program code
input
=
'''
input
=
'''
...
@@ -111,5 +110,3 @@ if __name__ == "__main__":
...
@@ -111,5 +110,3 @@ if __name__ == "__main__":
# returned value will be the final position of the robot.
# returned value will be the final position of the robot.
print
"position = "
,
parser
.
getASG
()
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 @
2d654c9a
...
@@ -46,7 +46,6 @@ semantic_actions = {
...
@@ -46,7 +46,6 @@ semantic_actions = {
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
try
:
# Program code
# Program code
input
=
'''
input
=
'''
...
@@ -85,5 +84,3 @@ if __name__ == "__main__":
...
@@ -85,5 +84,3 @@ if __name__ == "__main__":
# 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
)
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 @
2d654c9a
...
@@ -13,6 +13,7 @@ from arpeggio import *
...
@@ -13,6 +13,7 @@ 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
_
# Grammar
def
comment
():
return
[
_
(
"//.*"
),
_
(
"/
\
*.*
\
*/"
)]
def
comment
():
return
[
_
(
"//.*"
),
_
(
"/
\
*.*
\
*/"
)]
def
literal
():
return
_
(
r'\d*\.\d*|\d+|".*?"'
)
def
literal
():
return
_
(
r'\d*\.\d*|\d+|".*?"'
)
def
symbol
():
return
_
(
r"\w+"
)
def
symbol
():
return
_
(
r"\w+"
)
...
@@ -29,23 +30,20 @@ def functioncall(): return symbol, "(", expressionlist, ")"
...
@@ -29,23 +30,20 @@ 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
try
:
# Parser instantiation. simpleLanguage is the definition of the root rule
# Parser instantiation. simpleLanguage is the definition of the root rule
# and comment is a grammar rule for comments.
# and comment is a grammar rule for comments.
parser
=
ParserPython
(
simpleLanguage
,
comment
,
debug
=
True
)
parser
=
ParserPython
(
simpleLanguage
,
comment
,
debug
=
True
)
# We save parser model to dot file in order to visualise it.
# 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
# We can make a png out of it using dot (part of graphviz) like this
# dot -Tpng -O simple_parser.dot
# dot -Tpng -O simple_parser.dot
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"simple_parser_model.dot"
)
"simple_parser_model.dot"
)
# Parser model for comments is handled as separate model
# Parser model for comments is handled as separate model
PMDOTExporter
()
.
exportFile
(
parser
.
comments_model
,
PMDOTExporter
()
.
exportFile
(
parser
.
comments_model
,
"simple_parser_comments.dot"
)
"simple_parser_comments.dot"
)
input
=
"""
input
=
"""
function fak(n) {
function fak(n) {
if (n==0) {
if (n==0) {
// For 0! result is 0
// For 0! result is 0
...
@@ -54,11 +52,9 @@ try:
...
@@ -54,11 +52,9 @@ try:
return n * fak(n - 1);
return n * fak(n - 1);
};
};
}
}
"""
"""
parse_tree
=
parser
.
parse
(
input
)
parse_tree
=
parser
.
parse
(
input
)
PTDOTExporter
()
.
exportFile
(
parse_tree
,
# Export parse tree for visualization
"simple_parse_tree.dot"
)
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
)))
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