|
UsageCompiling Racc Grammar FileTo compile Racc grammar file, simply type: $ racc parse.yThis creates ruby script file "parse.tab.y". -o option changes this. Creating Parser with Racc
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. 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()".
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. re-distributing Racc runtimeA 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. |