From a4f2a248273064ec464e7d376f8f5778ac808bd6 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Sun, 28 Jun 2009 15:10:57 +0200 Subject: fixed error with not newline at file end moved __init__.py with testcode from src/front to src/ added in lexer "\n\n" at source end to have a newline at file end added unexpected end of file message, if matched token is None --- src/__init__.py | 38 ++++++++++++++++++++++++++++++++++++++ src/front/__init__.py | 38 -------------------------------------- src/front/lexer.py | 2 +- src/front/parser.py | 5 ++++- 4 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 src/__init__.py diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..747efde --- /dev/null +++ b/src/__init__.py @@ -0,0 +1,38 @@ +from front.lexer import Lexer +from front.parser import Parser +#from front.symbols import SymbolTable + +def main(): + source = '''fun fib[a] + if a < 2 + @1 + end + @( fib[a-1] + fib[a-2] ) +end + +# main function +fun main[] + sum = 0 + i = 0 + while (i < 10) + sum = sum + fib[i] + i = i + 1 + end + @sum +end''' + + #symbols = SymbolTable() + #lex = Lexer(source) + + # testing + #while True: + # token = lex.scan() + # print token.__repr__() + # if not token: + # break + + parse = Parser(Lexer(source)) + print parse.parse() + +if __name__ == "__main__": + main() diff --git a/src/front/__init__.py b/src/front/__init__.py index 747efde..e69de29 100644 --- a/src/front/__init__.py +++ b/src/front/__init__.py @@ -1,38 +0,0 @@ -from front.lexer import Lexer -from front.parser import Parser -#from front.symbols import SymbolTable - -def main(): - source = '''fun fib[a] - if a < 2 - @1 - end - @( fib[a-1] + fib[a-2] ) -end - -# main function -fun main[] - sum = 0 - i = 0 - while (i < 10) - sum = sum + fib[i] - i = i + 1 - end - @sum -end''' - - #symbols = SymbolTable() - #lex = Lexer(source) - - # testing - #while True: - # token = lex.scan() - # print token.__repr__() - # if not token: - # break - - parse = Parser(Lexer(source)) - print parse.parse() - -if __name__ == "__main__": - main() diff --git a/src/front/lexer.py b/src/front/lexer.py index ff67e6d..f2810cd 100644 --- a/src/front/lexer.py +++ b/src/front/lexer.py @@ -4,7 +4,7 @@ from token import * class Lexer: def __init__(self, source): - self.source = source.splitlines() + self.source = (source + "\n\n").splitlines() self.source.reverse() self.line = 0 self.doubleNewlineCheck = False diff --git a/src/front/parser.py b/src/front/parser.py index 9e738d3..29e300d 100644 --- a/src/front/parser.py +++ b/src/front/parser.py @@ -23,6 +23,9 @@ class Parser: raise Exception(msg) def match(self, tag): + if self.token == None: + self.error("Unexpected end of file.") + if self.token.tag != tag: self.error("match: expected %s got %s\n" %(tag, self.token.tag)) val = self.token.value @@ -155,7 +158,7 @@ class Parser: # function_list = function { function }. def function_list(self): funcs = [self.function()] - while self.token != None: + while self.token: funcs.append(self.function()) return funcs -- cgit v1.2.3