To compile Racc grammar file, simply type:
$ racc parse.yThis creates ruby script file "parse.tab.y". -o option changes this.
If you want your own parser, you have to write grammar file.
A grammar file contains name of parser class, grammar the parser can parse,
user code, and any.
When writing grammar file, yacc's knowledge is helpful.
If you have not use yacc, also racc is too difficult.
Here's example of Racc grammar file.
class Calcparser rule target: exp { print val[0] } exp: exp '+' exp | exp '*' exp | '(' exp ')' | NUMBER endRacc grammar 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'.
Then you must prepare parse entry method. There's two types of
racc's parse method,
"do_parse()" and
"yyparse()".
"do_parse()" is simple. it is yyparse() of yacc, and "next_token()" is
yylex(). This method must returns an array like [TOKENSYMBOL, ITS_VALUE].
EOF is [false, false].
(token symbol is ruby symbol (got by String#intern) as default.
If you want to change this, see grammar reference.
"yyparse()" is little complecated, but useful. It does not use "next_token()",
it gets tokens from any iterator. For example, "yyparse(obj, :scan)" causes
calling obj#scan, and you can return tokens by yielding them from obj#scan.
When debug, "-v" or/and "-g" option is helpful. "-v" causes creating verbose log file (.output). "-g" causes creating "Verbose Parser". Verbose Parser prints internal status when parsing. But it is not automatic. You must use -g option and set @yydebug true to get output. -g option only creates verbose parser.
A parser, which is created by Racc, requires Racc runtime. You should re-distribute Racc runtime with your parser. It can be done by using '-E' option.
racc -E myparser.ycreates myparser.tab.rb which includes racc runtime. Only you must do is to distribute parser file (.rb).
Note: parser.rb is LGPL but your parser is not. You own parser is completely yours.
Copyright (c) 1999-2001 Minero Aoki <aamine@loveruby.net>