我(现在)试图使用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.g4I 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 SEMIWhat 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.
更多推荐
发布评论