Advertisement
Guest User

Untitled

a guest
Oct 8th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 6.92 KB | None | 0 0
  1. %{
  2. (*  File MicroC/CPar.fsy
  3.     Parser specification for micro-C, a small imperative language
  4.     sestoft@itu.dk * 2009-09-29
  5.     No (real) shift/reduce conflicts thanks to Niels Kokholm.
  6. *)
  7.  
  8. open Absyn
  9.  
  10. let compose1 f (g, s) = ((fun x -> g(f(x))), s)
  11. let nl = CstI 10
  12. %}
  13.  
  14. %token <int> CSTINT CSTBOOL
  15. %token <string> CSTSTRING NAME
  16.  
  17. %token CHAR ELSE IF INT NULL PRINT PRINTLN RETURN VOID WHILE FOR
  18. %token PLUS MINUS TIMES DIV MOD PREINC PREDEC
  19. %token EQ NE GT LT GE LE
  20. %token NOT SEQOR SEQAND
  21. %token LPAR RPAR LBRACE RBRACE LBRACK RBRACK SEMI COMMA ASSIGN AMP
  22. %token COLN QSTN                                                     (* Exercise 8.5 *)
  23. %token SWITCH CASE                                                   (* Exercise 8.6 *)
  24. %token EOF
  25.  
  26. %right ASSIGN             /* lowest precedence */
  27. %nonassoc PRINT
  28. %left SEQOR
  29. %left SEQAND
  30. %left EQ NE
  31. %nonassoc GT LT GE LE
  32. %left PLUS MINUS
  33. %left TIMES DIV MOD
  34. %nonassoc NOT AMP
  35. %nonassoc LBRACK          /* highest precedence  */
  36.  
  37. %start Main
  38. %type <Absyn.program> Main
  39.  
  40. %%
  41.  
  42. Main:
  43.     Topdecs EOF                         { Prog $1 }
  44. ;
  45.  
  46. Topdecs:
  47.     /* empty */                         { [] }
  48.   | Topdec Topdecs                      { $1 :: $2 }
  49. ;
  50.  
  51. Topdec:
  52.     Vardec SEMI                         { Vardec (fst $1, snd $1) }
  53.   | Fundec                              { $1 }
  54. ;
  55.  
  56. Vardec:
  57.     Type Vardesc                        { ((fst $2) $1, snd $2) }
  58. ;
  59.  
  60. Vardesc:
  61.     NAME                                { ((fun t -> t), $1)                      }
  62.   | TIMES Vardesc                       { compose1 TypP $2                        }
  63.   | LPAR Vardesc RPAR                   { $2                                      }
  64.   | Vardesc LBRACK RBRACK               { compose1 (fun t -> TypA(t, None)) $1    }
  65.   | Vardesc LBRACK CSTINT RBRACK        { compose1 (fun t -> TypA(t, Some $3)) $1 }
  66. ;
  67.  
  68. Fundec:
  69.     VOID NAME LPAR Paramdecs RPAR Block { Fundec(None,     $2, $4, $6) }
  70.   | Type NAME LPAR Paramdecs RPAR Block { Fundec(Some($1), $2, $4, $6) }
  71. ;
  72.  
  73. Paramdecs:
  74.     /* empty */                         { [] }
  75.   | Paramdecs1                          { $1 }
  76. ;
  77.  
  78. Paramdecs1:
  79.     Vardec                              { [$1]     }
  80.   | Vardec COMMA Paramdecs1             { $1 :: $3 }
  81. ;
  82.  
  83. Block:
  84.     LBRACE StmtOrDecSeq RBRACE          { Block $2 }
  85. ;
  86.  
  87. StmtOrDecSeq:
  88.     /* empty */                         { [] }
  89.   | Stmt StmtOrDecSeq                   { Stmt $1 :: $2 }
  90.   | Vardec SEMI StmtOrDecSeq            { Dec (fst $1, snd $1) :: $3 }
  91. ;
  92.  
  93. Stmt:
  94.     StmtM                               { $1 }
  95.   | StmtU                               { $1 }
  96. ;
  97.  
  98. Cases:                                              (* Exercise 8.6 - Represents 1 or more cases to be used in a switch statement *)
  99.     CASE CSTINT COLN LBRACE Stmt RBRACE                  { [($2, $5)]                                  }
  100.   | CASE CSTINT COLN LBRACE Stmt RBRACE Cases            { ($2, $5) :: $7                              }
  101.  
  102. StmtM:  /* No unbalanced if-else */
  103.     Expr SEMI                                            { Expr($1)                                    }
  104.   | RETURN SEMI                                          { Return None                                 }
  105.   | RETURN Expr SEMI                                     { Return(Some($2))                            }
  106.   | Block                                                { $1                                          }
  107.   | IF LPAR Expr RPAR StmtM ELSE StmtM                   { If($3, $5, $7)                              }
  108.   | WHILE LPAR Expr RPAR StmtM                           { While($3, $5)                               }
  109.   | SWITCH LPAR Expr RPAR LBRACE Cases RBRACE            { Switch($3, $6)                              }   (* Exercise 8.6 - Switch statement *)
  110. ;
  111.  
  112. StmtU:
  113.     IF LPAR Expr RPAR StmtM ELSE StmtU                   { If($3, $5, $7)                              }
  114.   | IF LPAR Expr RPAR Stmt                               { If($3, $5, Block [])                        }
  115.   | WHILE LPAR Expr RPAR StmtU                           { While($3, $5)                               }
  116. ;
  117.  
  118. Expr:
  119.   | Access                              { Access $1           }
  120.   | ExprNotAccess                       { $1                  }
  121. ;
  122.  
  123. ExprNotAccess:
  124.     AtExprNotAccess                     { $1                  }
  125.   | PREDEC Access                       { PreDec $2           }              (* Added from earlier exercise *)
  126.   | PREINC Access                       { PreInc $2           }              (* Added from earlier exercise *)
  127.   | Expr QSTN Expr COLN Expr            { Ternary($1, $3, $5) }              (* Exercise 8.5 - Ternary statement *)
  128.   | Access ASSIGN Expr                  { Assign($1, $3)      }
  129.   | NAME LPAR Exprs RPAR                { Call($1, $3)        }  
  130.   | NOT Expr                            { Prim1("!", $2)      }
  131.   | PRINT Expr                          { Prim1("printi", $2) }
  132.   | PRINTLN                             { Prim1("printc", nl) }
  133.   | Expr PLUS  Expr                     { Prim2("+",  $1, $3) }
  134.   | Expr MINUS Expr                     { Prim2("-",  $1, $3) }
  135.   | Expr TIMES Expr                     { Prim2("*",  $1, $3) }
  136.   | Expr DIV   Expr                     { Prim2("/",  $1, $3) }
  137.   | Expr MOD   Expr                     { Prim2("%",  $1, $3) }
  138.   | Expr EQ    Expr                     { Prim2("==", $1, $3) }
  139.   | Expr NE    Expr                     { Prim2("!=", $1, $3) }
  140.   | Expr GT    Expr                     { Prim2(">",  $1, $3) }
  141.   | Expr LT    Expr                     { Prim2("<",  $1, $3) }
  142.   | Expr GE    Expr                     { Prim2(">=", $1, $3) }
  143.   | Expr LE    Expr                     { Prim2("<=", $1, $3) }
  144.   | Expr SEQAND Expr                    { Andalso($1, $3)     }
  145.   | Expr SEQOR  Expr                    { Orelse($1, $3)      }
  146. ;
  147.  
  148. AtExprNotAccess:
  149.     Const                               { CstI $1             }
  150.   | LPAR ExprNotAccess RPAR             { $2                  }
  151.   | AMP Access                          { Addr $2             }
  152. ;
  153.  
  154. Access:
  155.     NAME                                { AccVar $1           }
  156.   | LPAR Access RPAR                    { $2                  }  
  157.   | TIMES Access                        { AccDeref (Access $2)}
  158.   | TIMES AtExprNotAccess               { AccDeref $2         }
  159.   | Access LBRACK Expr RBRACK           { AccIndex($1, $3)    }  
  160. ;
  161.  
  162. Exprs:
  163.     /* empty */                         { []       }
  164.   | Exprs1                              { $1       }
  165. ;
  166.  
  167. Exprs1:
  168.     Expr                                { [$1]     }
  169.   | Expr COMMA Exprs1                   { $1 :: $3 }
  170. ;
  171.  
  172. Const:
  173.     CSTINT                              { $1       }
  174.   | CSTBOOL                             { $1       }
  175.   | MINUS CSTINT                        { - $2     }
  176.   | NULL                                { -1       }
  177. ;
  178.  
  179. Type:
  180.     INT                                 { TypI     }
  181.   | CHAR                                { TypC     }
  182. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement