This project is read-only.

Generated Java lexer/parser can't parse CSharp code.

May 9, 2012 at 11:03 PM
Edited May 9, 2012 at 11:08 PM

Hi,

At the moment I'm trying to build a Java lexer/parser combination using the cs.g file found in the code repository.
I filtered out all the csharp action lines and added some java action lines, but these are just assigning some values to variables.
In my unit tests I'm trying to feed the parser the following string:

_source = "using System;\n" +
                "using System.Collections.Generic;\n" +
                "namespace Foo.Bar {\n" +
                "public class TestClass {\n" +
                "private List<String> _testList;\n" +
                "protected String _testString = \"FooBar\";\n" +
                "public String Test { get; set; }\n" +
                "public TestClass() {\n" +
                "    _testList = new List<String>();\n" +
                "}\n" +
                "public void AddString(String testString) {\n" +
                "    String localVar = \"Test\";\n" +
                "    String localVar2 = \"Test\";\n" +
                "    _testList.Add(testString);\n" +
                "}\n" +
                "public override String ToString() {\n" +
                "    String localVar = \"Test\";\n" +
                "    return \"Hello world!\";\n" +
                "}\n" +
                "}\n" +
                "}\n";

 

But i get the following output:
line 1:0 no viable alternative at input 'using'
line 4:6 no viable alternative at input ' '
line 4:12 no viable alternative at input ' '
line 4:24 mismatched input '\n' expecting '}'
line 5:7 no viable alternative at input ' '
line 6:9 no viable alternative at input ' '
line 7:6 no viable alternative at input ' '
line 8:6 no viable alternative at input ' '
line 9:16 no viable alternative at input ' '
line 11:6 no viable alternative at input ' '
line 16:6 no viable alternative at input ' '
line 16:15 no viable alternative at input '

Does anyone know what might cause this?

PS: I've used ANTLRWorks 1.4.3 to generate the lexer and parser code.

Thanks in advance.

Regards,

Robert.

Coordinator
May 10, 2012 at 12:37 AM

What rule are you running?  You want to be using compilation_unit()

May 10, 2012 at 9:55 AM

Hi anbrad,

Thanks for your reply.

I'm running it like this:

_lexer.setCharStream(new ANTLRStringStream(source));
_parser.setTokenStream(new CommonTokenStream(_lexer));
_lexer.reset();
		
try {
	_parser.compilation_unit();
}
catch (RecognitionException e) {
	throw new ParseException(e);
}

Robert.

May 10, 2012 at 2:57 PM
Edited May 10, 2012 at 2:57 PM

I removed the csharp code from the cs.g file. It seems this messed it up, looking at it more closely it seems that it needs this code to function properly.
I've translated the csharp code to it's Java equivalent and now it seems to work properly.

Thanks for your time.

Robert.