fact_parser.pl: Script used to factorize parser.nit
Makefile: Update grammar and generate .nit files
- nit.sablecc3xx: Extended sablecc3 grammar (see prescc.sh)
- prescc.sh: Program to transform an extended sablecc3 to a standard one
+ nit.sablecc3xx: Extended sablecc3 grammar (see prescc.pl)
+ prescc.pl: Program to transform an extended sablecc3 to a standard one
parser_nodes.nit: token and nodes classes hierarchy used by the parser and the lexer
tables.nit, tables_nit.h: Interfaces to access the tables needed by the parser and the lexer
test_parser.nit:
+#!/usr/bin/perl -w
# This file is part of NIT ( http://www.nitlanguage.org ).
#
# Copyright 2009 Jean Privat <jean@pryen.org>
#
# Limitations
#
-# prescc is badly implemented with shell, sed and perl and is not robust.
+# prescc is badly implemented in perl and is not robust.
# Users must remember the following:
# * parametrized productions MUST be terminated with a line containing only a single semicolon (;)
# * parameters (~) and guards (!) in alternatives MUST correspond to a parameter of the enclosing production
# nop
# ;
+while (<>) {
+ push @lines, $_;
+}
+$lines = join "", @lines;
-case $# in
- 2);;
- *) echo "Usage: prescc infile outfile"; exit
-esac
+# List all the available parameters in the extended grammar
+@params = ();
+while ($lines =~ /\~([a-zA-Z]+)/g) {
+ if (!$found{$1}) {
+ push @params, $1;
+ $found{$1}=1;
+ }
+}
-
-infile=$1
-outfile=$2
-tmpfile=`mktemp "$2.XXXXXX"`
-
-printf "/* This file is autogenerated, do not modify it */" > "$outfile"
-cat "$infile" >> "$outfile"
-
-# The perl code is used to list all the available parameters in the extended grammar
-for token in `perl -ne 'while (/\~([a-zA-Z]+)/g) {if (!$found{$1}) {print "$1\n"; $found{$1}=1}}' "$infile"`
-do
- echo "Parameter ~$token"
- # first, sed starts from first line to the AST line and removes ~xxx and !xxx
- sed -n -e "
- 1,/^Abstract Syntax Tree/{
- /^Abstract Syntax Tree/b
- s/[\~!]$token//g
- p
- }
- " "$outfile" > "$tmpfile"
+$ast = "Abstract Syntax Tree";
+@res = ();
+for $token (@params) {
+ print STDERR "Parameter ~$token\n";
+ #push @res, "//Start part $token\n";
+# first, sed starts from first line to the AST line and removes ~xxx and !xxx
+ for $l (@lines) {
+ $_ = $l;
+ last if (/^$ast/);
+ s/[~!]$token//g;
+ push @res, $_;
+ }
+ #push @res, "//Generated part $token\n";
# second, sed clones ~xxx parametrized productions, substitute ~xxx with _xxx and delete !xxx lines
- sed -n -e "
- /\~$token/,/;/{
- s/\~$token/_$token/g
- /!$token/d
- p
- }
- " "$outfile" >> "$tmpfile"
- # third, sed continues fron AST line to last line and remove ~xxx and !xxx
- sed -n -e "
- /^Abstract Syntax Tree/,\${
- s/[\~!]$token//g
- p
- }
- " "$outfile" >> "$tmpfile"
- mv "$tmpfile" "$outfile"
-done
+ $into = 0;
+ for $l (@lines) {
+ $_ = $l;
+ $into = 1 if (/~$token/);
+ next if (!$into);
+ s/~$token/_$token/g;
+ next if /!$token/;
+ push @res, $_;
+ $into = 0 if (/;/);
+ }
+ #push @res, "//End of generated part $token\n";
+ # third, sed continues fron AST line to last line and remove ~xxx and !xxx
+ $into = 0;
+ for (@lines) {
+ $into = 1 if (/^$ast/);
+ next if (!$into);
+ push @res, $_;
+ }
+ #push @res, "//End part $token\n";
+ @lines = @res;
+ @res = ();
+}
+print "/* This file is autogenerated, do not modify it */";
+print (join "", @lines);