...
Early prototyping phase. A simple lexer is Simple lexer and parser are working.
Progress
Here's a simple example. Note that everything is still subject to change.
...
Code Block |
---|
lexer grammar T2;
options { language = Perl5; }
ID : ('a'..'z'\|'A'..'Z')\+ ;
INT : '0'..'9'\+ ;
NEWLINE:'\r'? '\n' { $self->skip(); } ;
WS : (' '\|'\t')\+ { $channel = HIDDEN; } ;
|
Code Block |
---|
$ perl t.pl
text: Hello
type: 4
pos: 1:0
channel: 0
token index: -1
text:
type: 7
pos: 1:5
channel: 99
token index: -1
text: World
type: 4
pos: 1:6
channel: 0
token index: -1
line 1:11 no viable alternative at character '!'
text: 42
type: 5
pos: 2:0
channel: 0
token index: -1
|
2007-06-26
+ Simple Parser is working
Quick, what is 2 + 2? If you can't remember here's an easy way to find out. First we need a grammar.
Code Block |
---|
grammar MExpr;
options {
language = Perl5;
}
prog: stat+ ;
stat: expr NEWLINE { print "$expr.value\n"; }
| NEWLINE
;
expr returns [value]
: e=atom { $value = $e.value; }
( '+' e=atom { $value += $e.value; }
| '-' e=atom { $value -= $e.value; }
)*
;
atom returns [value]
: INT { $value = $INT.text; }
| '(' expr ')' { $value = $expr.value; }
;
ID : ('a'..'z'|'A'..'Z')+ ;
INT : '0'..'9'+ ;
NEWLINE:'\r'? '\n' ;
WS : (' '|'\t')+ { \$self->skip(); } ;
|
And here's the test program.
Code Block |
---|
#!/usr/bin/perl
use strict;
use warnings;
use ANTLR::Runtime::ANTLRStringStream;
use ANTLR::Runtime::CommonTokenStream;
use MExprLexer;
use MExprParser;
while (<>) {
my $input = ANTLR::Runtime::ANTLRStringStream->new($_);
my $lexer = MExprLexer->new($input);
my $tokens = ANTLR::Runtime::CommonTokenStream->new({ token_source => $lexer });
my $parser = MExprParser->new($tokens);
$parser->prog();
}
|
Finally we're getting to the answer.
Code Block |
---|
$ perl t.pl
2 + 2
4
|
Author
Ronald Blaschke (ron at rblasch org)