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
08653e36
Commit
08653e36
authored
Feb 02, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changed calc example grammar. Removed logging module from calc example.
parent
f594984b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
17 deletions
+23
-17
calc.py
examples/calc.py
+20
-15
calc_peg.py
examples/calc_peg.py
+3
-2
No files found.
examples/calc.py
View file @
08653e36
...
@@ -13,29 +13,36 @@
...
@@ -13,29 +13,36 @@
from
arpeggio
import
*
from
arpeggio
import
*
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio.export
import
PMDOTExport
,
PTDOTExport
from
arpeggio
import
RegExMatch
as
_
from
arpeggio
import
RegExMatch
as
_
import
logging
def
number
():
return
_
(
r'\d*\.\d*|\d+'
)
def
number
():
return
_
(
r'\d*\.\d*|\d+'
)
def
factor
():
return
[
number
,
(
"("
,
expression
,
")"
)]
def
factor
():
return
Optional
([
"+"
,
"-"
]),
[
number
,
(
"("
,
expression
,
")"
)]
def
term
():
return
factor
,
ZeroOrMore
([
"*"
,
"/"
],
factor
)
def
term
():
return
factor
,
ZeroOrMore
([
"*"
,
"/"
],
factor
)
def
expression
():
return
Optional
([
"+"
,
"-"
]),
term
,
ZeroOrMore
([
"+"
,
"-"
],
term
)
def
expression
():
return
term
,
ZeroOrMore
([
"+"
,
"-"
],
term
)
def
calc
():
return
expression
,
EndOfFile
def
calc
():
return
OneOrMore
(
expression
),
EndOfFile
# Semantic actions
# Semantic actions
class
ToFloat
(
SemanticAction
):
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
,
nodes
):
logging
.
debug
(
"Converting
%
s."
%
node
.
value
)
print
"Converting
%
s."
%
node
.
value
return
float
(
node
.
value
)
return
float
(
node
.
value
)
class
Factor
(
SemanticAction
):
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
,
nodes
):
logging
.
debug
(
"Factor
%
s"
%
nodes
)
print
"Factor
%
s"
%
nodes
if
nodes
[
0
]
==
"("
:
if
len
(
nodes
)
==
1
:
return
nodes
[
1
]
else
:
return
nodes
[
0
]
return
nodes
[
0
]
sign
=
-
1
if
nodes
[
0
]
==
'-'
else
1
next
=
0
if
nodes
[
0
]
in
[
'+'
,
'-'
]:
next
=
1
if
nodes
[
next
]
==
'('
:
return
sign
*
nodes
[
next
+
1
]
else
:
return
sign
*
nodes
[
next
]
class
Term
(
SemanticAction
):
class
Term
(
SemanticAction
):
'''
'''
...
@@ -43,14 +50,14 @@ class Term(SemanticAction):
...
@@ -43,14 +50,14 @@ class Term(SemanticAction):
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
,
nodes
):
logging
.
debug
(
"Term
%
s"
%
nodes
)
print
"Term
%
s"
%
nodes
term
=
nodes
[
0
]
term
=
nodes
[
0
]
for
i
in
range
(
2
,
len
(
nodes
),
2
):
for
i
in
range
(
2
,
len
(
nodes
),
2
):
if
nodes
[
i
-
1
]
==
"*"
:
if
nodes
[
i
-
1
]
==
"*"
:
term
*=
nodes
[
i
]
term
*=
nodes
[
i
]
else
:
else
:
term
/=
nodes
[
i
]
term
/=
nodes
[
i
]
logging
.
debug
(
"Term =
%
f"
%
term
)
print
"Term =
%
f"
%
term
return
term
return
term
class
Expr
(
SemanticAction
):
class
Expr
(
SemanticAction
):
...
@@ -59,7 +66,7 @@ class Expr(SemanticAction):
...
@@ -59,7 +66,7 @@ class Expr(SemanticAction):
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
,
nodes
):
logging
.
debug
(
"Expression
%
s"
%
nodes
)
print
"Expression
%
s"
%
nodes
expr
=
0
expr
=
0
start
=
0
start
=
0
# Check for unary + or - operator
# Check for unary + or - operator
...
@@ -72,7 +79,7 @@ class Expr(SemanticAction):
...
@@ -72,7 +79,7 @@ class Expr(SemanticAction):
else
:
else
:
expr
+=
nodes
[
i
]
expr
+=
nodes
[
i
]
logging
.
debug
(
"Expression =
%
f"
%
expr
)
print
"Expression =
%
f"
%
expr
return
expr
return
expr
class
Calc
(
SemanticAction
):
class
Calc
(
SemanticAction
):
...
@@ -88,8 +95,6 @@ calc.sem = Calc()
...
@@ -88,8 +95,6 @@ calc.sem = Calc()
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
try
:
try
:
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
# 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.
...
...
examples/calc_peg.py
View file @
08653e36
...
@@ -24,9 +24,10 @@ from calc import ToFloat, Factor, Term, Expr, Calc
...
@@ -24,9 +24,10 @@ from calc import ToFloat, Factor, Term, Expr, Calc
# Grammar is defined using textual specification based on PEG language.
# Grammar is defined using textual specification based on PEG language.
calc_grammar
=
"""
calc_grammar
=
"""
number <- r'
\
d*
\
.
\
d*|
\
d+';
number <- r'
\
d*
\
.
\
d*|
\
d+';
factor <- number / "(" expression ")";
factor <- ("+" / "-")?
(number / "(" expression ")");
term <- factor (( "*" / "/") factor)*;
term <- factor (( "*" / "/") factor)*;
expression <-
("+" / "-")?
term (("+" / "-") term)*;
expression <- term (("+" / "-") term)*;
calc <- expression EndOfFile;
calc <- expression EndOfFile;
"""
"""
...
...
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