Subsecciones

La Calculadora

Uso desde Línea de Comandos

[~/src/PL/rexical/sample(master)]$ racc --help
Usage: racc [options] <input>
    -o, --output-file=PATH           output file name [<input>.tab.rb]
    -t, --debug                      Outputs debugging parser.
    -g                               Equivalent to -t (obsolete).
    -v, --verbose                    Creates <filename>.output log file.
    -O, --log-file=PATH              Log file name [<input>.output]
    -e, --executable [RUBYPATH]      Makes executable parser.
    -E, --embedded                   Embeds Racc runtime in output.
        --line-convert-all           Converts line numbers of user codes.
    -l, --no-line-convert            Never convert line numbers.
    -a, --no-omit-actions            Never omit actions.
        --superclass=CLASSNAME       Uses CLASSNAME instead of Racc::Parser.
        --runtime=FEATURE            Uses FEATURE instead of 'racc/parser'
    -C, --check-only                 Checks syntax and quit immediately.
    -S, --output-status              Outputs internal status time to time.
    -P                               Enables generator profile
    -D flags                         Flags for Racc debugging (do not use).
        --version                    Prints version and quit.
        --runtime-version            Prints runtime version and quit.
        --copyright                  Prints copyright and quit.
        --help                       Prints this message and quit.

[~/Dropbox/src/PL/rexical/sample(master)]$ cat -n Rakefile 
     1  task :default => %W{racc rex} do
     2    sh "ruby calc3.tab.rb"
     3  end
     4  
     5  task :racc do
     6    sh "racc calc3.racc"
     7  end
     8  
     9  task :rex do
    10    sh "rex calc3.rex"
    11  end

Análisis Léxico con rexical

[~/Dropbox/src/PL/rexical/sample(master)]$ cat -n calc3.rex
     1  #
     2  # calc3.rex
     3  # lexical scanner definition for rex
     4  #
     5  
     6  class Calculator3
     7  macro
     8    BLANK         \s+
     9    DIGIT         \d+
    10  rule
    11    {BLANK}
    12    {DIGIT}       { [:NUMBER, text.to_i] }
    13    .|\n          { [text, text] }
    14  inner
    15  end

Análisis Sintáctico

[~/Dropbox/src/PL/rexical/sample(master)]$ cat -n calc3.racc 
     1  #
     2  # A simple calculator, version 3.
     3  #
     4  
     5  class Calculator3
     6    prechigh
     7      nonassoc UMINUS
     8      left '*' '/'
     9      left '+' '-'
    10    preclow
    11    options no_result_var
    12  rule
    13    target  : exp
    14            | /* none */ { 0 }
    15  
    16    exp     : exp '+' exp { val[0] + val[2] }
    17            | exp '-' exp { val[0] - val[2] }
    18            | exp '*' exp { val[0] * val[2] }
    19            | exp '/' exp { val[0] / val[2] }
    20            | '(' exp ')' { val[1] }
    21            | '-' NUMBER  =UMINUS { -(val[1]) }
    22            | NUMBER
    23  end
    24  
    25  ---- header ----
    26  #
    27  # generated by racc
    28  #
    29  require 'calc3.rex'
    30  
    31  ---- inner ----
    32  
    33  ---- footer ----
    34  
    35  puts 'sample calc'
    36  puts '"q" to quit.'
    37  calc = Calculator3.new
    38  while true
    39    print '>>> '; $stdout.flush
    40    str = $stdin.gets.strip
    41    break if /q/i === str
    42    begin
    43      p calc.scan_str(str)
    44    rescue ParseError
    45      puts 'parse error'
    46    end
    47  end

Precedencias

right is yacc's %right, left is yacc's %left.

= SYMBOL means yacc's %prec SYMBOL:

prechigh
  nonassoc '++'
  left     '*' '/'
  left     '+' '-'
  right    '='
preclow

rule
  exp: exp '*' exp
     | exp '-' exp
     | '-' exp       =UMINUS   # equals to "%prec UMINUS"
         :
         :

Atributos

You can use following special local variables in action.

  1. result ($$)
    
    The value of left-hand side (lhs). A default value is val[0].

  2. val ($1,$2,$3...)
    
    An array of value of right-hand side (rhs).

  3. _values (...$-2,$-1,$0)
    
    A stack of values. DO NOT MODIFY this stack unless you know what you are doing.

Declaring Tokens

By declaring tokens, you can avoid bugs.

token NUM ID IF

Opciones

You can write options for racc command in your racc file.

options OPTION OPTION ...
Options are:

  1. omit_action_call
    
    omit empty action call or not.

  2. result_var
    
    use/does not use local variable "result"

You can use no_ prefix to invert its meanings.

User Code Block

"User Code Block" is a Ruby source code which is copied to output. There are three user code block, "header" "inner" and " footer".

Format of user code is like this:

---- header
  ruby statement
  ruby statement
  ruby statement

---- inner
  ruby statement
     :
     :
If four - exist on line head, racc treat it as beginning of user code block. A name of user code must be one word.

Casiano Rodríguez León
2016-03-27