Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
618eef32
Commit
618eef32
authored
Feb 25, 2019
by
Eckhart Arnold
Browse files
- Arithmetic example extended
parent
44c61528
Changes
4
Hide whitespace changes
Inline
Side-by-side
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
(
Drop
Token
(
"*"
),
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
Supports
Markdown
0%
Try again
or
attach a new 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