Arithmetic.ebnf 1.4 KB
Newer Older
eckhart's avatar
eckhart committed
1
2
3
4
5
6
7
8
# Arithmetic-grammar

#######################################################################
#
#  EBNF-Directives
#
#######################################################################

9
10
11
12
13
@ 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
eckhart's avatar
eckhart committed
14
15
16

#######################################################################
#
Eckhart Arnold's avatar
Eckhart Arnold committed
17
#: Structure and Components
eckhart's avatar
eckhart committed
18
19
20
#
#######################################################################

Eckhart Arnold's avatar
Eckhart Arnold committed
21
expression = term  { (PLUS|MINUS) term}
22
term       = factor { (DIV|MUL) factor}
Eckhart Arnold's avatar
Eckhart Arnold committed
23
24
25
factor     = [sign] ( NUMBER | VARIABLE | group )
sign       = POSITIVE | NEGATIVE
group      = "(" expression ")"
26
27
28

#######################################################################
#
Eckhart Arnold's avatar
Eckhart Arnold committed
29
#: "Leaf"-Expressions
30
31
32
#
#######################################################################

Eckhart Arnold's avatar
Eckhart Arnold committed
33
34
PLUS       = "+"
MINUS      = "-"
35
MUL        = "*" | &factor  # TODO: higher precedence of &factor
Eckhart Arnold's avatar
Eckhart Arnold committed
36
DIV        = "/"
37

Eckhart Arnold's avatar
Eckhart Arnold committed
38
39
POSITIVE   = /[+]/      # no implicit whitespace after signs
NEGATIVE   = /[-]/
40

eckhart's avatar
eckhart committed
41
42
NUMBER     = /(?:0|(?:[1-9]\d*))(?:\.\d+)?/~
VARIABLE   = /[A-Za-z]/~