daily pastebin goal
72%
SHARE
TWEET

Untitled

a guest Feb 14th, 2018 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %skeleton "lalr1.cc"
  2. %defines
  3. %define parser_class_name {tiger_parser}
  4.  
  5. %define api.token.constructor
  6. %define api.value.type variant
  7. %define parse.assert
  8.  
  9. %code requires
  10. {
  11. #include <string>
  12. class ParserDriver;
  13. #include "../ast/nodes.hh"
  14. #include "../utils/errors.hh"
  15. #include "../utils/nolocation.hh"
  16.  
  17. using namespace ast::types;
  18. using utils::nl;
  19. }
  20.  
  21. // The parsing context.
  22. %param { ParserDriver& driver }
  23.  
  24. %locations
  25. %initial-action
  26. {
  27.   // Initialize the initial location.
  28.   @$.begin.filename = @$.end.filename = &driver.file;
  29. };
  30.  
  31. %define parse.trace
  32. %define parse.error verbose
  33.  
  34. %code
  35. {
  36. #include "parser_driver.hh"
  37. }
  38.  
  39. // Define Tiger's symbols and keywords tokens
  40.  
  41. %define api.token.prefix {TOK_}
  42. %token
  43.   EOF  0  "end of file"
  44.   COMMA ","
  45.   COLON ":"
  46.   SEMICOLON ";"
  47.   LPAREN "("
  48.   RPAREN ")"
  49.   LBRACE "{"
  50.   RBRACE "}"
  51.   PLUS "+"
  52.   MINUS "-"
  53.   TIMES "*"
  54.   DIVIDE "/"
  55.   EQ "="
  56.   NEQ "<>"
  57.   LT "<"
  58.   LE "<="
  59.   GT ">"
  60.   GE ">="
  61.   AND "&"
  62.   OR "|"
  63.   ASSIGN ":="
  64.   IF "if"
  65.   THEN "then"
  66.   ELSE "else"
  67.   WHILE "while"
  68.   FOR "for"
  69.   TO "to"
  70.   DO "do"
  71.   LET "let"
  72.   IN "in"
  73.   END "end"
  74.   BREAK "break"
  75.   FUNCTION "function"
  76.   VAR "var"
  77.   UMINUS "uminus"
  78. ;
  79.  
  80. // Define tokens that have an associated value, such as identifiers or strings
  81.  
  82. %token <Symbol> ID "id"
  83. %token <Symbol> STRING "string"
  84. %token <int> INT "integer"
  85.  
  86. // Declare the nonterminals types
  87.  
  88. // %type <Var *> var;
  89. %type <VarDecl *> param;
  90. %type <std::vector<VarDecl *>> params nonemptyparams;
  91. %type <Decl *> decl funcDecl varDecl;
  92. %type <std::vector<Decl *>> decls;
  93. %type <Expr *> expr stringExpr seqExpr callExpr opExpr negExpr
  94.             assignExpr whileExpr forExpr breakExpr letExpr var
  95.             intExpr ifElseExpr ifExpr;
  96.  
  97. %type <std::vector<Expr *>> exprs nonemptyexprs;
  98. %type <std::vector<Expr *>> arguments nonemptyarguments;
  99.  
  100. %type <Expr *> program;
  101.  
  102. %type <boost::optional<Symbol>> typeannotation;
  103.  
  104. %%
  105.  
  106. // Declare precedence rules
  107.  
  108. %nonassoc FUNCTION VAR TYPE DO OF ASSIGN;
  109. %left PLUS MINUS;
  110. %left TIMES DIVIDE;
  111. %left UMINUS;
  112.  
  113. // Declare grammar rules and production actions
  114.  
  115. %start program;
  116.  
  117. program: expr { driver.result_ast = $1; }
  118. ;
  119.  
  120. decl: varDecl { $$ = $1; }
  121.    | funcDecl { $$ = $1; }
  122. ;
  123.  
  124. expr: stringExpr { $$ = $1; }
  125.    | seqExpr { $$ = $1; }
  126.    | var { $$ = $1; }
  127.    | callExpr { $$ = $1; }
  128.    | opExpr { $$ = $1; }
  129.    | negExpr { $$ = $1; }
  130.    | assignExpr { $$ = $1; }
  131.    | whileExpr { $$ = $1; }
  132.    | forExpr { $$ = $1; }
  133.    | ifElseExpr { $$ = $1;}
  134.    | ifExpr {$$ = $1;}
  135.    | breakExpr { $$ = $1; }
  136.    | letExpr { $$ = $1; }
  137.    | intExpr { $$ = $1; }
  138. ;
  139.  
  140.  
  141. varDecl: VAR ID typeannotation ASSIGN expr
  142.   { $$ = new VarDecl(@1, $2, $5, $3); }
  143. ;
  144.  
  145. funcDecl: FUNCTION ID LPAREN params RPAREN typeannotation EQ expr
  146.   { $$ = new FunDecl(@1, $2, $4, $8, $6); }
  147. ;
  148.  
  149. /* Exprs */
  150.  
  151. stringExpr: STRING
  152.   { $$ = new StringLiteral(@1, Symbol($1)); }
  153. ;
  154.  
  155. var : ID
  156.   { $$ = new Identifier(@1, $1); }
  157. ;
  158.  
  159. callExpr: ID LPAREN arguments RPAREN
  160.   { $$ = new FunCall(@1, $3, Symbol($1)); }
  161. ;
  162.  
  163. negExpr: MINUS expr
  164.   { $$ = new BinaryOperator(@1, new IntegerLiteral(@1, 0), $2, o_minus); }
  165.   %prec UMINUS
  166. ;
  167.  
  168. intExpr : INT
  169.     { $$ = new IntegerLiteral(@1, $1) ; };
  170.  
  171. /*opExp: expr op expr*/
  172.  
  173. opExpr: expr PLUS expr   { $$ = new BinaryOperator(@2, $1, $3, o_plus); }
  174.       | expr MINUS expr  { $$ = new BinaryOperator(@2, $1, $3, o_minus); }
  175.       | expr TIMES expr  { $$ = new BinaryOperator(@2, $1, $3, o_times); }
  176.       | expr DIVIDE expr { $$ = new BinaryOperator(@2, $1, $3, o_divide); }
  177.       | expr EQ expr     { $$ = new BinaryOperator(@2, $1, $3, o_eq); }
  178.       | expr NEQ expr    { $$ = new BinaryOperator(@2, $1, $3, o_neq); }
  179.       | expr LT expr     { $$ = new BinaryOperator(@2, $1, $3, o_lt); }
  180.       | expr GT expr     { $$ = new BinaryOperator(@2, $1, $3, o_gt); }
  181.       | expr LE expr     { $$ = new BinaryOperator(@2, $1, $3, o_le); }
  182.       | expr GE expr     { $$ = new BinaryOperator(@2, $1, $3, o_ge); }
  183.       | expr AND expr    {
  184.         $$ = new IfThenElse(@2, $1,
  185.                             new IfThenElse(@3, $3, new IntegerLiteral(nl, 1), new IntegerLiteral(nl, 0)),
  186.                             new IntegerLiteral(nl, 0));}
  187.       | expr OR expr      {
  188.         $$ = new IfThenElse(@2, $1, new IntegerLiteral(nl, 1),
  189.                             new IfThenElse(@3, $3, new IntegerLiteral(nl, 1), new IntegerLiteral(nl, 0)));}
  190. ;
  191.  
  192.  
  193. assignExpr: ID ASSIGN expr
  194.   { $$ = new Assign(@2, new Identifier(@1, $1), $3); }
  195. ;
  196.  
  197. whileExpr: WHILE expr DO expr { $$ = new WhileLoop(@1, $2, $4); }
  198. ;
  199.  
  200. ifElseExpr : IF expr THEN expr ELSE expr
  201.   { $$ = new  IfThenElse(@1, $2, $4, $6); }
  202. ;
  203.  
  204. ifExpr : IF expr THEN expr
  205.   { $$ = new IfThenElse(@1, $2, $4, new Sequence(nl, {})); }
  206. ;
  207.  
  208. forExpr: FOR ID ASSIGN expr TO expr DO expr
  209.   { $$ = new ForLoop(@1, new VarDecl(@2, $2, $4, boost::none), $6, $8); }
  210. ;
  211.  
  212. breakExpr: BREAK { $$ = new Break(@1); }
  213. ;
  214.  
  215. letExpr: LET decls IN exprs END
  216.   { $$ = new Let(@1, $2, new Sequence(nl, $4)); }
  217. ;
  218.  
  219. seqExpr : LPAREN exprs RPAREN { $$ = new Sequence(@1, $2); }
  220. ;
  221.  
  222. exprs: { $$ = std::vector<Expr *>(); }
  223.   | nonemptyexprs { $$ = $1; }
  224. ;
  225.  
  226. nonemptyexprs: expr { $$ = std::vector<Expr *>({$1}); }
  227.   | nonemptyexprs SEMICOLON expr
  228.   {
  229.     $$ = std::move($1);
  230.     $$.push_back($3);
  231.   }
  232. ;
  233.  
  234. arguments: { $$ = std::vector<Expr *>(); }
  235.   | nonemptyarguments { $$ = $1; }
  236. ;
  237.  
  238. nonemptyarguments: expr { $$ = std::vector<Expr *>({$1}); }
  239.   | nonemptyarguments COMMA expr
  240.   {
  241.     $$ = std::move($1);
  242.     $$.push_back($3);
  243.   }
  244. ;
  245.  
  246. params: { $$ = std::vector<VarDecl *>(); }
  247.   | nonemptyparams { $$ = $1; }
  248. ;
  249.  
  250. nonemptyparams: param { $$ = std::vector<VarDecl *>({$1}); }
  251.   | nonemptyparams COMMA param
  252.   {
  253.     $$ = std::move($1);
  254.     $$.push_back($3);
  255.   }
  256. ;
  257.  
  258. decls: { $$ = std::vector<Decl *>();}
  259.   | decls decl
  260.   {
  261.     $$ = std::move($1);
  262.     $$.push_back($2);
  263.   }
  264. ;
  265.  
  266. param: ID COLON ID { $$ = new VarDecl(@1, $1, nullptr, $3); }
  267. ;
  268.  
  269. typeannotation: { $$ = boost::none; }
  270.   | COLON ID { $$ = Symbol($2); }
  271. ;
  272.  
  273. %%
  274.  
  275. void
  276. yy::tiger_parser::error (const location_type& l,
  277.                           const std::string& m)
  278. {
  279.   utils::error (l, m);
  280. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top