Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
DHParser
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
badw-it
DHParser
Commits
618eef32
Commit
618eef32
authored
Feb 25, 2019
by
Eckhart Arnold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- Arithmetic example extended
parent
44c61528
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
13 additions
and
13 deletions
+13
-13
examples/Arithmetic/Arithmetic.ebnf
examples/Arithmetic/Arithmetic.ebnf
+4
-4
examples/Arithmetic/ArithmeticCompiler.py
examples/Arithmetic/ArithmeticCompiler.py
+5
-8
examples/Arithmetic/grammar_tests/02_test_components.ini
examples/Arithmetic/grammar_tests/02_test_components.ini
+3
-0
examples/Arithmetic/tst_Arithmetic_grammar.py
examples/Arithmetic/tst_Arithmetic_grammar.py
+1
-1
No files found.
examples/Arithmetic/Arithmetic.ebnf
View file @
618eef32
...
...
@@ -18,9 +18,9 @@
#
#######################################################################
expression = term { (PLUS
|
MINUS) term}
term = factor { (DIV
|
MUL) factor}
factor = [sign] (
NUMBER | VARIABLE | group )
expression = term { (PLUS
|
MINUS) term}
term = factor { (DIV
|
MUL) factor}
factor = [sign] (
NUMBER | VARIABLE | group) { VARIABLE | group }
sign = POSITIVE | NEGATIVE
group = "(" expression ")"
...
...
@@ -32,7 +32,7 @@ group = "(" expression ")"
PLUS = "+"
MINUS = "-"
MUL = "*"
| &factor # TODO: higher precedence of &factor
MUL = "*"
DIV = "/"
POSITIVE = /[+]/ # no implicit whitespace after signs
...
...
examples/Arithmetic/ArithmeticCompiler.py
View file @
618eef32
...
...
@@ -59,8 +59,7 @@ class ArithmeticGrammar(Grammar):
r
"""Parser for an Arithmetic source file.
"""
expression
=
Forward
()
factor
=
Forward
()
source_hash__
=
"6bca790f81db2b6dda4c92abdbe06d90"
source_hash__
=
"a8a1011bf1a9e1204d87031054b831f4"
static_analysis_pending__
=
[
True
]
parser_initialization__
=
[
"upon instantiation"
]
resume_rules__
=
{}
...
...
@@ -74,12 +73,12 @@ class ArithmeticGrammar(Grammar):
NEGATIVE
=
RegExp
(
'[-]'
)
POSITIVE
=
RegExp
(
'[+]'
)
DIV
=
Series
(
Token
(
"/"
),
dwsp__
)
MUL
=
Alternative
(
Series
(
DropToken
(
"*"
),
dwsp__
),
Lookahead
(
factor
)
)
MUL
=
Series
(
Token
(
"*"
),
dwsp__
)
MINUS
=
Series
(
Token
(
"-"
),
dwsp__
)
PLUS
=
Series
(
Token
(
"+"
),
dwsp__
)
group
=
Series
(
Series
(
DropToken
(
"("
),
dwsp__
),
expression
,
Series
(
DropToken
(
")"
),
dwsp__
))
sign
=
Alternative
(
POSITIVE
,
NEGATIVE
)
factor
.
set
(
Series
(
Option
(
sign
),
Alternative
(
NUMBER
,
VARIABLE
,
group
)))
factor
=
Series
(
Option
(
sign
),
Alternative
(
NUMBER
,
VARIABLE
,
group
),
ZeroOrMore
(
Alternative
(
VARIABLE
,
group
)))
term
=
Series
(
factor
,
ZeroOrMore
(
Series
(
Alternative
(
DIV
,
MUL
),
factor
)))
expression
.
set
(
Series
(
term
,
ZeroOrMore
(
Series
(
Alternative
(
PLUS
,
MINUS
),
term
))))
root__
=
expression
...
...
@@ -108,11 +107,9 @@ def group_no_asterix_mul(context):
Arithmetic_AST_transformation_table
=
{
# AST Transformations for the Arithmetic-grammar
"expression"
:
[
left_associative
,
replace_by_single_child
],
"term"
:
[
left_associative
,
replace_by_single_child
],
"factor"
:
[
replace_by_single_child
],
"expression, term"
:
[
left_associative
,
replace_by_single_child
],
"factor, sign"
:
replace_by_single_child
,
"group"
:
[
remove_tokens
(
'('
,
')'
),
replace_by_single_child
],
"sign"
:
[
replace_by_single_child
]
}
...
...
examples/Arithmetic/grammar_tests/02_test_components.ini
View file @
618eef32
...
...
@@ -14,6 +14,9 @@ M3: "-2.71828"
M4:
"-x"
M5:
"(2
+
x)"
M6:
"-(a
*
b)"
M7:
"2x"
M8:
"-2x"
M9:
"-(2
+
4)x(3
+
4)"
[fail:factor]
F1:
"x4"
...
...
examples/Arithmetic/tst_Arithmetic_grammar.py
View file @
618eef32
...
...
@@ -24,7 +24,7 @@ except ModuleNotFoundError:
CONFIG_PRESET
[
'ast_serialization'
]
=
"S-expression"
CONFIG_PRESET
[
'test_parallelization'
]
=
Fals
e
CONFIG_PRESET
[
'test_parallelization'
]
=
Tru
e
def
recompile_grammar
(
grammar_src
,
force
):
grammar_tests_dir
=
os
.
path
.
join
(
scriptpath
,
'grammar_tests'
)
...
...
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