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
ff9ad67a
Commit
ff9ad67a
authored
Aug 07, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Semantic actions may now be Python callables.
parent
ddfb9d40
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
23 deletions
+17
-23
__init__.py
arpeggio/__init__.py
+3
-0
calc.py
examples/calc.py
+9
-18
calc_peg.py
examples/calc_peg.py
+5
-5
No files found.
arpeggio/__init__.py
View file @
ff9ad67a
...
...
@@ -951,6 +951,9 @@ class Parser(object):
if
node
.
rule
in
sem_actions
:
sem_action
=
sem_actions
[
node
.
rule
]
if
type
(
sem_action
)
is
types
.
FunctionType
:
retval
=
sem_action
(
self
,
node
,
children
)
else
:
retval
=
sem_action
.
first_pass
(
self
,
node
,
children
)
if
hasattr
(
sem_action
,
"second_pass"
):
...
...
examples/calc.py
View file @
ff9ad67a
...
...
@@ -23,38 +23,30 @@ def calc(): return OneOrMore(expression), EOF
# Semantic actions
class
ToFloat
(
SemanticActio
n
):
def
to_floatSA
(
parser
,
node
,
childre
n
):
"""
Converts node value to float.
"""
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Converting {}."
.
format
(
node
.
value
))
return
float
(
node
.
value
)
class
Factor
(
SemanticAction
):
def
factorSA
(
parser
,
node
,
children
):
"""
Removes parenthesis if exists and returns what was contained inside.
"""
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Factor {}"
.
format
(
children
))
if
len
(
children
)
==
1
:
return
children
[
0
]
sign
=
-
1
if
children
[
0
]
==
'-'
else
1
next_chd
=
0
if
children
[
0
]
in
[
'+'
,
'-'
]:
next_chd
=
1
return
sign
*
children
[
next_chd
]
return
sign
*
children
[
-
1
]
class
Term
(
SemanticActio
n
):
def
termSA
(
parser
,
node
,
childre
n
):
"""
Divides or multiplies factors.
Factor nodes will be already evaluated.
"""
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Term {}"
.
format
(
children
))
term
=
children
[
0
]
...
...
@@ -68,12 +60,11 @@ class Term(SemanticAction):
return
term
class
Expr
(
SemanticActio
n
):
def
exprSA
(
parser
,
node
,
childre
n
):
"""
Adds or substracts terms.
Term nodes will be already evaluated.
"""
def
first_pass
(
self
,
parser
,
node
,
children
):
if
parser
.
debug
:
print
(
"Expression {}"
.
format
(
children
))
expr
=
0
...
...
@@ -95,10 +86,10 @@ class Expr(SemanticAction):
# Connecting rules with semantic actions
number
.
sem
=
ToFloat
()
factor
.
sem
=
Factor
()
term
.
sem
=
Term
()
expression
.
sem
=
Expr
()
number
.
sem
=
to_floatSA
factor
.
sem
=
factorSA
term
.
sem
=
termSA
expression
.
sem
=
exprSA
def
main
(
debug
=
False
):
# First we will make a parser - an instance of the calc parser model.
...
...
examples/calc_peg.py
View file @
ff9ad67a
...
...
@@ -17,7 +17,7 @@ from __future__ import absolute_import
from
arpeggio.peg
import
ParserPEG
# Semantic actions
from
calc
import
ToFloat
,
Factor
,
Term
,
Expr
from
calc
import
to_floatSA
,
factorSA
,
termSA
,
exprSA
# Grammar is defined using textual specification based on PEG language.
calc_grammar
=
"""
...
...
@@ -31,10 +31,10 @@ calc_grammar = """
# Rules are mapped to semantic actions
sem_actions
=
{
"number"
:
ToFloat
()
,
"factor"
:
Factor
()
,
"term"
:
Term
()
,
"expression"
:
Expr
()
,
"number"
:
to_floatSA
,
"factor"
:
factorSA
,
"term"
:
termSA
,
"expression"
:
exprSA
,
}
def
main
(
debug
=
False
):
...
...
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