antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.12k stars 3.69k forks source link

CSharp example at http://lab.antlr.org/ is broken #3412

Open abulka opened 1 year ago

abulka commented 1 year ago

The CSharp example at http://lab.antlr.org/ is broken

Tool console
No such start rule: compilation_unit

P.S. What is the state of the Charp grammer these days? Is it possible to get a working parser that handles generics etc. I was able to generate a C# parser 4 or so years ago, but today I can't even compile the generated java files - getting thousands of error messages instead. Perhaps I'm compiling it wrong?

antlr4 CSharpLexer.g4
antlr4 CSharpParser.g4
antlr4 CSharpPreprocessorParser.g4

works, but

javac CSharp*.java

generates errors

  javac CSharpPreprocessorParser.java - Click to expand ``` % javac CSharpPreprocessorParser.java CSharpPreprocessorParser.java:12: error: cannot find symbol public class CSharpPreprocessorParser extends CSharpPreprocessorParserBase { ^ symbol: class CSharpPreprocessorParserBase CSharpPreprocessorParser.java:155: error: method does not override or implement a method from a supertype @Override ^ CSharpPreprocessorParser.java:161: error: method does not override or implement a method from a supertype @Override ^ CSharpPreprocessorParser.java:167: error: method does not override or implement a method from a supertype @Override ^ CSharpPreprocessorParser.java:170: error: method does not override or implement a method from a supertype @Override ^ CSharpPreprocessorParser.java:173: error: method does not override or implement a method from a supertype @Override ^ CSharpPreprocessorParser.java:176: error: method does not override or implement a method from a supertype @Override ^ CSharpPreprocessorParser.java:181: error: cannot find symbol _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); ^ symbol: variable _interp location: class CSharpPreprocessorParser CSharpPreprocessorParser.java:181: error: incompatible types: CSharpPreprocessorParser cannot be converted to Parser _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); ^ CSharpPreprocessorParser.java:331: error: cannot find symbol Preprocessor_directiveContext _localctx = new Preprocessor_directiveContext(_ctx, getState()); ^ symbol: variable _ctx location: class CSharpPreprocessorParser CSharpPreprocessorParser.java:331: error: cannot find symbol Preprocessor_directiveContext _localctx = new Preprocessor_directiveContext(_ctx, getState()); ^ symbol: method getState() location: class CSharpPreprocessorParser CSharpPreprocessorParser.java:332: error: cannot find symbol enterRule(_localctx, 0, RULE_preprocessor_directive); ^ symbol: method enterRule(Preprocessor_directiveContext,int,int) location: class CSharpPreprocessorParser CSharpPreprocessorParser.java:335: error: cannot find symbol setState(80); ^ symbol: method setState(int) location: class CSharpPreprocessorParser CSharpPreprocessorParser.java:336: error: cannot find symbol _errHandler.sync(this); ^ symbol: variable _errHandler location: class CSharpPreprocessorParser CSharpPreprocessorParser.java:337: error: cannot find symbol switch (_input.LA(1)) { ^ symbol: variable _input location: class CSharpPreprocessorParser CSharpPreprocessorParser.java:340: error: cannot find symbol enterOuterAlt(_localctx, 1); ^ symbol: method enterOuterAlt(Preprocessor_directiveContext,int) location: class CSharpPreprocessorParser CSharpPreprocessorParser.java:342: error: cannot find symbol setState(6); ^ symbol: method setState(int) location: class CSharpPreprocessorParser CSharpPreprocessorParser.java:343: error: cannot find symbol match(DEFINE); ... ... ```   
  javac CSharpLexer.java - Click to expand ``` % javac CSharpLexer.java CSharpLexer.java:12: error: cannot find symbol public class CSharpLexer extends CSharpLexerBase { ^ symbol: class CSharpLexerBase CSharpLexer.java:209: error: method does not override or implement a method from a supertype @Override ^ CSharpLexer.java:215: error: method does not override or implement a method from a supertype @Override ^ CSharpLexer.java:224: error: cannot find symbol _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); ^ symbol: variable _interp location: class CSharpLexer CSharpLexer.java:224: error: incompatible types: CSharpLexer cannot be converted to Lexer _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); ^ CSharpLexer.java:227: error: method does not override or implement a method from a supertype @Override ^ CSharpLexer.java:230: error: method does not override or implement a method from a supertype @Override ^ CSharpLexer.java:233: error: method does not override or implement a method from a supertype @Override ^ CSharpLexer.java:236: error: method does not override or implement a method from a supertype @Override ^ CSharpLexer.java:239: error: method does not override or implement a method from a supertype @Override ^ CSharpLexer.java:242: error: method does not override or implement a method from a supertype @Override ^ CSharpLexer.java:245: error: method does not override or implement a method from a supertype @Override ^ CSharpLexer.java:277: error: cannot find symbol this.OnInterpolatedRegularStringStart(); ^ symbol: method OnInterpolatedRegularStringStart() CSharpLexer.java:284: error: cannot find symbol this.OnInterpolatedVerbatiumStringStart(); ^ symbol: method OnInterpolatedVerbatiumStringStart() CSharpLexer.java:291: error: cannot find symbol this.OnOpenBrace(); ^ symbol: method OnOpenBrace() CSharpLexer.java:298: error: cannot find symbol this.OnCloseBrace(); ^ symbol: method OnCloseBrace() CSharpLexer.java:305: error: cannot find symbol this.OnColon(); ^ symbol: method OnColon() CSharpLexer.java:312: error: cannot find symbol this.OpenBraceInside(); ^ symbol: method OpenBraceInside() CSharpLexer.java:319: error: cannot find symbol this.OnDoubleQuoteInside(); ^ symbol: method OnDoubleQuoteInside() CSharpLexer.java:326: error: cannot find symbol this.OnCloseBraceInside(); ^ symbol: method OnCloseBraceInside() CSharpLexer.java:330: error: method does not override or implement a method from a supertype @Override ^ CSharpLexer.java:347: error: cannot find symbol return this.IsRegularCharInside() ; ^ symbol: method IsRegularCharInside() CSharpLexer.java:354: error: cannot find symbol return this.IsVerbatiumDoubleQuoteInside() ; ^ symbol: method IsVerbatiumDoubleQuoteInside() CSharpLexer.java:361: error: cannot find symbol return this.IsRegularCharInside() ; ^ symbol: method IsRegularCharInside() CSharpLexer.java:368: error: cannot find symbol return this.IsVerbatiumDoubleQuoteInside() ; ^ symbol: method IsVerbatiumDoubleQuoteInside() Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 25 errors ```   
msagca commented 1 year ago

Hi @abulka

http://lab.antlr.org/ uses the wrong parser grammar, it should be CSharpParser not CSharpPreprocessorParser.

I can successfully test the grammar.

alias antlr4='java org.antlr.v4.Tool'
alias grun='java org.antlr.v4.gui.TestRig'
cd ~/grammars-v4/csharp
cp Java/* .
antlr4 CSharpLexer.g4 CSharpParser.g4 CSharpPreprocessorParser.g4
javac *.java
grun CSharp compilation_unit examples/C2430.cs -tree
(compilation_unit (namespace_member_declarations (namespace_member_declaration (type_declaration (class_definition class (identifier foo) (class_body { }))))) <EOF>)
abulka commented 1 year ago

Brilliant, thanks. I was missing the

cp Java/* .

step which brings in those supporting java files.

kaby76 commented 1 year ago

Notes: