module Racc
Racc is an LALR(1) parser generator. It is written in Ruby itself, and generates Ruby programs.
Command-line Reference
racc [-o<var>filename</var>] [--output-file=<var>filename</var>]
[-e<var>rubypath</var>] [--executable=<var>rubypath</var>]
[-v] [--verbose]
[-O<var>filename</var>] [--log-file=<var>filename</var>]
[-g] [--debug]
[-E] [--embedded]
[-l] [--no-line-convert]
[-c] [--line-convert-all]
[-a] [--no-omit-actions]
[-C] [--check-only]
[-S] [--output-status]
[--version] [--copyright] [--help] <var>grammarfile</var>
grammarfile-
Raccgrammar file. Any extension is permitted. - -o+outfile+, âoutput-file=
outfile -
A filename for output. default is <
filename>.tab.rb - -O+filename+, âlog-file=
filename -
Place logging output in file
filename. Default log file name is <filename>.output. - -e+rubypath+, âexecutable=
rubypath -
output executable file(mode 755). where
pathis the Ruby interpreter. - -v, âverbose
-
verbose mode. create
filename.output file, like yaccâs y.output file. - -g, âdebug
-
add debug code to parser class. To display debugging information, use this â-gâ option and set @yydebug true in parser class.
- -E, âembedded
-
Output parser which doesnât need runtime files (racc/parser.rb).
- -F, âfrozen
-
Output parser which declares frozen_string_literals: true
- -C, âcheck-only
-
Check syntax of racc grammar file and quit.
- -S, âoutput-status
-
Print messages time to time while compiling.
- -l, âno-line-convert
-
turns off line number converting.
- -c, âline-convert-all
-
Convert line number of actions, inner, header and footer.
- -a, âno-omit-actions
-
Call all actions, even if an action is empty.
- âversion
-
print
Raccversion and quit. - âcopyright
-
Print copyright and quit.
- âhelp
-
Print usage and quit.
Generating Parser Using Racc
To compile Racc grammar file, simply type:
$ racc parse.y
This creates Ruby script file âparse.tab.yâ. The -o option can change the output filename.
Writing A Racc Grammar File
If you want your own parser, you have to write a grammar file. A grammar file contains the name of your parser class, grammar for the parser, user code, and anything else. When writing a grammar file, yaccâs knowledge is helpful. If you have not used yacc before, Racc is not too difficult.
Hereâs an example Racc grammar file.
class Calcparser
rule
target: exp { print val[0] }
exp: exp '+' exp
| exp '*' exp
| '(' exp ')'
| NUMBER
end
Racc grammar files resemble yacc files. But (of course), this is Ruby code. yaccâs $$ is the âresultâ, $0, $1⌠is an array called âvalâ, and $-1, $-2⌠is an array called â_valuesâ.
See the Grammar File Reference for more information on grammar files.
Parser
Then you must prepare the parse entry method. There are two types of parse methods in Racc, Racc::Parser#do_parse and Racc::Parser#yyparse
Racc::Parser#do_parse is simple.
Itâs yyparse() of yacc, and Racc::Parser#next_token is yylex(). This method must returns an array like [TOKENSYMBOL, ITS_VALUE]. EOF is [false, false]. (TOKENSYMBOL is a Ruby symbol (taken from String#intern) by default. If you want to change this, see the grammar reference.
Racc::Parser#yyparse is little complicated, but useful. It does not use Racc::Parser#next_token, instead 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+.
Debugging
When debugging, â-vâ or/and the â-gâ option is helpful.
â-vâ creates verbose log file (.output). â-gâ creates a âVerbose Parserâ. Verbose Parser prints the internal status when parsing. But itâs not automatic. You must use -g option and set +@yydebug+ to true in order to get output. -g option only creates the verbose parser.
Racc reported syntax error.
Isnât there too many âendâ? grammar of racc file is changed in v0.10.
Racc does not use â%â mark, while yacc uses huge number of â%â marks..
Racc reported âXXXX conflictsâ.
Try âracc -v xxxx.yâ. It causes producing raccâs internal log file, xxxx.output.
Generated parsers does not work correctly
Try âracc -g xxxx.yâ. This command let racc generate âdebugging parserâ. Then set @yydebug=true in your parser. It produces a working log of your parser.
Re-distributing Racc runtime
A parser, which is created by Racc, requires the Racc runtime module; racc/parser.rb.
Ruby 1.8.x comes with Racc runtime module, you need NOT distribute Racc runtime files.
If you want to include the Racc runtime module with your parser. This can be done by using â-Eâ option:
$ racc -E -omyparser.rb myparser.y
This command creates myparser.rb which âincludesâ Racc runtime. Only you must do is to distribute your parser file (myparser.rb).
Note: parser.rb is ruby license, but your parser is not. Your own parser is completely yours.
Racc is an LALR(1) parser generator. It is written in Ruby itself, and generates Ruby programs.
Command-line Reference
racc [-o<var>filename</var>] [--output-file=<var>filename</var>]
[-e<var>rubypath</var>] [--executable=<var>rubypath</var>]
[-v] [--verbose]
[-O<var>filename</var>] [--log-file=<var>filename</var>]
[-g] [--debug]
[-E] [--embedded]
[-l] [--no-line-convert]
[-c] [--line-convert-all]
[-a] [--no-omit-actions]
[-C] [--check-only]
[-S] [--output-status]
[--version] [--copyright] [--help] <var>grammarfile</var>
grammarfile-
Raccgrammar file. Any extension is permitted. - -o+outfile+, âoutput-file=
outfile -
A filename for output. default is <
filename>.tab.rb - -O+filename+, âlog-file=
filename -
Place logging output in file
filename. Default log file name is <filename>.output. - -e+rubypath+, âexecutable=
rubypath -
output executable file(mode 755). where
pathis the Ruby interpreter. - -v, âverbose
-
verbose mode. create
filename.output file, like yaccâs y.output file. - -g, âdebug
-
add debug code to parser class. To display debugging information, use this â-gâ option and set @yydebug true in parser class.
- -E, âembedded
-
Output parser which doesnât need runtime files (racc/parser.rb).
- -F, âfrozen
-
Output parser which declares frozen_string_literals: true
- -C, âcheck-only
-
Check syntax of racc grammar file and quit.
- -S, âoutput-status
-
Print messages time to time while compiling.
- -l, âno-line-convert
-
turns off line number converting.
- -c, âline-convert-all
-
Convert line number of actions, inner, header and footer.
- -a, âno-omit-actions
-
Call all actions, even if an action is empty.
- âversion
-
print
Raccversion and quit. - âcopyright
-
Print copyright and quit.
- âhelp
-
Print usage and quit.
Generating Parser Using Racc
To compile Racc grammar file, simply type:
$ racc parse.y
This creates Ruby script file âparse.tab.yâ. The -o option can change the output filename.
Writing A Racc Grammar File
If you want your own parser, you have to write a grammar file. A grammar file contains the name of your parser class, grammar for the parser, user code, and anything else. When writing a grammar file, yaccâs knowledge is helpful. If you have not used yacc before, Racc is not too difficult.
Hereâs an example Racc grammar file.
class Calcparser
rule
target: exp { print val[0] }
exp: exp '+' exp
| exp '*' exp
| '(' exp ')'
| NUMBER
end
Racc grammar files resemble yacc files. But (of course), this is Ruby code. yaccâs $$ is the âresultâ, $0, $1⌠is an array called âvalâ, and $-1, $-2⌠is an array called â_valuesâ.
See the Grammar File Reference for more information on grammar files.
Parser
Then you must prepare the parse entry method. There are two types of parse methods in Racc, Racc::Parser#do_parse and Racc::Parser#yyparse
Racc::Parser#do_parse is simple.
Itâs yyparse() of yacc, and Racc::Parser#next_token is yylex(). This method must returns an array like [TOKENSYMBOL, ITS_VALUE]. EOF is [false, false]. (TOKENSYMBOL is a Ruby symbol (taken from String#intern) by default. If you want to change this, see the grammar reference.
Racc::Parser#yyparse is little complicated, but useful. It does not use Racc::Parser#next_token, instead 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+.
Debugging
When debugging, â-vâ or/and the â-gâ option is helpful.
â-vâ creates verbose log file (.output). â-gâ creates a âVerbose Parserâ. Verbose Parser prints the internal status when parsing. But itâs not automatic. You must use -g option and set +@yydebug+ to true in order to get output. -g option only creates the verbose parser.
Racc reported syntax error.
Isnât there too many âendâ? grammar of racc file is changed in v0.10.
Racc does not use â%â mark, while yacc uses huge number of â%â marks..
Racc reported âXXXX conflictsâ.
Try âracc -v xxxx.yâ. It causes producing raccâs internal log file, xxxx.output.
Generated parsers does not work correctly
Try âracc -g xxxx.yâ. This command let racc generate âdebugging parserâ. Then set @yydebug=true in your parser. It produces a working log of your parser.
Re-distributing Racc runtime
A parser, which is created by Racc, requires the Racc runtime module; racc/parser.rb.
Ruby 1.8.x comes with Racc runtime module, you need NOT distribute Racc runtime files.
If you want to include the Racc runtime module with your parser. This can be done by using â-Eâ option:
$ racc -E -omyparser.rb myparser.y
This command creates myparser.rb which âincludesâ Racc runtime. Only you must do is to distribute your parser file (myparser.rb).
Note: parser.rb is ruby license, but your parser is not. Your own parser is completely yours.
Constants
- Copyright
- VERSION
- Version