10.02.2013 Views

Foundations of Software - LAMP - EPFL

Foundations of Software - LAMP - EPFL

Foundations of Software - LAMP - EPFL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The Alternative Combinator<br />

� The alternative combinator P | Q succeeds if either P or Q<br />

succeeds.<br />

� It returns the result <strong>of</strong> P if P succeeds, or the result <strong>of</strong> Q, if Q<br />

succeeds.<br />

� The alternative combinator is implemented as a method <strong>of</strong> class<br />

Parser.<br />

def | (q : ⇒ Parser[T]) = new Parser[T] {<br />

def apply(in : Input) = Parser.this(in) match {<br />

case s1 @ Success( , ) ⇒ s1<br />

case failure ⇒ q(in)<br />

}<br />

}<br />

39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!