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
448d1855
Commit
448d1855
authored
Feb 09, 2019
by
Eckhart Arnold
Browse files
- Adjusted Arithmetic example a bit more...
parent
2b52940c
Changes
3
Hide whitespace changes
Inline
Side-by-side
dhparser.py
View file @
448d1855
...
...
@@ -16,7 +16,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
"""
3
"""
# TODO: This is still a stub...
...
...
examples/Arithmetic/Arithmetic.ebnf
View file @
448d1855
...
...
@@ -6,11 +6,11 @@
#
#######################################################################
@ whitespace = vertical # implicit whitespace, includes any number of line feeds
@ literalws = right # literals have implicit whitespace on the right hand side
@ comment = /#.*/ # comments range from a '#'-character to the end of the line
@ ignorecase = False # literals and regular expressions are case-sensitive
@ drop = whitespace
# drop anonymous whitespace
@ whitespace = vertical
# implicit whitespace, includes any number of line feeds
@ literalws = right
# literals have implicit whitespace on the right hand side
@ comment = /#.*/
# comments range from a '#'-character to the end of the line
@ ignorecase = False
# literals and regular expressions are case-sensitive
@ drop = whitespace
, token
# drop anonymous whitespace
#######################################################################
#
...
...
@@ -18,16 +18,20 @@
#
#######################################################################
expression = term {
("+" | "-")
term}
term = factor {
("*" | "/")
factor}
factor = [
/-/
] ( NUMBER | VARIABLE | group ) { VARIABLE | group }
expression = term {
EXPR_OP
term}
term = factor {
TERM_OP
factor}
factor = [
SIGN
] ( NUMBER | VARIABLE | group ) { VARIABLE | group }
group = "(" expression ")"
#######################################################################
#
#
Regular
Expressions
#
"Leaf"-
Expressions
#
#######################################################################
EXPR_OP = "+" | "-"
TERM_OP = "*" | "/"
SIGN = /-/
NUMBER = /(?:0|(?:[1-9]\d*))(?:\.\d+)?/~
VARIABLE = /[A-Za-z]/~
examples/Arithmetic/ArithmeticCompiler.py
View file @
448d1855
...
...
@@ -59,7 +59,7 @@ class ArithmeticGrammar(Grammar):
r
"""Parser for an Arithmetic source file.
"""
expression
=
Forward
()
source_hash__
=
"
a94242482d508901d1692b82c48ba903
"
source_hash__
=
"
5a5e8df98d9c78186acdd7f602aa51da
"
parser_initialization__
=
[
"upon instantiation"
]
resume_rules__
=
{}
COMMENT__
=
r
'#.*'
...
...
@@ -69,10 +69,13 @@ class ArithmeticGrammar(Grammar):
wsp__
=
Whitespace
(
WSP_RE__
)
VARIABLE
=
Series
(
RegExp
(
'[A-Za-z]'
),
dwsp__
)
NUMBER
=
Series
(
RegExp
(
'(?:0|(?:[1-9]
\\
d*))(?:
\\
.
\\
d+)?'
),
dwsp__
)
group
=
Series
(
Series
(
Token
(
"("
),
dwsp__
),
expression
,
Series
(
Token
(
")"
),
dwsp__
))
factor
=
Series
(
Option
(
RegExp
(
'-'
)),
Alternative
(
NUMBER
,
VARIABLE
,
group
),
ZeroOrMore
(
Alternative
(
VARIABLE
,
group
)))
term
=
Series
(
factor
,
ZeroOrMore
(
Series
(
Alternative
(
Series
(
Token
(
"*"
),
dwsp__
),
Series
(
Token
(
"/"
),
dwsp__
)),
factor
)))
expression
.
set
(
Series
(
term
,
ZeroOrMore
(
Series
(
Alternative
(
Series
(
Token
(
"+"
),
dwsp__
),
Series
(
Token
(
"-"
),
dwsp__
)),
term
))))
SIGN
=
RegExp
(
'-'
)
TERM_OP
=
Alternative
(
Series
(
DropToken
(
"*"
),
dwsp__
),
Series
(
DropToken
(
"/"
),
dwsp__
))
EXPR_OP
=
Alternative
(
Series
(
DropToken
(
"+"
),
dwsp__
),
Series
(
DropToken
(
"-"
),
dwsp__
))
group
=
Series
(
Series
(
DropToken
(
"("
),
dwsp__
),
expression
,
Series
(
DropToken
(
")"
),
dwsp__
))
factor
=
Series
(
Option
(
SIGN
),
Alternative
(
NUMBER
,
VARIABLE
,
group
),
ZeroOrMore
(
Alternative
(
VARIABLE
,
group
)))
term
=
Series
(
factor
,
ZeroOrMore
(
Series
(
TERM_OP
,
factor
)))
expression
.
set
(
Series
(
term
,
ZeroOrMore
(
Series
(
EXPR_OP
,
term
))))
root__
=
expression
def
get_grammar
()
->
ArithmeticGrammar
:
...
...
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