[antlr-interest] Newbie needs help with grammmar

Mikael Petterson mikael.petterson at ericsson.com
Wed Sep 10 07:22:38 PDT 2008


Hi,

I have the following parser in my grammar file:

error returns [ClearCaseElementState el]
	: {int exceptionCode = 0;}
	  {boolean throwException = false;}
	CLEARTOOL 'Error:' ('Entry' 'named' elem=STRING 'already'
'exists.' {throwException = true; exceptionCode |=
ClearCase.ERROR_ALREADY_ADDED;}
	| 'Element' elem=STRING 'has' 'checkouts.' {throwException =
true; exceptionCode |= ClearCase.ERROR_ALREADY_CHECKED_OUT;}
	| 'Element' elem=STRING 'is' 'already' 'checked' 'out' 'to' VIEW
STRING DOT {el = new ClearCaseElementState($elem.text,
ClearCase.IS_ELEMENT | ClearCase.ERROR_ALREADY_CHECKED_OUT);}
	| 'Can\u0027t' 'modify' 'directory' elem=STRING 'because' 'it'
'is' 'not' 'checked' 'out.' {throwException = true; exceptionCode |=
ClearCase.ERROR_DIR_IS_NOT_CHECKED_OUT;}
	| 'Unable' 'to' 'access' elem=STRING ':' 'No' 'such' 'file' 'or'
'directory.' {throwException = true; exceptionCode |=
ClearCase.ERROR_NOT_ACCESSIBLE;}
	| 'By' 'default' COMMA 'won\u0027t' 'create' 'version' 'with'
'data' 'identical' 'to' 'predecessor.' NEWLINE
	 CLEARTOOL 'Error:' 'Unable' 'to' 'check' 'in' elem=STRING DOT
{throwException = true; exceptionCode |=
ClearCase.ERROR_PREDECESSOR_IS_IDENTICAL;}
	| 'The' 'most' 'recent' 'version' 'on' 'branch' STRING 'is'
'not' 'the' 'predecessor' 'of' 'this' 'version.'NEWLINE	 CLEARTOOL
'Error:' 'Unable' 'to' 'check' 'in' elem=STRING DOT {throwException =
true; exceptionCode |=
ClearCase.ERROR_MOST_RECENT_NOT_PREDECESSOR_OF_THIS_VERSION;}
                | (LITERAL | STRING | DOT)+)
	{if (throwException) throw new ClearCaseException(exceptionCode,
new String[]{$elem.text}, null);}
	;

I have a junit test with the following lines:

public void testRecognitionIndeticalResourceCheckin() {
		StringBuffer cleartoolOutput = new StringBuffer();
		cleartoolOutput
				.append("cleartool: Error: By default,
won't create version with data identical to predecessor.\r\n"+
                                                            "cleartool:
Error: Unable to check in
\"M:\\eraonel_testa\\mbv_admin\\testarea\\productsample\\com\\sample\\pr
oduct\\ProductC.java\".\r\n");

		try {

			Reader reader = new
StringReader(cleartoolOutput.toString());
			CleartoolOutputLexer lexer = new
CleartoolOutputLexer(
					new ANTLRReaderStream(reader));
			CommonTokenStream cts = new
CommonTokenStream(lexer);
			CleartoolOutputParser parser = new
CleartoolOutputParser(cts);
			List elements = parser.main();
			ClearCaseElementState[] elArray =
(ClearCaseElementState[]) elements
					.toArray(new
ClearCaseElementState[elements.size()]);
			fail();
		} catch (ClearCaseException e){
			assertTrue(e.getErrorCode() ==
ClearCase.ERROR_PREDECESSOR_IS_IDENTICAL);
			
		} catch (IOException e) {
			// TODO handle catch block
			e.printStackTrace();
		} catch (RecognitionException e) {
			// TODO handle catch block
			e.printStackTrace();
		}
	}

This is the code that I am using:

protected ClearCaseElementState[] parserOutput(final String[] result)
			throws ClearCaseException {
		final StringBuffer display = new StringBuffer();
		for (int i = 0; i < result.length; i++) {
			if (i != 0) {
				display.append("\n");
			}
			display.append(result[i]);
		}
		LOG.finest("parsing:" + display.toString());
		ClearCaseElementState[] res = null;
		try {
			final Reader reader = new
StringReader(display.toString());
			final CleartoolOutputLexer lexer = new
CleartoolOutputLexer(
					new ANTLRReaderStream(reader));
			final CommonTokenStream cts = new
CommonTokenStream(lexer);
			final CleartoolOutputParser parser = new
CleartoolOutputParser(cts);
			final List elements = parser.main();
			res = (ClearCaseElementState[]) elements
					.toArray(new
ClearCaseElementState[elements.size()]);
		} catch (final IOException e) {
			// TODO handle catch block
			e.printStackTrace();
		} catch (final RecognitionException e) {
			// TODO handle catch block
			e.printStackTrace();
		}
		return res;
	}

This is the output that I get:

line 3:11 no viable alternative at character 'c'
line 10:8 mismatched character ' ' expecting set null
line 11:27 mismatched character '\r' expecting set null
BR.recoverFromMismatchedToken
line 13:28 mismatched input 'check' expecting 'access'

No exception will be thrown!

It seems like my parser is finding:

Unable' 'to' 'access' elem=STRING ':' 'No' 'such' 'file' 'or'
'directory.' {throwException = true; exceptionCode |=
ClearCase.ERROR_NOT_ACCESSIBLE;}

But the input that I have is:

cleartool: Error: Unable to check in
\"M:\\eraonel_testa\\mbv_admin\\testarea\\productsample\\com\\sample\\pr
oduct\\ProductC.java\"

How can I change my grammar?

cheers,

//mike - a newbie on this.



Mikael Petterson
Software Designer


Ericsson AB, Stockholm, Sweden
Visiting address: Isafjordsgatan 15, Kista
Phone: +46 70 2673044 
E-mail: mikael.petterson at ericsson.com 



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080910/57b55a4f/attachment.html 


More information about the antlr-interest mailing list