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
3c79d326
Commit
3c79d326
authored
Aug 04, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix in the parse tree reduction. Tests.
parent
f305a445
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
12 deletions
+52
-12
__init__.py
arpeggio/__init__.py
+1
-1
test_python_parser.py
tests/unit/test_python_parser.py
+0
-11
test_reduce_tree.py
tests/unit/test_reduce_tree.py
+51
-0
No files found.
arpeggio/__init__.py
View file @
3c79d326
...
...
@@ -292,7 +292,7 @@ class OrderedChoice(Sequence):
c_pos
=
parser
.
position
for
e
in
self
.
nodes
:
try
:
result
=
e
.
parse
(
parser
)
result
=
[
e
.
parse
(
parser
)]
match
=
True
except
NoMatch
as
m
:
parser
.
position
=
c_pos
# Backtracking
...
...
tests/unit/test_python_parser.py
View file @
3c79d326
...
...
@@ -43,14 +43,3 @@ def test_parse_input():
assert
str
(
result
)
==
"4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
assert
repr
(
result
)
==
"[ [ [ [ number '4' [0] ] ], '+' [1], [ [ number '5' [2] ], '*' [3], [ number '7' [4] ], '/' [5], [ number '3.45' [6] ], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ [ [ number '2.56' [16] ] ], '+' [20], [ [ number '32' [21] ] ] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ [ [ number '2' [30] ] ], '-' [31], [ [ number '1.34' [32] ] ] ], ')' [36] ] ] ], EOF [37] ]"
def
test_reduce_tree
():
parser
=
ParserPython
(
calc
,
reduce_tree
=
True
)
input
=
"4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
result
=
parser
.
parse
(
input
)
assert
isinstance
(
result
,
NonTerminal
)
assert
str
(
result
)
==
"4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
assert
repr
(
result
)
==
"[ [ number '4' [0], '+' [1], [ number '5' [2], '*' [3], number '7' [4], '/' [5], number '3.45' [6], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ number '2.56' [16], '+' [20], number '32' [21] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ number '2' [30], '-' [31], number '1.34' [32] ], ')' [36] ] ] ], EOF [37] ]"
tests/unit/test_reduce_tree.py
0 → 100644
View file @
3c79d326
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_reduce_tree
# Purpose: Test parse tree reduction
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
import
pytest
# Grammar
from
arpeggio
import
ZeroOrMore
,
OneOrMore
,
ParserPython
,
Terminal
,
NonTerminal
from
arpeggio.export
import
PTDOTExporter
from
arpeggio
import
RegExMatch
as
_
def
grammar
():
return
first
,
"a"
,
second
,
[
first
,
second
]
def
first
():
return
[
fourth
,
third
],
ZeroOrMore
(
third
)
def
second
():
return
OneOrMore
(
third
),
"b"
def
third
():
return
[
third_str
,
fourth
]
def
third_str
():
return
"3"
def
fourth
():
return
_
(
r'\d+'
)
def
test_reduce_tree
():
input
=
"34 a 3 3 b 3 b"
parser
=
ParserPython
(
grammar
,
reduce_tree
=
False
)
result
=
parser
.
parse
(
input
)
# PTDOTExporter().exportFile(result, 'test_reduce_tree_pt.dot')
assert
result
[
0
]
.
rule
==
'first'
assert
isinstance
(
result
[
0
],
NonTerminal
)
assert
result
[
3
]
.
rule
==
'first'
assert
result
[
0
][
0
]
.
rule
==
'fourth'
# Check reduction for direct OrderedChoice
assert
result
[
2
][
0
]
.
rule
==
'third'
parser
=
ParserPython
(
grammar
,
reduce_tree
=
True
)
result
=
parser
.
parse
(
input
)
# PTDOTExporter().exportFile(result, 'test_reduce_tree_pt.dot')
assert
result
[
0
]
.
rule
==
'fourth'
assert
isinstance
(
result
[
0
],
Terminal
)
assert
result
[
3
]
.
rule
==
'fourth'
# Check reduction for direct OrderedChoice
assert
result
[
2
][
0
]
.
rule
==
'third_str'
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