Skip to content

Instantly share code, notes, and snippets.

@rns
Created October 2, 2014 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rns/d9e24130722b98ab0db7 to your computer and use it in GitHub Desktop.
Save rns/d9e24130722b98ab0db7 to your computer and use it in GitHub Desktop.
Setting trace_terminals option
Setting trace_values option
Lexer "L0" accepted lexeme L1c1-5 e1: nstring; value="line1"
Lexer "L0" accepted lexeme L1c6 e2: newline; value="
"
Lexer "L0" accepted lexeme L2c1-4 e3: nstring; value="next"
Lexer "L0" accepted lexeme L2c5 e4: hspace; value=" "
Lexer "L0" accepted lexeme L2c6-10 e5: nstring; value="line2"
Lexer "L0" accepted lexeme L2c11 e6: newline; value="
"
Lexer "L0" accepted lexeme L3c1-18 e7: comment; value="// mycomment line3"
Lexer "L0" accepted lexeme L3c19 e8: newline; value="
"
Lexer "L0" accepted lexeme L4c1-5 e9: nstring; value="line4"
Lexer "L0" accepted lexeme L4c6 e10: hspace; value=" "
Lexer "L0" accepted lexeme L4c7-18 e11: comment; value="// mycomment"
Lexer "L0" accepted lexeme L4c19 e12: newline; value="
"
Lexer "L0" accepted lexeme L5c1-13 e13: nstring; value="5endcomment//"
Lexer "L0" accepted lexeme L5c14 e14: newline; value="
"
Lexer "L0" accepted lexeme L6c1-8 e15: nstring; value="+testtag"
Lexer "L0" accepted lexeme L6c1-8 e15: tag; value="+testtag"
Lexer "L0" accepted lexeme L6c9 e16: hspace; value=" "
Lexer "L0" accepted lexeme L6c10-14 e17: nstring; value="hello"
Lexer "L0" accepted lexeme L6c15 e18: newline; value="
"
Lexer "L0" accepted lexeme L7c1-13 e19: qstring; value=""quoted text""
Lexer "L0" accepted lexeme L7c14 e20: newline; value="
"
Lexer "L0" accepted lexeme L8c1-6 e21: nstring; value="#a_tag"
Lexer "L0" accepted lexeme L8c1-6 e21: tag; value="#a_tag"
Lexer "L0" accepted lexeme L8c7 e22: hspace; value=" "
Lexer "L0" accepted lexeme L8c8-25 e23: qstring; value=""more quoted text""
Lexer "L0" accepted lexeme L8c26 e24: hspace; value=" "
Lexer "L0" accepted lexeme L8c27-38 e25: nstring; value="#another_tag"
Lexer "L0" accepted lexeme L8c27-38 e25: tag; value="#another_tag"
Lexer "L0" accepted lexeme L8c39 e26: hspace; value=" "
Lexer "L0" accepted lexeme L8c40-55 e27: comment; value="// and a comment"
Lexer "L0" accepted lexeme L8c56 e28: newline; value="
"
---Reading input...
Input:
line1
next line2
// mycomment line3
line4 // mycomment
5endcomment//
+testtag hello
"quoted text"
#a_tag "more quoted text" #another_tag // and a comment
---Setting up parser...
---Parsing Input file with parser...
---Retrieving the tree...
NEW: $VAR1 = 'main';
Tree:
$VAR1 = [
'tasktokens',
[
'tasktoken',
[
'nstring',
'line1'
]
],
[
'tasktoken',
[
'newline',
'
'
]
],
[
'tasktoken',
[
'nstring',
'next'
]
],
[
'tasktoken',
[
'hspace',
' '
]
],
[
'tasktoken',
[
'nstring',
'line2'
]
],
[
'tasktoken',
[
'newline',
'
'
]
],
[
'tasktoken',
[
'comment',
'// mycomment line3'
]
],
[
'tasktoken',
[
'newline',
'
'
]
],
[
'tasktoken',
[
'nstring',
'line4'
]
],
[
'tasktoken',
[
'hspace',
' '
]
],
[
'tasktoken',
[
'comment',
'// mycomment'
]
],
[
'tasktoken',
[
'newline',
'
'
]
],
[
'tasktoken',
[
'nstring',
'5endcomment//'
]
],
[
'tasktoken',
[
'newline',
'
'
]
],
[
'tasktoken',
[
'tag',
'+testtag'
]
],
[
'tasktoken',
[
'hspace',
' '
]
],
[
'tasktoken',
[
'nstring',
'hello'
]
],
[
'tasktoken',
[
'newline',
'
'
]
],
[
'tasktoken',
[
'qstring',
'"quoted text"'
]
],
[
'tasktoken',
[
'newline',
'
'
]
],
[
'tasktoken',
[
'tag',
'#a_tag'
]
],
[
'tasktoken',
[
'hspace',
' '
]
],
[
'tasktoken',
[
'qstring',
'"more quoted text"'
]
],
[
'tasktoken',
[
'hspace',
' '
]
],
[
'tasktoken',
[
'tag',
'#another_tag'
]
],
[
'tasktoken',
[
'hspace',
' '
]
],
[
'tasktoken',
[
'comment',
'// and a comment'
]
],
[
'tasktoken',
[
'newline',
'
'
]
]
];
---DONE;
Tool completed successfully
#!/usr/bin/env perl
use strict;
use warnings;
use Marpa::R2;
use File::Slurp 'read_file';
use Data::Dumper;
$Data::Dumper::Deepcopy = 1;
my $input = <<"INPUT";
line1
next line2
// mycomment line3
line4 // mycomment
5endcomment//
+testtag hello
"quoted text"
#a_tag "more quoted text" #another_tag // and a comment
INPUT
my $grammar = << 'GRAMMAR';
:default ::= action => [name, values]
lexeme default = action => [ name, value ] latm => 1
:start ::= tasktokens
tasktokens ::= tasktoken +
tasktoken ::= comment
| tag
| qstring
| nstring
| hspace
| newline
comment ~ '//' comment_body
comment_body ~ comment_body_char*
comment_body_char ~ [^\n\r]
qstring ~ quote in_string quote
quote ~ ["]
in_string ~ in_string_char*
in_string_char ~ [^"] | '\"'
tag ~ tagstart tag_body
tag_body ~ [^\h]+
tagstart ~ [+#@]
nstring ~ [^\h\n\r]+
hspace ~ [\h]+
newline ~ [\n\r]
#word ~ [\w\d\-\_]+
GRAMMAR
my $g = Marpa::R2::Scanless::G->new({
action_object => 'main',
source => \( $grammar )
});
print "---Reading input...\n";
#my $string = read_file('rtask-minimal.txt');
print "Input:\n$input";
my $string = $input;
print "---Setting up parser...\n";
my $r = Marpa::R2::Scanless::R->new({
grammar => $g,
trace_terminals => 1,
trace_values => 1
});
print "---Parsing Input file with parser...\n";
$r->read(\$string);
print "---Retrieving the tree...\n";
my $tree = ${$r->value};
print "Tree:\n";
print Dumper($tree);
print "---DONE;\n\n";
sub new {
print "NEW: ".Dumper(@_);
}
sub do_nothing { return; }
sub do_list_line { print "LINE: ".Dumper(@_); }
sub do_list_comment { print "comment: ".Dumper(@_); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment