[antlr-interest] Line delimted grammar
Harald M. Müller
harald_m_mueller at gmx.de
Tue Jan 1 09:25:20 PST 2008
Hi - here is a simple line-delimited grammar.
The only "interesting thing" is that a "statement" = "line" may end with an
NL (newline/end of line) AS WELL AS a COMMENT_TO_NL. This makes it easier to
write the COMMENT_TO_NL token - it munches up all chars including the
end-of-line chars; and hence can represent an end-of-line.
If you need the white-space also in your result (AST or whatever), the whole
thing is more work, but still easy - the "emtpy line" then will be about
line : WS (stmt WS)? WS? (COMMENT_TO_NL | NL);
Regards
Harald
grammar LineDelimited;
options {
language=CSharp;
output=AST;
}
@header {
#pragma warning disable 0219
}
file
: line* EOF!
;
line
: stmt? (COMMENT_TO_NL! | NL!)
;
stmt
: 'dir'^ filepattern
| 'for'^ VAR 'in'! '('! filepattern ')'! 'do'! stmt
| ID^ filepattern*
;
filepattern
: ( ID | '*' )
( '.'!
(ID | '*')
)?
;
COMMENT_TO_NL
: '#' ~('\r'|'\n')* NL
;
ID : ('a'..'z'|'0'..'9'|'_')+
;
VAR : '%' ID
;
WS : (' '|'\t')+ { $channel=HIDDEN; };
NL : '\r' '\n'
| '\r'
| '\n'
;
===================
using System;
using System.Collections.Generic;
using Antlr.Runtime;
using Antlr.Runtime.Tree;
namespace Interpreter {
class Program {
static void Main() {
ParseAndInterpret(@"
dir *.bak # only bak files
for %f in (*.bak) do del *.bak # now they are gone ...
dir *.bak
run *.*
");
Console.In.Read();
}
private static void ParseAndInterpret(string input) {
// Setup lexer and parser
ICharStream cs = new ANTLRStringStream(input);
LineDelimitedLexer lexer = new LineDelimitedLexer(cs);
CommonTokenStream ts = new CommonTokenStream(lexer);
LineDelimitedParser parser = new LineDelimitedParser(ts);
CommonTree tree = (CommonTree) parser.file().Tree;
Console.Out.WriteLine(tree.ToStringTree());
}
}
}
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Thad
> Sent: Monday, December 03, 2007 7:14 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Line delimted grammar
>
> For a certain project I am working on I need to parse a
> complex configuration file.
>
> Two propeties of the file are: it is line delimited, and
> blank lines are ignored.
>
> I tried to do this a while ago and encountered problems with
> the line delimitaion.
>
> Can someone recommend a simple antlr example for a line
> delimited language with blank lines ignored? ( It seems to be
> the combination of the two that causes me problems. )
>
> If you can't then can someone post a grammar for a simple
> language? Something like:
> each program is a list of statements, statements are one
> line, whitespace is ignored, blank lines are ignored,
> anything after a # is a comment, lines of the form
> letter=integer are statements?
>
> Thanks
> Thad
>
> PS.
> Is there some simple way to search the archives for old
> messages? I can help but think that there is something on
> line delimited grammars in there.
>
More information about the antlr-interest
mailing list