parser.py 16.7 KB
Newer Older
Aravind Bk's avatar
Aravind Bk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
#-------------------------------------------------------------------------
#Parser.py -- ATG file parser
#Compiler Generator Coco/R,
#Copyright (c) 1990, 2004 Hanspeter Moessenboeck, University of Linz
#extended by M. Loeberbauer & A. Woess, Univ. of Linz
#ported from Java to Python by Ronald Longo
#Parser.frame modified for use as incremental model checker by Sean Sedwards
#
#This program is free software; you can redistribute it and/or modify it
#under the terms of the GNU General Public License as published by the
#Free Software Foundation; either version 2, or (at your option) any
#later version.
#
#This program is distributed in the hope that it will be useful, but
#WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#for more details.
#
#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#As an exception, it is allowed to write an extension of Coco/R that is
#used as a plugin in non-free software.
#
#If not otherwise stated, any source code generated by Coco/R (other than
#Coco/R itself) does not fall under the GNU General Public License.
#-------------------------------------------------------------------------*/

import sys

# Sean changed Scanner to model_checker.scanner
from model_checker.scanner import Token
from model_checker.scanner import Scanner
from model_checker.scanner import Position

class ErrorRec( object ):
   def __init__( self, l, c, s ):
      self.line   = l
      self.col    = c
      self.num    = 0
      self.str    = s


class Errors( object ):
   # errMsgFormat = "file %(file)s : (%(line)d, %(col)d) %(text)s\n"
   # Sean removed "file" from message format
   errMsgFormat = "%(file)s : (%(line)d, %(col)d) %(text)s\n"
   eof          = False
   count        = 0         # number of errors detected
   fileName     = ''
   listName     = ''
   mergeErrors  = False
   mergedList   = None      # PrintWriter
   errors       = [ ]
   minErrDist   = 2
   errDist      = minErrDist
      # A function with prototype: f( errorNum=None ) where errorNum is a
      # predefined error number.  f returns a tuple, ( line, column, message )
      # such that line and column refer to the location in the 
      # source file most recently parsed.  message is the error
      # message corresponging to errorNum.

   @staticmethod
   def Init( fn, dir, merge, getParsingPos, errorMessages ):
      Errors.theErrors = [ ]
      Errors.getParsingPos = getParsingPos
      Errors.errorMessages = errorMessages
      Errors.fileName = fn
      listName = dir + 'listing.txt'
      Errors.mergeErrors = merge
      if Errors.mergeErrors:
         try:
            Errors.mergedList = open( listName, 'w' )
         except IOError:
            raise RuntimeError( '-- Compiler Error: could not open ' + listName )

   @staticmethod
   def storeError( line, col, s ):
      if Errors.mergeErrors:
         Errors.errors.append( ErrorRec( line, col, s ) )
      else:
         Errors.printMsg( Errors.fileName, line, col, s )

   @staticmethod
   def SynErr( errNum, errPos=None ):
      line,col = errPos if errPos else Errors.getParsingPos( )
      msg = Errors.errorMessages[ errNum ]
      Errors.storeError( line, col, msg )
      Errors.count += 1
      # Sean added exit
      sys.exit( 1 )

   @staticmethod
   def SemErr( errMsg, errPos=None ):
      line,col = errPos if errPos else Errors.getParsingPos( )
      Errors.storeError( line, col, errMsg )
      Errors.count += 1
      # Sean added exit
      sys.exit( 1 )

   @staticmethod
   def Warn( errMsg, errPos=None ):
      line,col = errPos if errPos else Errors.getParsingPos( )
      Errors.storeError( line, col, errMsg )

   @staticmethod
   def Exception( errMsg ):
      print (errMsg)
      sys.exit( 1 )

   @staticmethod
   def printMsg( fileName, line, column, msg ):
      vals = { 'file':fileName, 'line':line, 'col':column, 'text':msg }
      sys.stdout.write( Errors.errMsgFormat % vals )

   @staticmethod
   def display( s, e ):
      Errors.mergedList.write('**** ')
      for c in range( 1, e.col ):
         if s[c-1] == '\t':
            Errors.mergedList.write( '\t' )
         else:
            Errors.mergedList.write( ' ' )
      Errors.mergedList.write( '^ ' + e.str + '\n')

   @staticmethod
   def Summarize( sourceBuffer ):
      if Errors.mergeErrors:     
         # Initialize the line iterator
         srcLineIter = iter(sourceBuffer)
         srcLineStr  = srcLineIter.next( )
         srcLineNum  = 1
         
         try:
            # Initialize the error iterator
            errIter = iter(Errors.errors)
            errRec  = errIter.next( )
            
            # Advance to the source line of the next error
            while srcLineNum < errRec.line:
               Errors.mergedList.write( '%4d %s\n' % (srcLineNum, srcLineStr) )
               
               srcLineStr = srcLineIter.next( )
               srcLineNum += 1
            
            # Write out all errors for the current source line
            while errRec.line == srcLineNum:
               Errors.display( srcLineStr, errRec )
               
               errRec = errIter.next( )
         except:
            pass
         
         # No more errors to report
         try:
            # Advance to end of source file
            while True:
               Errors.mergedList.write( '%4d %s\n' % (srcLineNum, srcLineStr) )
               
               srcLineStr = srcLineIter.next( )
               srcLineNum += 1
         except:
            pass
         
         Errors.mergedList.write( '\n' )
         Errors.mergedList.write( '%d errors detected\n' % Errors.count )
         Errors.mergedList.close( )
      
      sys.stdout.write( '%d errors detected\n' % Errors.count )
      if (Errors.count > 0) and Errors.mergeErrors:
         sys.stdout.write( 'see ' + Errors.listName + '\n' )


class Parser( object ):
   _EOF = 0
   _proposition = 1
   maxT = 14

   T          = True
   x          = False
   minErrDist = 2

   UNDEFINED = -1
   FALSE = 0
   UNDECIDED = 1
   TRUE = 2
   
   
   def Check_old(self, propscanner, trace) :
      """Deprecated method to check an entire trace with a new property Scanner.
         Includes SetProperty, which includes ResetProperty. 
      """
      self.SetProperty(propscanner)
      for state in trace :
         result = self.CheckIncremental(state)
         if result != Parser.UNDECIDED : break
      return result
      
   def Check(self, trace) :
      """Checks an entire trace w.r.t. an existing property Scanner.
         Includes ResetProperty, but not SetProperty.
      """
      self.ResetProperty()
      for state in trace :
         result = self.CheckIncremental(state)
         if result != Parser.UNDECIDED : break
      return result
                
   def SetProperty(self, propscanner) :
      """Sets the property Scanner that tokenizes the property.
      """
      self.scanner = propscanner
      self.ResetProperty()
      
   def ResetProperty(self) :
      """Re-iniitializes an existing property Scanner.
      """
      self.step = 0
      self.maxfactor = -1
      self.start = self.scanner.t = self.scanner.tokens

   def CheckIncremental(self, state) :
      """Checks a new state w.r.t. an existing property Scanner.
	      Constructs a trace from new states using a new or previous trace list.
	      If a previous trace list is used, states after index self.step are not valid.
      """
      if len(self.trace) <= self.step :
         self.trace.append(state)
      else :
         self.trace[self.step] = state
      result = Parser.UNDECIDED
      while self.step > self.maxfactor and result == Parser.UNDECIDED :
         self.scanner.t = self.start
         self.la = self.start
         self.Get()
         result = self.Property(0)
         self.Expect(0)
         # print ("step [{}] checked state [{}]".format(self.step, self.maxfactor))
      self.step += 1
      return result



   def __init__( self ):
      self.scanner     = None
      self.token       = None           # last recognized token
      self.la          = None           # lookahead token
      self.genScanner  = False
      self.tokenString = ''             # used in declarations of literal tokens
      self.noString    = '-none-'       # used in declarations of literal tokens
      self.errDist     = Parser.minErrDist
      self.trace       = []

   def getParsingPos( self ):
      return self.la.line, self.la.col

   def SynErr( self, errNum ):
      if self.errDist >= Parser.minErrDist:
         Errors.SynErr( errNum )

      self.errDist = 0

   def SemErr( self, msg ):
      if self.errDist >= Parser.minErrDist:
         Errors.SemErr( msg )

      self.errDist = 0

   def Warning( self, msg ):
      if self.errDist >= Parser.minErrDist:
         Errors.Warn( msg )
      
      self.errDist = 0

   def Successful( self ):
      return Errors.count == 0;

   def LexString( self ):
      return self.token.val

   def LookAheadString( self ):
      return self.la.val

   def Get( self ):
      while True:
         self.token = self.la
         # Sean added print
         # print (self.la)
         self.la = self.scanner.Scan( )
         if self.la.kind <= Parser.maxT:
            self.errDist += 1
            break
         
         self.la = self.token

   def Expect( self, n ):
      if self.la.kind == n:
         self.Get( )
      else:
         self.SynErr( n )

   def StartOf( self, s ):
      return self.set[s][self.la.kind]

   def ExpectWeak( self, n, follow ):
      if self.la.kind == n:
         self.Get( )
      else:
         self.SynErr( n )
         while not self.StartOf(follow):
            self.Get( )

   def WeakSeparator( self, n, syFol, repFol ):
      s = [ False for i in range( Parser.maxT+1 ) ]
      if self.la.kind == n:
         self.Get( )
         return True
      elif self.StartOf(repFol):
         return False
      else:
         for i in range( Parser.maxT ):
            s[i] = self.set[syFol][i] or self.set[repFol][i] or self.set[0][i]
         self.SynErr( n )
         while not s[self.la.kind]:
            self.Get( )
         return self.StartOf( syFol )

   def PyCheck( self ):
      """Unused dummy method.""" 
      result = self.Property(0)
      return result

   def Property( self, index ):
      """Main property entry point.""" 
      start = self.la 
      if self.step == 0 :
         start.result = Parser.UNDECIDED
         start.index = 0
      if index > start.index :
         start.index = index
         start.result = Parser.UNDECIDED 
      if self.StartOf(1):
         result = self.Implication(start.index if start.until else index)
         if self.step == 0 : start.result = result 
         if (self.la.kind == 2):
            if result == Parser.UNDECIDED : self.SemErr("first argument of U is UNDECIDED") 
            self.Get( )
            start.until = True							# label previous Implication as part of Until 
            r2 = self.Implication(start.index)
            if start.result == Parser.UNDECIDED :	# Until not yet FALSE of TRUE
               if r2 == Parser.TRUE :
                  result = start.result = Parser.TRUE
               elif result == Parser.FALSE :
                  start.result = result
               else :
                  result = Parser.UNDECIDED
                  start.index += 1
            else :											# Until is already decided, so result is unchanged
               result = start.result 
      elif self.la.kind == 3:
         self.Get( )
         result = self.Implication(start.index)
         if start.result == Parser.UNDECIDED :	# Finally not yet FALSE or TRUE
            if result == Parser.TRUE :				# sub-formula is decided TRUE 
               start.result = result				# Finally will remain TRUE
            elif result == Parser.FALSE :			# sub-formula is decided FALSE,
               result = Parser.UNDECIDED			# but Finally is not decided,
               start.index += 1						# so check next sub-formula
            else :										# sub-formula is UNDECIDED, 
               result = Parser.UNDECIDED			# so Finally is not decided 
               start.index = start.index			# send the same index to sub-formula
         else :											# Finally is already decided, so result is unchanged
            result = start.result 
      elif self.la.kind == 4:
         self.Get( )
         result = self.Implication(start.index)
         if start.result == Parser.UNDECIDED :	# Globally not yet FALSE or TRUE
            if result == Parser.FALSE :			# sub-formula is decided FALSE
               start.result = Parser.FALSE		# Globally will remain FALSE
            elif result == Parser.TRUE :			# sub-formula is decided TRUE,
               result = Parser.UNDECIDED			# but Globally is not decided
               start.index += 1						# check the next sub-formula
            else :										# result is UNDECIDED, so sub-formula is not yet resolved
               result = Parser.UNDECIDED			# and Globally is also not resolved
               start.index = start.index			# send the same index to sub-formula
         else :											# Globally is already decided, so result is unchanged
            result = start.result 
      elif self.la.kind == 5:
         self.Get( )
         result = self.Implication(start.index)
         if start.index == index+1 :
            if result == Parser.UNDECIDED :
               result = Parser.UNDECIDED
               start.index = start.index
            else :
               start.result = result
               start.index += 1
         elif start.index == index :
            start.index += 1
            result = Parser.UNDECIDED
         elif index > start.index :
            self.SemErr("X has lost synchronization: index = {}, start.index = {}".format(index,start.index)) 
      else:
         self.SynErr(15)
      return result

   def Implication( self, index ):
      result = self.Disjunction(index)
      if (self.la.kind == 6):
         self.Get( )
         r2 = self.Disjunction(index)
         if result == Parser.FALSE or r2 == Parser.TRUE :
            result = Parser.TRUE
         elif result == Parser.TRUE and r2 == Parser.FALSE :
            result = Parser.FALSE
         elif result == Parser.UNDECIDED or r2 == Parser.UNDECIDED :
            result = Parser.UNDECIDED
         else :
            self.SemErr("unrecognized argument") 
      return result

   def Disjunction( self, index ):
      result = self.Conjunction(index)
      while self.la.kind == 7:
         self.Get( )
         r2 = self.Conjunction(index)
         if result == Parser.TRUE or r2 == Parser.TRUE : result = Parser.TRUE
         elif result == Parser.UNDECIDED or r2 == Parser.UNDECIDED : result = Parser.UNDECIDED
         else : result = Parser.FALSE 

      return result

   def Conjunction( self, index ):
      result = self.Factor(index)
      while self.la.kind == 8:
         self.Get( )
         r2 = self.Factor(index)
         if result == Parser.FALSE or r2 == Parser.FALSE : result = Parser.FALSE
         elif result == Parser.UNDECIDED or r2 == Parser.UNDECIDED : result = Parser.UNDECIDED
         else : result = Parser.TRUE 

      return result

   def Factor( self, index ):
      if index > self.maxfactor :
         self.maxfactor = index
      neg = False
      result = Parser.UNDEFINED 
      if (self.la.kind == 9):
         self.Get( )
         neg = True 
      if self.la.kind == 1:
         if self.la.val not in self.APdict :
            self.SemErr("unrecognized proposition") 
         self.Get( )
         if self.trace[index] & 1 << self.APdict[self.token.val] :
            result = Parser.TRUE
         else : result = Parser.FALSE 
      elif self.la.kind == 10:
         self.Get( )
         result = Parser.TRUE 
      elif self.la.kind == 11:
         self.Get( )
         result = Parser.FALSE 
      elif self.la.kind == 12:
         self.Get( )
         result = self.Property(index)
         self.Expect(13)
      else:
         self.SynErr(16)
      if neg : result = Parser.TRUE - result 
      return result



   def Parse( self, scanner ):
      self.scanner = scanner
      self.la = Token( )
      self.la.val = u''
      self.Get( )
      self.PyCheck()
      self.Expect(0)


   set = [
      [T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x],
      [x,T,x,x, x,x,x,x, x,T,T,T, T,x,x,x]

      ]

   errorMessages = {
      
      0 : "EOF expected",
      1 : "proposition expected",
      2 : "\"U\" expected",
      3 : "\"F\" expected",
      4 : "\"G\" expected",
      5 : "\"X\" expected",
      6 : "\"=>\" expected",
      7 : "\"or\" expected",
      8 : "\"and\" expected",
      9 : "\"not\" expected",
      10 : "\"true\" expected",
      11 : "\"false\" expected",
      12 : "\"(\" expected",
      13 : "\")\" expected",
      14 : "??? expected",
      15 : "invalid Property",
      16 : "invalid Factor",
      }