当我尝试用ANTLR4生成C#时会出现这些奇怪的错误是什么?(What are these odd errors that occur when I attempt to generate C# wi

编程入门 行业动态 更新时间:2024-10-25 22:30:55
当我尝试用ANTLR4生成C#时会出现这些奇怪的错误是什么?(What are these odd errors that occur when I attempt to generate C# with ANTLR4?)

我(现在)试图使用ANTLR4和C#来设计一种语言,到目前为止我一直在摆弄它。 在这个过程中,我决定尝试创建一个简单的数学表达式求值器。 在这个过程中,我为它创建了以下ANTLR语法:

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) ;
 

当我尝试使用此命令从它生成C#代码时:

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

我得到这些奇怪的错误:

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

什么可能会导致这些错误,我该如何解决它们? 我现在最好的猜想是,Visual Studio将奇数字符插入到文件的开头,并且我无法删除它们。

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.

最满意答案

今天不是一个好日子。

Visual Studio决定与我混淆并将我的所有文件的文件格式更改为UTF-8。 我需要做的就是进入File > Advanced Save Settings ,并将编码更改为US-ASCII。 这删除了开头插入的奇怪字符,并解决了(大部分)我的问题。

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.

更多推荐

本文发布于:2023-08-07 11:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当我   时会   奇怪   错误   attempt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!