Commit 448d1855 authored by Eckhart Arnold's avatar Eckhart Arnold

- Adjusted Arithmetic example a bit more...

parent 2b52940c
......@@ -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...
......
......@@ -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]/~
......@@ -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:
......
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