feed

Feb 2008 Example Grammar by Chris Poirier • in Discussions, Generalpermalink

I don’t have time to post a full article tonight, but I’d like to get something up for you all to look at.  So here is a sample RCC grammar file to look at.  It is, in fact, the RCC description of its own grammar language.  I’ll break it down in an article tomorrow.

[UPDATE]: I’ve corrected a couple of typos and introduced a couple of new efficiencies since I posted this last night.  It’s amazing what posting something in public does for your perspective.



grammar RCC

   start_rule system_spec
   ignore     whitespace
   ignore     comment
   enable     backtracking

   strings
      any_character     => [\u0000-\uFFFF]
      digit             => [0-9]
      hex_digit         => [{digit}a-fA-F]

      unicode_sequence  => '\\' 'u' hex_digit hex_digit hex_digit hex_digit
      escape_sequence   => '\\' [a-z\\\-\[\]\']
      general_character => [{any_character}]-['\n\r\\]
      general_text      => general_character+
      property_text     => [{general_character}]-[}]+

      word_first_char   => [a-zA-Z_]
      word_char         => [{word_first_char}{digit}]
      word              => word_first_char word_char*

      eol               => '\n'
      whitespace        => [ \t\r]+
      comment           => '#' [{any_character}]-[\n]*
   end

   macros
      statement         => %% eol+ ;
      block( header )   => statement() [ statement() [$header] %% 'end' ]
   end


   section grammar
      system_spec  => grammar_spec+
      grammar_spec => block('grammar' word:name) [ option* specification* ]

      group option
         start_rule          => statement() [ 'start_rule' word:rule_name        ]
         ignore_switch       => statement() [ 'ignore'     word:name             ]
         backtracking_switch => statement() [ 'enable'     'backtracking'        ]
         pluralization_guide => statement() [ 'pluralize'  word:name word:plural ]
      end

      group specification
         macros_spec     => block('macros')              [ macro_spec*           ]
         strings_spec    => block('strings')             [ string_spec*          ]
         precedence_spec => block('precedence')          [ precedence_level*     ]
         section_spec    => block('section' word:name)   [ specification*        ]
         group_spec      => block('group' word:name)     [ (rule_spec|group_spec|spec_reference):specification* ]
         rule_spec       => statement() [ word:name '=>' expression directive* ] transformation_spec*
      end
      
      spec_reference   => statement() [ word:name ]
      precedence_level => statement() [ word:reference+ ]

   end
   

   section strings_spec
      string_spec => statement() [ word:name '=>' string_pattern:definition ]

      group string_pattern
         sp_reference  => word:name
         sp_group      => '(' string_pattern ')'
         sp_branch     => string_pattern '|' string_pattern          @associativity=left
         sp_concat     => string_pattern string_pattern              @associativity=left
         sp_repeated   => string_pattern ('*'|'+'|'?'):repeat_count
         string        => '\'' (unicode_sequence|escape_sequence|general_text)+ '\''
         character_set
      end

      group character_set
         cs_characters => '[' cs_element+ ']'
         cs_difference => character_set:lhs '-' character_set:rhs   @associativity=none
      end
      
      group cs_element
         cs_range      => character:from '-' character:to           @associativity=none
         cs_reference  => '{' word:name '}'              
         character                              
      end

      group character
         unicode_sequence
         escape_sequence
         general_character
      end
   end


   section rule_spec
      macros
         labelled()          => %% (':' word:label)?
         attribute_set(name) => '@' $name '=' %%
      end

      group expression
         reference_exp   => labelled() [ word:name            ]
         string_exp      => labelled() [ string               ]
         variable_exp    => labelled() [ '$' word:name        ]
         group_exp       => labelled() [ '(' expression ')'   ]
         
         sequence_exp    => expression:tree expression:leaf            @associativity=left
         branch_exp      => expression:tree '|' expression:leaf        @associativity=left
         repeated_exp    => expression ('*'|'+'|'?'):repeat_count
         gateway_exp     => '!' !whitespace word
         recovery_commit => ';'
         transclusion    => '%%'
         macro_call      => word:macro_name !whitespace '(' parameters? ')' ('[' expression:body? ']')?
                         ** @parameters = @parameters/(expression|parameter_tree/(@tree|@leaf)//)         
      end

      group parameters
         parameter_tree  => parameters:tree ',' parameters:leaf      @associativity=left
         expression
      end

      group directive
         associativity_directive => attribute_set('associativity') [ ('left'|'right'|'none'):direction ]
      end
      
      precedence
         repeated_exp
         branch_exp
         sequence_exp
      end
   end

   
   section transformations_spec
      group transformation_spec
         assignment_transform => statement() ['**' npath:destination '='  npath:source ]
         append_transform     => statement() ['**' npath:destination '+=' npath:source ]
      end

      group npath
         npath_self_exp       => '.'
         npath_type_exp       => word:type_name
         npath_slot_exp       => '@' word:slot_name
         npath_recurse_exp    => npath '//'
         npath_path_exp       => npath:tree '/' npath:leaf   @associativity=left
         npath_branch_exp     => npath:tree '|' npath:leaf   @associativity=left
         npath_group_exp      => '(' npath ')'
                              ** . = @npath
      end

      precedence
         npath_path_exp
         npath_branch_exp
      end
   end
   

   section macros_spec
      macro_spec => statement() [ word:name (!whitespace '(' parameter_defs? ')')? '=>' expression ]
                 ** @parameter_defs => @parameter_defs/(word|parameter_def_tree/(@tree|@leaf)//)
      
      group parameter_defs
         parameter_def_tree => parameter_defs:tree ',' parameter_defs:leaf   @associativity=left
         word
      end
   end

end

Related Links

in General:
in Discussions:
on site:

Discussion: No comments

Jump to comment form | comments rss | trackback uri

Leave a comment

Markdown: The kinds of formatting markup you'd use in an email will probably work here. For more details on what you can do, check out the Markdown docs.

Which is not a colour? red, green, apple, orange (required)


Site copyright 2007-2008 Chris Poirier.       Powered by Wordpress.       Entries RSS Comments RSS Validate Log in