To compile racc grammer file, simply type:
$ racc parse.yThis create ruby script file "parse.tab.y". -o option changes this.
If you want your own parser, you have to write grammer file.
A grammer file contains name of parser class, grammer the parser can parse,
user code, and any.
When writing grammer file, yacc's knowledge is helpful.
If you have not use yacc, also racc is too difficult.
Here's example of Racc grammer file.
class Calcparser rule target: exp { print val[0] } ; exp: exp '+' exp | exp '*' exp | '(' exp ')' | NUMBER ; endRacc grammer file is resembles to yacc file. But (of cource), action is Ruby code. yacc's $$ is 'result', $0, $1... is an array 'val', $-1, $-2... is an array '_values'.
yylex() is method "next_token()". This method must returns an array like [TOKENSIMBOL, ITS_VALUE]. EOF is [false, false]. (token simbol is ruby simbol (got by String#intern) as default. If you want to change this, see grammer reference.
yyparse() is method "do_parse()".
When debug, "-v" or/and "-g" option is helpful.
"-v" causes creating verbose output file (.output).
"-g" causes creating "Verbose Parser".
Verbose Parser prints internal status when parsing.
But, attention please! it is not automatic.
You must use -g option and be @yydebug true to get output.
-g option only create verbose parser.
A parser, which is created by Racc, requires Racc runtime. You should re-distribute Racc runtime with your parser, or users need to install Racc.
A Ruby script "rtpack.rb" helps you to include Racc runtime into your own package. Usage of pack.rb is:
/usr/src/racc-xxx/ $ ruby pack.rb TARGET/This copies Racc runtime under directory TARGET/ . (attention: pack.rb will clears TARGET/)
And the way to install Racc runtime when your installation is:
cd TARGET/ ruby setup.rb