Advertisement
MrHn

Micro C Parser

Oct 7th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 5.67 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 FOR ELSE IF INT NULL PRINT PRINTLN RETURN VOID WHILE
  18. %token PLUS MINUS TIMES DIV MOD
  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 EOF
  23.  
  24. %right ASSIGN             /* lowest precedence */
  25. %nonassoc PRINT
  26. %left SEQOR
  27. %left SEQAND
  28. %left EQ NE
  29. %nonassoc GT LT GE LE
  30. %left PLUS MINUS
  31. %left TIMES DIV MOD
  32. %nonassoc NOT AMP
  33. %nonassoc LBRACK          /* highest precedence  */
  34.  
  35. %start Main
  36. %type <Absyn.program> Main
  37.  
  38. %%
  39.  
  40. Main:
  41.     Topdecs EOF                         { Prog $1 }
  42. ;
  43.  
  44. Topdecs:
  45.     /* empty */                         { [] }
  46.   | Topdec Topdecs                      { $1 :: $2 }
  47. ;
  48.  
  49. Topdec:
  50.     Vardec SEMI                         { Vardec (fst $1, snd $1) }
  51.   | Fundec                              { $1 }
  52. ;
  53.  
  54. Vardec:
  55.     Type Vardesc                        { ((fst $2) $1, snd $2) }
  56. ;
  57.  
  58. Vardesc:
  59.     NAME                                { ((fun t -> t), $1)                      }
  60.   | TIMES Vardesc                       { compose1 TypP $2                        }
  61.   | LPAR Vardesc RPAR                   { $2                                      }
  62.   | Vardesc LBRACK RBRACK               { compose1 (fun t -> TypA(t, None)) $1    }
  63.   | Vardesc LBRACK CSTINT RBRACK        { compose1 (fun t -> TypA(t, Some $3)) $1 }
  64. ;
  65.  
  66. Fundec:
  67.     VOID NAME LPAR Paramdecs RPAR Block { Fundec(None,     $2, $4, $6) }
  68.   | Type NAME LPAR Paramdecs RPAR Block { Fundec(Some($1), $2, $4, $6) }
  69. ;
  70.  
  71. Paramdecs:
  72.     /* empty */                         { [] }
  73.   | Paramdecs1                          { $1 }
  74. ;
  75.  
  76. Paramdecs1:
  77.     Vardec                              { [$1]     }
  78.   | Vardec COMMA Paramdecs1             { $1 :: $3 }
  79. ;
  80.  
  81. Block:
  82.     LBRACE StmtOrDecSeq RBRACE          { Block $2 }
  83. ;
  84.  
  85. StmtOrDecSeq:
  86.     /* empty */                         { [] }
  87.   | Stmt StmtOrDecSeq                   { Stmt $1 :: $2 }
  88.   | Vardec SEMI StmtOrDecSeq            { Dec (fst $1, snd $1) :: $3 }
  89. ;
  90.  
  91. Stmt:
  92.     StmtM                               { $1 }
  93.   | StmtU                               { $1 }
  94. ;
  95.  
  96. StmtM:  /* No unbalanced if-else */
  97.     Expr SEMI                           { Expr($1)             }
  98.   | RETURN SEMI                         { Return None          }
  99.   | RETURN Expr SEMI                    { Return(Some($2))     }
  100.   | Block                               { $1                   }
  101.   | FOR LPAR Expr SEMI Expr SEMI Expr RPAR StmtU {Block[Stmt(Expr($3)); While($5, Block[Stmt($9); Stmt(Expr($7))])]}
  102.   | IF LPAR Expr RPAR StmtM ELSE StmtM  { If($3, $5, $7)       }
  103.   | WHILE LPAR Expr RPAR StmtM          { While($3, $5)        }
  104. ;
  105.  
  106. StmtU:
  107.     IF LPAR Expr RPAR StmtM ELSE StmtU  { If($3, $5, $7)       }
  108.   | IF LPAR Expr RPAR Stmt              { If($3, $5, Block []) }
  109.   | WHILE LPAR Expr RPAR StmtU          { While($3, $5)        }
  110. ;
  111.  
  112. Expr:
  113.     Access                              { Access $1           }
  114.   | ExprNotAccess                       { $1                  }
  115. ;
  116.  
  117. ExprNotAccess:
  118.     AtExprNotAccess                     { $1                  }
  119.   | Access ASSIGN Expr                  { Assign($1, $3)      }
  120.   | NAME LPAR Exprs RPAR                { Call($1, $3)        }  
  121.   | NOT Expr                            { Prim1("!", $2)      }
  122.   | PRINT Expr                          { Prim1("printi", $2) }
  123.   | PRINTLN                             { Prim1("printc", nl) }
  124.   | Expr PLUS  Expr                     { Prim2("+",  $1, $3) }
  125.   | Expr MINUS Expr                     { Prim2("-",  $1, $3) }
  126.   | Expr TIMES Expr                     { Prim2("*",  $1, $3) }
  127.   | Expr DIV   Expr                     { Prim2("/",  $1, $3) }
  128.   | Expr MOD   Expr                     { Prim2("%",  $1, $3) }
  129.   | Expr EQ    Expr                     { Prim2("==", $1, $3) }
  130.   | Expr NE    Expr                     { Prim2("!=", $1, $3) }
  131.   | Expr GT    Expr                     { Prim2(">",  $1, $3) }
  132.   | Expr LT    Expr                     { Prim2("<",  $1, $3) }
  133.   | Expr GE    Expr                     { Prim2(">=", $1, $3) }
  134.   | Expr LE    Expr                     { Prim2("<=", $1, $3) }
  135.   | Expr SEQAND Expr                    { Andalso($1, $3)     }
  136.   | Expr SEQOR  Expr                    { Orelse($1, $3)      }
  137. ;
  138.  
  139. AtExprNotAccess:
  140.     Const                               { CstI $1             }
  141.   | LPAR ExprNotAccess RPAR             { $2                  }
  142.   | AMP Access                          { Addr $2             }
  143. ;
  144.  
  145. Access:
  146.     NAME                                { AccVar $1           }
  147.   | LPAR Access RPAR                    { $2                  }  
  148.   | TIMES Access                        { AccDeref (Access $2)}
  149.   | TIMES AtExprNotAccess               { AccDeref $2         }
  150.   | Access LBRACK Expr RBRACK           { AccIndex($1, $3)    }  
  151. ;
  152.  
  153. Exprs:
  154.     /* empty */                         { []       }
  155.   | Exprs1                              { $1       }
  156. ;
  157.  
  158. Exprs1:
  159.     Expr                                { [$1]     }
  160.   | Expr COMMA Exprs1                   { $1 :: $3 }
  161. ;
  162.  
  163. Const:
  164.     CSTINT                              { $1       }
  165.   | CSTBOOL                             { $1       }
  166.   | MINUS CSTINT                        { - $2     }
  167.   | NULL                                { -1       }
  168. ;
  169.  
  170. Type:
  171.     INT                                 { TypI     }
  172.   | CHAR                                { TypC     }
  173. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement