开发者

Problems with some grammar in ANTLR

I've been doing an excersise that consists on read from 开发者_开发技巧a text file and:

  • Replace consecutive spaces/tabs with only one space
  • Replace consecutive newline characters with only one
  • Make all the text upper-case.

When I execute it, it doesn't write anything to the output.txt file...

I've been trying to find the problem for a while without success. Here is my code:

grammar Ejerc1;
options
{
    language = CSharp3;
}
@header
{
using System.IO;
using System;
}
fragment Spaces     :   (' '|'\t')+ { $text = " "; };
fragment Any        :   (~(' '|'\t'|'\n'|'\r'))+ { $text = $text.ToUpper(); };
fragment NewLines   :   ('\r'|'\n')+ { $text = "\r\n"; };

/* Parser */
public file[string filePath]
@init {
if (File.Exists($filePath)) { 
    File.Delete($filePath); 
}
StreamWriter w = new StreamWriter($filePath);
}
@after {
w.Close();
}
:
(
 Spaces { w.Write($Spaces.text); }
|NewLines { w.Write($NewLines.text); }
|Any { w.Write($Any.text); }
)*
EOF;

and here is the code inside the Main method:

string inputPath = "text.txt";
string outputPath = "output.txt";
var input = new ANTLRFileStream(inputPath);
var lexer = new Ejerc1Lexer(input);

var tokens = new CommonTokenStream(lexer);
var parser = new Ejerc1Parser(tokens);
parser.file(outputPath);


Fragment rules can only be used from other lexer rules, not from parser rules, as you're trying to do. Simply remove the fragment keywords from your 3 lexer rules.

Also, the snippet:

$text = " ";

translates to the following (pseudo code):

getText() = " "

which is not valid (at least, when using the Java target it's invalid). You might want to try:

Spaces     :   (' '|'\t')+ { SetText(" "); };

instead. But the CSharp3 target might just accept it though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜