Commit f99743f4 authored by eckhart's avatar eckhart
Browse files

- ArithmeticExperimental example extended

parent 52bdcae5
......@@ -41,9 +41,10 @@ div = factor "/" term
#
#######################################################################
factor = [sign] [element] tail
factor = [sign] [element] tail ~
tail = seq | tail_elem
seq = tail_elem tail
sign = PLUS | MINUS
#######################################################################
......@@ -52,16 +53,38 @@ seq = tail_elem tail
#
#######################################################################
element = pow | atom
pow = atom "^" pow
atom = NUMBER | VARIABLE | group
element = pow | value
pow = value `^` pow
value = NUMBER | tail_value
tail_elem = tail_pow | tail_atom
tail_pow = fragment "^" pow
tail_atom = VARIABLE | group
tail_elem = tail_pow | tail_value
tail_pow = tail_value `^` pow
tail_value = special | function | VARIABLE | group
group = `(` §expression `)`
sign = PLUS | MINUS
group = "(" §expression ")"
#######################################################################
#
#: Special Values
#
#######################################################################
special = pi | e
pi = `pi` | `π`
e = `e`
#######################################################################
#
#: Functions
#
#######################################################################
function = sin | cos | tan | log
sin = 'sin(' §expression `)`
cos = 'cos(' §expression `)`
tan = 'tan(' §expression `)`
log = 'log(' §expression `)`
#######################################################################
......@@ -72,5 +95,5 @@ group = "(" §expression ")"
PLUS = /\+/
MINUS = /-/
NUMBER = /(?:0|(?:[1-9]\d*))(?:\.\d+)?/~
VARIABLE = /[i-z]/~
NUMBER = /(?:0|(?:[1-9]\d*))(?:\.\d+)?i?/
VARIABLE = /[j-z]/
......@@ -59,8 +59,10 @@ class ArithmeticExperimentalGrammar(Grammar):
r"""Parser for an ArithmeticExperimental source file.
"""
expression = Forward()
pow = Forward()
tail = Forward()
term = Forward()
source_hash__ = "59c96ad0b1c0703bea6ba8fc50253bea"
source_hash__ = "eee86a4b8fdffdf468581501debf2461"
static_analysis_pending__ = [True]
parser_initialization__ = ["upon instantiation"]
resume_rules__ = {}
......@@ -69,15 +71,31 @@ class ArithmeticExperimentalGrammar(Grammar):
WSP_RE__ = mixin_comment(whitespace=WHITESPACE__, comment=COMMENT__)
dwsp__ = DropWhitespace(WSP_RE__)
wsp__ = Whitespace(WSP_RE__)
VARIABLE = Series(RegExp('[A-Za-z]'), dwsp__)
NUMBER = Series(RegExp('(?:0|(?:[1-9]\\d*))(?:\\.\\d+)?'), dwsp__)
VARIABLE = RegExp('[j-z]')
NUMBER = RegExp('(?:0|(?:[1-9]\\d*))(?:\\.\\d+)?i?')
MINUS = RegExp('-')
PLUS = RegExp('\\+')
group = Series(Series(DropToken("("), dwsp__), expression, Series(DropToken(")"), dwsp__), mandatory=1)
log = Series(Series(DropToken('log('), dwsp__), expression, DropToken(")"), mandatory=1)
tan = Series(Series(DropToken('tan('), dwsp__), expression, DropToken(")"), mandatory=1)
cos = Series(Series(DropToken('cos('), dwsp__), expression, DropToken(")"), mandatory=1)
sin = Series(Series(DropToken('sin('), dwsp__), expression, DropToken(")"), mandatory=1)
function = Alternative(sin, cos, tan, log)
e = Token("e")
pi = Alternative(DropToken("pi"), DropToken("π"))
special = Alternative(pi, e)
group = Series(DropToken("("), expression, DropToken(")"), mandatory=1)
tail_value = Alternative(special, function, VARIABLE, group)
tail_pow = Series(tail_value, DropToken("^"), pow)
tail_elem = Alternative(tail_pow, tail_value)
value = Alternative(NUMBER, tail_value)
pow.set(Series(value, DropToken("^"), pow))
element = Alternative(pow, value)
sign = Alternative(PLUS, MINUS)
factor = Series(Option(sign), Alternative(NUMBER, VARIABLE, group))
seq = Series(tail_elem, tail)
tail.set(Alternative(seq, tail_elem))
factor = Series(Option(sign), Option(element), tail, dwsp__)
div = Series(factor, Series(DropToken("/"), dwsp__), term)
mul = Series(factor, Option(Series(DropToken("*"), dwsp__)), term)
mul = Series(factor, Series(DropToken("*"), dwsp__), term)
term.set(Alternative(mul, div, factor))
sub = Series(term, Series(DropToken("-"), dwsp__), expression)
add = Series(term, Series(DropToken("+"), dwsp__), expression)
......
......@@ -19,9 +19,14 @@ F5: -22
[match:VARIABLE]
M1: a
M1: j
M2: Z
[fail:VARIABLE]
F1: ä
F2: ab
F3: a
F4: e
F5: i
F6: π
......@@ -37,7 +37,8 @@ def recompile_grammar(grammar_src, force):
with DHParser.log.logging(LOGGING):
# recompiles Grammar only if it has changed
name = os.path.splitext(os.path.basename(grammar_src))[0]
if not dsl.recompile_grammar(grammar_src, force=force):
if not dsl.recompile_grammar(grammar_src, force=force,
notify=lambda: print('recompiling ' + grammar_src)):
print('\nErrors while recompiling "{}":'.format(grammar_src) +
'\n--------------------------------------\n\n')
with open('{}_ebnf_ERRORS.txt'.format(name)) as f:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment