示例#1
0
 def parse(self):
     self.stmt = Stmt()
     self.stmt.parse()
     if TokList.checkTok('END') or TokList.checkTok('ELSE') or TokList.checkTok('ENDIF') or TokList.checkTok('ENDWHILE'):
         return
     self.stmt_seq = StmtSeq()
     self.stmt_seq.parse()
示例#2
0
 def parse(self):
     self.factor = Factor()
     self.factor.parse()
     if TokList.checkTok('MULT'):
         TokList.nextToken()
         self.term = Term()
         self.term.parse()
示例#3
0
 def parse(self):
     if TokList.checkTok('ID'):
         self.altNo = 1
         self.s1 = Assign()
         self.s1.parse()
     elif TokList.checkTok('IF'):
         self.altNo = 2
         self.s2 = If()
         self.s2.parse()
     elif TokList.checkTok('WHILE'):
         self.altNo = 3
         self.s3 = Loop()
         self.s3.parse()
     elif TokList.checkTok('INPUT'):
         self.altNo = 4
         self.s4 = In()
         self.s4.parse()
     elif TokList.checkTok('OUTPUT'):
         self.altNo = 5
         self.s5 = Out()
         self.s5.parse()
     elif TokList.checkTok('CASE'):
         self.altNo = 6
         self.s6 = Case()
         self.s6.parse()
     else:
         #Used to briefly check for and EOF exception
         TokList.match('', 'statement')
示例#4
0
 def parse(self):
     if TokList.checkTok('NOT'):
         self.notStr = '!'
         TokList.nextToken()
         TokList.match('LEFTPARAN', 'cond')
         self.cond = Cond()
         self.cond.parse()
         TokList.match('RIGHTPARAN', 'cond')
         return
     self.cmpr = Cmpr()
     self.cmpr.parse()
     if TokList.checkTok('OR'):
         TokList.nextToken()
         self.cond = Cond()
         self.cond.parse()
示例#5
0
 def parse(self):
     TokList.match('WHILE', 'loop')
     self.cond = Cond()
     self.cond.parse()
     TokList.match('BEGIN', 'loop')
     self.stmt_seq = StmtSeq.StmtSeq()
     self.stmt_seq.parse()
     TokList.match('ENDWHILE', 'loop')
     TokList.match('SEMICOLON', 'loop')
示例#6
0
 def parse(self):
     self.decl = Decl()
     self.decl.parse()
     #Checks if the decl_seq is at an end
     if TokList.checkTok('BEGIN'):
         return
     self.decl_seq = DeclSeq()
     self.decl_seq.parse()
示例#7
0
 def print(self):
     TokList.printIndent()
     print('while ', end='')
     self.cond.print()
     print(' begin')
     TokList.increaseIndent()
     self.stmt_seq.print()
     TokList.decreaseIndent()
     TokList.printIndent()
     print('endwhile;')
示例#8
0
 def print(self):
     print("program")
     TokList.increaseIndent()
     self.decl_seq.print()
     TokList.decreaseIndent()
     print("begin")
     TokList.increaseIndent()
     self.stmt_seq.print()
     TokList.decreaseIndent()
     print("end")
示例#9
0
 def parse(self):
     self.id = Id()
     self.id.parse(TokList.getIdOrConst())
     TokList.nextToken()
     TokList.match('ASSIGN', 'assign')
     self.expr = Expr()
     self.expr.parse()
     TokList.match('SEMICOLON', 'assign')
示例#10
0
 def parse(self):
     self.leftExpr = Expr()
     self.leftExpr.parse()
     if TokList.checkTok('EQUAL'):
         self.checkValue = '='
         TokList.nextToken()
     elif TokList.checkTok('LESSTHAN'):
         self.checkValue = '<'
         TokList.nextToken()
     elif TokList.checkTok('LESSTHANEQUAL'):
         self.checkValue = '<='
         TokList.nextToken()
     else:
         print('Error: improper syntax for comparator')
         exit()
     self.rightExpr = Expr()
     self.rightExpr.parse()
示例#11
0
 def parse(self):
     TokList.match("PROGRAM", "program")
     self.decl_seq = DeclSeq()
     self.decl_seq.parse()
     TokList.match("BEGIN", "program")
     self.stmt_seq = StmtSeq()
     self.stmt_seq.parse()
     TokList.match("END", "program")
     #Checks for code after end of program
     if not TokList.checkTok('EOF'):
         print("ERROR: Improper code after end of program")
示例#12
0
 def parse(self):
     TokList.nextToken()
     self.const = Const()
     self.const.parse(TokList.getIdOrConst())
     TokList.nextToken()
     if TokList.currentToken() == 'COMMA':
         self.constList = ConstList()
         self.constList.parse()
示例#13
0
 def parse(self):
     self.id = Id()
     #Passing in name of id to the parse function
     self.id.parse(TokList.getIdOrConst())
     TokList.nextToken()
     if TokList.checkTok('COMMA'):
         TokList.match('COMMA', 'id list')
         self.id_list = IdList()
         self.id_list.parse()
示例#14
0
 def parse(self):
     self.const = Const()
     self.const.parse(TokList.getIdOrConst())
     TokList.nextToken()
     if TokList.currentToken() == 'COMMA':
         self.constList = ConstList()
         self.constList.parse()
     TokList.match('COLON', 'case line')
     self.expr = Expr()
     self.expr.parse()
     self.caseLineFollow = CaseLineFollow()
     self.caseLineFollow.parse()
示例#15
0
 def parse(self):
     self.term = Term()
     self.term.parse()
     if TokList.checkTok('PLUS'):
         TokList.nextToken()
         self.plusOrMinus = '+'
         self.expr = Expr()
         self.expr.parse()
     elif TokList.checkTok('MINUS'):
         TokList.nextToken()
         self.plusOrMinus = '-'
         self.expr = Expr()
         self.expr.parse()
示例#16
0
 def setIdValues(self, init):
     if init == 1:
         if TokList.getIdValue(self.id.name) == 'null':
             print('ERROR: value ' + self.id.name +
                   ' is already initialized')
             exit()
         #Initialized to 'null' as None is the previous assignment, 'null' is used later to check for initialization
         TokList.setIdValue(self.id.name, 'null')
         if self.id_list is not None:
             self.id_list.setIdValues(1)
     else:
         self.id.setValue(TokList.currentData())
         TokList.nextData()
         if self.id_list is not None:
             self.id_list.setIdValues(0)
示例#17
0
 def setValue(self, val):
     if TokList.getIdValue(self.name) is None:
         print('ERROR: Value not initialized')
     TokList.setIdValue(self.name, val)
示例#18
0
 def parse(self):
     TokList.match('INT', "decl")
     self.id_list = IdList()
     self.id_list.parse()
     TokList.match('SEMICOLON', "decl")
示例#19
0
 def parse(self):
     if TokList.checkTok('CONST'):
         self.const = Const()
         self.const.parse(TokList.getIdOrConst())
         TokList.nextToken()
     elif TokList.checkTok('ID'):
         self.id = Id()
         self.id.parse(TokList.getIdOrConst())
         TokList.nextToken()
     elif TokList.match('LEFTPARAN', 'factor'):
         self.expr = Expr.Expr()
         self.expr.parse()
         TokList.match('RIGHTPARAN', 'factor')
示例#20
0
 def parse(self):
     TokList.match('CASE','case')
     self.id = Id()
     self.id.parse(TokList.getIdOrConst())
     TokList.nextToken()
     TokList.match('OF','case')
     self.case_line = CaseLine()
     self.case_line.parse()
     TokList.match('ELSE','case')
     self.expr = Expr()
     self.expr.parse()
     TokList.match('END','case')
     TokList.match('SEMICOLON','case')
示例#21
0
 def print(self):
     TokList.printIndent()
     print('case ', end='')
     self.id.print()
     print(' of')
     TokList.increaseIndent()
     TokList.printIndent()
     self.case_line.print()
     #Necessary for new line printings
     print()
     TokList.printIndent()
     print('else ', end='')
     self.expr.print()
     print()
     TokList.decreaseIndent()
     TokList.printIndent()
     print('end;')
示例#22
0
 def print(self):
     TokList.printIndent()
     self.id.print()
     print(':=', end='')
     self.expr.print()
     print(';')
示例#23
0
 def parse(self):
     TokList.match('INPUT', 'input')
     self.idList = IdList()
     self.idList.parse()
     TokList.match('SEMICOLON', 'input')
示例#24
0
 def getValue(self):
     if TokList.getIdValue(
             self.name) == 'null' or TokList.getIdValue(self.name) is None:
         print('ERROR: Value undeclared')
         exit()
     return int(TokList.getIdValue(self.name))
示例#25
0
 def parse(self):
     TokList.match('OUTPUT', 'output')
     self.expr = Expr()
     self.expr.parse()
     TokList.match('SEMICOLON', 'output')
示例#26
0
 def print(self):
     TokList.printIndent()
     print('output ', end='')
     self.expr.print()
     print(';')
示例#27
0
 def print(self):
     TokList.printIndent()
     print('if ', end='')
     self.cond.print()
     print(' then')
     TokList.increaseIndent()
     self.stmt_seq_then.print()
     if self.stmt_seq_else is not None:
         TokList.decreaseIndent()
         TokList.printIndent()
         print('else')
         TokList.increaseIndent()
         self.stmt_seq_else.print()
     TokList.decreaseIndent()
     TokList.printIndent()
     print('endif;')
示例#28
0
 def print(self):
     TokList.printIndent()
     self.decl.print()
     print(';')
     if self.decl_seq is not None:
         self.decl_seq.print()
示例#29
0
 def print(self):
     TokList.printIndent()
     print('input ', end='')
     self.idList.print()
     print(';')
示例#30
0
from program import Program
from Tokenizer import TokList
import sys
#Recieves the command line arguements for execution
TokList.getFiles(sys.argv[1], sys.argv[2])
#Declares the program object
p = Program()
#Parses, prints, and executes
p.parse()
p.print()
p.exec()