Advertisement
Guest User

(Some of) the structure of a --C program

a guest
Oct 23rd, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.49 KB | None | 0 0
  1. /* The top level of a --C program a series of 1 or more function definitions.
  2.  * Not having a function named "main" should be considered an error, though
  3.  * "main" may appear anywhere.
  4.  * Comments can also appear on the top level, but they should be tokenised and
  5.  * then dropped (https://www.youtube.com/watch?v=YLdCKDyxQeI) during the
  6.  * lexical analysis.
  7.  */
  8.  
  9. // I'm going to use regular yacc syntax in this document because.
  10.  
  11. /*     function_definition : identifier identifier LBRACKET param_list RBRACKET block
  12.  *                         ;
  13.  * (names for stuff to be decided)
  14.  *
  15.  * (side note: Params - in the function definition. Args - in the function
  16.  * call.)
  17.  * There are only three types (int, function, void) and one of them is only to
  18.  * say that a function doesn't return anything. We may want to just use an
  19.  * IDENTIFIER token for both of them, but an identifier rule would be useful so
  20.  * we can have it do t[0] = t[1].value for all the other times we need to use
  21.  * such identifiers.
  22.  *     def p_identifier(t):
  23.  *         """identifier : IDENTIFIER"""
  24.  *         t[0] = t[1].value
  25.  * (side note: yacc case sensitivity?)
  26.  * (of course we may want it to output something like ("ID", t[1].value) so the
  27.  * execution part can easily tell what kind of eldrich horror it has on its
  28.  * virtual hands)
  29.  *
  30.  * We might also want to separate type identifiers and variable identifiers so
  31.  * that we can get yacc to deal with the fact that there are only three possible
  32.  * types at parse time.
  33.  *     type : TYPE_INT
  34.  *          | TYPE_FUNCTION
  35.  *          ;
  36.  * If we were feeling particularly enterprising/insane we could even make a
  37.  * separate function_type rule for dealing with functions.
  38.  *     type_void : TYPE_VOID
  39.  *               ;
  40.  *     function_type : type
  41.  *                   | type_void
  42.  *                   ;
  43.  * (using "type_void" instead of "TYPE_VOID" works better)
  44.  */
  45. function negative_double_multiplier(int m) {
  46.     int m2 = -2 * m;
  47.     int f(int n) {
  48.         return n * m2;
  49.     }
  50.     return f;
  51. }
  52.  
  53. /*     block : LCURLY declare_list statement_list RCURLY
  54.  *           ;
  55.  * A block is the body of a function, an if statement, a while loop etc.
  56.  * I'm not sure about this, but would a function definition such as maybe_this
  57.  * below be valid?
  58.  */
  59. int maybe_this(int a, int b)
  60.     return a + b;
  61. /* If so, then the "block" in function_definition above could be replaced with
  62.  * "statement".
  63.  * (side note: statements are executed, expressions are evaluated(?))
  64.  *     statement : block
  65.  *               | expression SEMICOLON
  66.  *               | assign_statement SEMICOLON
  67.  *               | function_definition
  68.  *               | while_statement
  69.  *               | if_statement
  70.  *               | return_statement SEMICOLON
  71.  *               | iter_control SEMICOLON
  72.  *               ;
  73.  * The only difference between a function's block and an if statement's one is
  74.  * that the function also has the calling arguments in the local environment.
  75.  * (also it can't contain iter_control statements (break, continue) as top-level
  76.  * statements, but when not inside a while loop neither can if statements, so
  77.  * we will also have to some logic for that somewhere (while possible,
  78.  * making an alternate statement definition, if_statement definition etc might
  79.  * not be the best (but it would allow for yacc to detect wrongly placed
  80.  * iter_control statements))).
  81.  *
  82.  * Declaration statements (while they are statements) aren't here because they
  83.  * aren't allowed in statement_list, only in declare_list.
  84.  *
  85.  * One other point: do embedded function definitions have to be at the top of
  86.  * the block (that is to say in the declare_list)? If so then obviously
  87.  * function_definition shouldn't be here.
  88.  *
  89.  *     declare_start : type identifier
  90.  *                   ;
  91.  * I separate this from declare because it can also be used in param_list.
  92.  * (unrelated: (lambda x: x(x))(lambda y: y(y)))
  93.  *     declare : declare_start SEMICOLON
  94.  *             | declare_start ASSIGN expression SEMICOLON
  95.  *             ;
  96.  * ASSIGN is "=", I used the name because it is less ambiguous. Equality is
  97.  * "EQ".
  98.  *
  99.  *     expression : function_call
  100.  *                | unary_expression
  101.  *                | infix_expression
  102.  *                | bracket_expression
  103.  *                | number
  104.  *                | identifier
  105.  *                ;
  106.  * I may have forgotten one or two things here.
  107.  */
  108.  
  109. int main() {
  110.     int x;
  111.     function multi = negative_double_multiplier(7);
  112.     x = multi(multi(5));
  113.     return maybe_this(2, x);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement