Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are these odd errors that occur when I attempt to generate C# with ANTLR4?

I'm (now) trying to use ANTLR4 and C# to design a language, and so far I've been fiddling around with it. In the process, I decided to try and create a simple mathematical expression evaluator. In the process, I created the following ANTLR grammar for it:

grammar Calculator;

@parser::members
{
    protected const int EOF = Eof;
}

@lexer::members
{
    protected const int EOF = EOF;
    protected const int HIDDEN = Hidden;
}

program : expr+ ;

expr : expr op=('*' | '/') expr
     | expr op=('+' | '-') expr
     | INT
     | '(' expression ')'
     ;

INT : [0-9]+ ;
MUL : '*' ;
DIV : '/' ;
ADD : '+' ;
SUB : '-' ;
WS : (' ' | '\r' | '\n') -> channel(HIDDEN) ;

When I try to generate C# code from it using this command:

java -jar C:\...\antlr-4.2-complete.jar -DLanguage=CSharp .\...\Grammar.g4

I get these odd errors:

error(50): C:\Users\Ethan\Documents\Visual Studio 2015\Projects\CypressLang\CypressLang\Source\.\Grammar\CypressGrammar.g4:1:0: syntax error: 'ï' came as a complete surprise to me    
error(50): C:\Users\Ethan\Documents\Visual Studio 2015\Projects\CypressLang\CypressLang\Source\.\Grammar\CypressGrammar.g4:1:1: syntax error: '»' came as a complete surprise to me    
error(50): C:\Users\Ethan\Documents\Visual Studio 2015\Projects\CypressLang\CypressLang\Source\.\Grammar\CypressGrammar.g4:1:2: syntax error: '¿' came as a complete surprise to me  
error(50): C:\Users\Ethan\Documents\Visual Studio 2015\Projects\CypressLang\CypressLang\Source\.\Grammar\CypressGrammar.g4:1:3: syntax error: mismatched input 'grammar' expecting SEMI

What might be causing these errors, and how can I fix them? My best guess at the moment is that Visual Studio is inserting odd characters onto the beginning of the file, and I can't remove them.

like image 820
Ethan Bierlein Avatar asked Oct 31 '15 00:10

Ethan Bierlein


1 Answers

Today is not a good day.

Visual Studio decided to mess with me and change my file formats to UTF-8 for all of my files. All I needed to do was go to File > Advanced Save Settings, and change the encoding to US-ASCII. This removed the odd characters inserted at the beginning, and solved (most) of my problems.

like image 73
Ethan Bierlein Avatar answered Oct 13 '22 12:10

Ethan Bierlein