diff options
author | Benedikt Böhm <bb@xnull.de> | 2009-05-18 20:53:42 +0200 |
---|---|---|
committer | Benedikt Böhm <bb@xnull.de> | 2009-05-18 20:53:42 +0200 |
commit | 49f510d2d60129526832bfcd9c0f4049962bc80e (patch) | |
tree | 2828b773c75bdbc4014a3421c8046778a1a2b11b /doc | |
parent | 144258f0196b69cbdd2f29bd501276942efc3182 (diff) | |
download | swppy-49f510d2d60129526832bfcd9c0f4049962bc80e.tar.gz swppy-49f510d2d60129526832bfcd9c0f4049962bc80e.tar.xz swppy-49f510d2d60129526832bfcd9c0f4049962bc80e.zip |
move stuff around and create initial source structure
Diffstat (limited to 'doc')
-rw-r--r-- | doc/demo-funktionen.txt | 53 | ||||
-rw-r--r-- | doc/ebnf.py | 152 | ||||
-rw-r--r-- | doc/mylang.ebnf | 33 |
3 files changed, 238 insertions, 0 deletions
diff --git a/doc/demo-funktionen.txt b/doc/demo-funktionen.txt new file mode 100644 index 0000000..ad16f4b --- /dev/null +++ b/doc/demo-funktionen.txt @@ -0,0 +1,53 @@ + +fun fib[a] + if a < 2 + @1 + @( fib[a-1] + fib[a-2] ) +end + +# main function +fun main[] + sum = 0 + i = 0 + while (i < 10) + sum = sum + fib[i = i + 1] + end + @sum +end + +#------------------------------------------------- alex + +0 : "fib" +1 : "a" + +<fun> <identifier,0> <[> <identifier,1> <]> <nl> +<tab> <if> <identifier,1> <operator,lessthan> <number,2> <nl> +<tab> <tab> <return> <number,1> <nl> +<tab> <return> <(><identifier,0> <[> <identifier,1> <operator,minus> <number,1> <]> <operator,plus> <identifier,0> <[> <identifier,1> <operator,minus> <number,2> <]> <)> <nl> +<end> + +... + + +#------------------------------------------------- michael + +PROGRAM +| ++- FUNCTION +| + <fun> <identifier,0> <[> <identifier,1> <]> STATEMENT <end> +| + <if> EXPRESSION STATEMENT <end> +| + EXPRESSION OPERATOR EXPRESSION + .... +| + <identifier,1> + <operator,lessthan> +<number,2> ++- FUNCTION +... + +#------------------------------------------------- nutz + + +zwischencode zeug mit annotiertem baum +3-Adress-Code + +#-------------------------------------------------- jana + + +maschinencode / endprodukt diff --git a/doc/ebnf.py b/doc/ebnf.py new file mode 100644 index 0000000..1989118 --- /dev/null +++ b/doc/ebnf.py @@ -0,0 +1,152 @@ +import string + +class EBNF: + IDENT = 0 + LITERAL = 2 + LPAREN = 3 + LBRAK = 4 + LBRACE = 5 + BAR = 6 + EQL = 7 + RPAREN = 8 + RBRAK = 9 + RBRACE = 10 + PERIOD = 11 + OTHER = 12 + + __input__ = None + __char__ = None + __symbol__ = None + __data__ = None + + def __init__(self, input): + self.__input__ = input + self.__get_char__() + + def __get_char__(self): + self.__char__ = self.__input__.read(1) + if self.__char__ == '': + raise EOFError + return self.__char__ + + def __get_sym__(self): + self.__data__ = None + + while self.__char__ in string.whitespace: + self.__get_char__() + + if self.__char__ in string.letters + "_": + self.__symbol__ = self.IDENT + self.__data__ = self.__char__ + while self.__get_char__() in string.letters + "_": + self.__data__ += self.__char__ + return + + elif self.__char__ == '"': + self.__symbol__ = self.LITERAL + self.__data__ = "" + while self.__get_char__() != '"': + self.__data__ += self.__char__ + + elif self.__char__ == '=': + self.__symbol__ = self.EQL + elif self.__char__ == '(': + self.__symbol__ = self.LPAREN + elif self.__char__ == ')': + self.__symbol__ = self.RPAREN + elif self.__char__ == '[': + self.__symbol__ = self.LBRAK + elif self.__char__ == ']': + self.__symbol__ = self.RBRAK + elif self.__char__ == '{': + self.__symbol__ = self.LBRACE + elif self.__char__ == '}': + self.__symbol__ = self.RBRACE + elif self.__char__ == '|': + self.__symbol__ = self.BAR + elif self.__char__ == '.': + self.__symbol__ = self.PERIOD + else: + self.__symbol__ = self.OTHER + + self.__get_char__() + + def __error__(self, num): + pos = self.__input__.tell() + if pos > self.__lastpos__ + 2: + print "ERROR: pos=%d, err=%d, sym=%d" % (pos, num, self.__symbol__) + self.__lastpos__ = pos + + def __expression__(self): + self.__term__() + while self.__symbol__ == self.BAR: + self.__get_sym__() + self.__term__() + + def __term__(self): + self.__factor__() + while self.__symbol__ < self.BAR: + self.__factor__() + + def __factor__(self): + if self.__symbol__ == self.IDENT: + print "record(TO, '%s', 1)" % (self.__data__,) + self.__get_sym__() + elif self.__symbol__ == self.LITERAL: + print "record(T1, '%s', 0)" % (self.__data__,) + self.__get_sym__() + elif self.__symbol__ == self.LPAREN: + self.__get_sym__() + self.__expression__() + if self.__symbol__ == self.RPAREN: + self.__get_sym__() + else: + self.__error__(2) + elif self.__symbol__ == self.LBRAK: + self.__get_sym__() + self.__expression__() + if self.__symbol__ == self.RBRAK: + self.__get_sym__() + else: + self.__error__(3) + elif self.__symbol__ == self.LBRACE: + self.__get_sym__() + self.__expression__() + if self.__symbol__ == self.RBRACE: + self.__get_sym__() + else: + self.__error__(4) + else: + self.__error__(5) + + def __production__(self): + self.__get_sym__() + + if self.__symbol__ == self.EQL: + self.__get_sym__() + else: + self.__error__(7) + + self.__expression__() + + if self.__symbol__ == self.PERIOD: + self.__get_sym__() + else: + self.__error__(8) + + def __syntax__(self): + while self.__symbol__ == self.IDENT: + self.__production__() + + def compile(self): + try: + self.__lastpos__ = 0 + self.__get_sym__() + self.__syntax__() + except EOFError: + return + +if __name__ == '__main__': + import sys + ebnf = EBNF(sys.stdin) + ebnf.compile() diff --git a/doc/mylang.ebnf b/doc/mylang.ebnf new file mode 100644 index 0000000..53be3a4 --- /dev/null +++ b/doc/mylang.ebnf @@ -0,0 +1,33 @@ +lowercaseLetter = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z". +uppercaseLetter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z". +letter = lowercaseLetter | uppercaseLetter. + +digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9". +nl = "\n". + +ident = letter { letter | digit }. +integer = digit { digit }. + +factor = ident | integer | "(" expression ")" | "~" factor. +term = factor {("*" | "/" | "%" | "&&") factor}. +simple_expression = ["+" | "-"] term {("+" | "-" | "||") term}. +expression = simple_expression [ ( "==" | "!=" | ">=" | "<=" | ">" | "<" ) simple_expression ]. + +ident_list = ident { "," ident }. +expression_list = expression { "," expression }. + +program = function { function }. +function = "fun" ident "[" [ ident_list ] "]" statements "end". + +statement = [ if_statement | while_statement | assignment | function_call | return_statement ]. +statements = statement { nl statement }. + +if_statement = "if" expression statements [ "else" statements ] "end". +while_statement = "while" expression statements "end". +return_statement = "@" expression. +assignment = ident "=" expression. +function_call = ident "[" [ expression_list ] "]". + + + + |