Up Next

Revision: Compilers and Factories

How do you share a single grammar amongst several tools that operate on the same language?


... systematically named functions. ... or methods.
%right ASSIGN /* lowest precedence */
%left PLUS    /* highest precedence */
%right ASSIGN /* lowest precedence */
%left PLUS    /* highest precedence */
 
%{
translator *context;
%}
%%
%%
expression
  : IDENTIFIER
     { $$ = parse_expression_identifier($1); }
  | expression PLUS expression
     { $$ = parse_expression_addition($1, $3); }
  | expression ASSIGN expression
     { $$ = parse_expression_assignment($1, $3); }
  | etc etc
  ;
expression
  : IDENTIFIER
     { $$ = context->expression_identifier($1); }
  | expression PLUS expression
     { $$ = context->expression_addition($1, $3); }
  | expression ASSIGN expression
     { $$ = context->expression_assignment($1, $3); }
  | etc etc
  ;
Aha! What if the methods were virtual?

You get an AST factory abstraction you can share amongst tools:

/* vim: set ts=8 sw=4 et : */